?
Solved

Binary to decimal

Posted on 2006-06-05
11
Medium Priority
?
881 Views
Last Modified: 2013-11-15
I have a 4 byte unsigned char.  I am taking the 1st byte, and extracting bits 29 through 27 as well as 13 and 11.
I need to then take each set of 3 bits and get the decimal value of it.

Is there a graceful way of doing this other than extracting each bit manually then calculating the decimal value?
0
Comment
Question by:jewee
10 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 1400 total points
ID: 16838676
Actually that's easy, that's what a binary '&' is for

#define EXTRACT_MASK (1 << 29) | (1 << 28) | (1 << 27) | (1 << 13) | (1 << 11)

unsigned char uc[4];
unsigned long ul = *(unsigned long*) uc;

unsigned long result = ul & EXTRACT_MASK;

printf("Result: %u\n", result)
0
 
LVL 3

Expert Comment

by:srinims
ID: 16839709
for>>
>>I need to then take each set of 3 bits and get the decimal value of it.

#define EXTRACT_MASK1 (1 << 29) | (1 << 28) //having bit value 1 at position 29 and 28, and other places filled 0's
#define EXTRACT_MASK2 (1 << 27) | (1 << 13) | (1 << 11)

unsigned char uc[4];
unsigned long ul = *(unsigned long*) uc;

printf("Result: %u %u\n", (ul & EXTRACT_MASK1), (ul & EXTRACT_MASK2))
0
 
LVL 5

Assisted Solution

by:Dragon_Krome
Dragon_Krome earned 200 total points
ID: 16840391
The solutions given above won't give you the actual decimal value of the 3 bits, but the result of AND-ing the extraction masks with the initial number. The results need to be right shifted for this to be accurate.
You said 13 and 11 and then you mention "each set of 3 bits", so i assume you meant 13 to 11 too.

Here's a modified solution based on jkr's:

#define EXTRACT_MASK (7 << 11)|(7<<27)

unsigned char uc[4];
unsigned long ul = *(unsigned long*) uc;

unsigned long result = ul & EXTRACT_MASK;

unsigned char group_11_13 = (result >> 11) & 7;
unsigned char group_27_29 = (result >> 27) & 7;

printf("%u %u", group_11_13, group_27_29);



0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 53

Expert Comment

by:Infinity08
ID: 16840397
It's still not what jewee is looking for i think (correct me if I'm wrong). Here's my attempt :

#define EXTRACT_1(ul) (((ul) & 0x38000000) >> 27)
#define EXTRACT_2(ul) (((ul) & 0x00003800) >> 11)

unsigned char uc[4];
unsigned long ul = *(unsigned long*) uc;

printf("Result: %u %u\n", EXTRACT_1(ul), EXTRACT_2(ul))
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 16840409
Dragon_Krome posted the same time as me - his solution should work too, although a bit more complicated.
0
 
LVL 8

Assisted Solution

by:kaliyugkaarjun
kaliyugkaarjun earned 400 total points
ID: 16842330


Hi jewee , chk out this code ..enter any binay no. and u will get its decimal, hexadecimal  as well as octal representation..
cheers.

prog starts below
//-----------------
#include <stdio.h>
#include <string.h>
 
int bin2dec(char *bin);
 
int main()
{
  char bin[80] = "";
  char *p;
  int  dec;
 
  while(strcmp(bin,"0"))
  {
    printf("\n Enter a binary number (just 0 to EXIT): ");
    fgets(bin, sizeof(bin), stdin);
    // check for and remove trailing \n
    if ((p = strchr(bin,'\n')) != NULL)
    {
      *p = '\0';
    }
    dec = bin2dec(bin);
    if (dec) printf("\nDecimal = %d  Hexadecimal = 0x%04X  Octal = 0%o\n",dec,dec,dec);
  }
 
  getchar();  // wait
  return 0;
}
 
// convert a binary string to a decimal number, returns decimal value
int bin2dec(char *bin)  
{
  int  b, k, m, n;
  int  len, sum = 0;
 
  len = strlen(bin) - 1;
  for(k = 0; k <= len; k++)
  {
    n = (bin[k] - '0'); // char to numeric value
    if ((n > 1) || (n < 0))
    {
      puts("\n\n ERROR! BINARY has only 1 and 0!\n");
      return (0);
    }
    for(b = 1, m = len; m > k; m--)
    {
      // 1 2 4 8 16 32 64 ... place-values, reversed here
      b *= 2;
    }
    // sum it up
    sum = sum + n * b;
    //printf("%d*%d + ",n,b);  // uncomment to show the way this works
  }
  return(sum);
}
0
 

Expert Comment

by:padamjs
ID: 16849901
This is a very simple way of extracting the decimal value of bits 29-27 and 13-11 stored in the unsignied char bytes:

unsigned char data[4];

///fill value in this char data

dec1=(data[0] & 0x1c)>>2;
dec2=(data[2] & 0x1c)>>2;

In binary, 0x1c is 001 1100.  So for the first byte, this mask will get you bits in positions 29-27 and in the third byte, it will get you bits 13-11.
The "shift right" two bits to get the decimal value.

HTH.
Padam JS.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 16850139
>> In binary, 0x1c is 001 1100
Bit counting starts from 0, so you need 0x38 (and a shift of 3) as in my earlier example.
0
 

Expert Comment

by:padamjs
ID: 16851373
>>Bit counting starts from 0, so you need 0x38 (and a shift of 3) as in my earlier example.
Bit counting indeed starts at 0.
However, 0x1c>>2 is equal to 0x38>>3!
Also, you are generating a mask with 0x38, which very well can be done using 0x1c.

I am not generating any mask, and hence I can just And with the mask 0x1c.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 16851507
My point was that you're extracting the wrong bits :

    dec1=(data[0] & 0x1c)>>2;

will extract bits 28-26 and not 29-27 as requested.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

While Plesk offers many potential benefits to website administrators, including compatibility with Windows Server and other leading technologies, the company has also been working to differentiate it from other control panels for content management…
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question