Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert Hex to binary

Status
Not open for further replies.

adamr99

Programmer
Jun 25, 2001
59
US
Pretty self explanitory. I need to convert a hex string into a binary one (keeping the leading zeros). Assume that they are both in an array. I just can't seem to figure this out.... (I am sure that it is easy)

Thanks

Adam
 
didn't do much with HEX to binary translation, but from Dec to bin is simple, and from Hex to dec is simple... perhaps the solution lies in somthing like this psedo code:
//through binary...
int num
for end-1 to begin()
if HEX is alhpa,
switch (HEX)
case(a)
num += 10
case(b)
...
else
num += ((int)HEX - 48)
//Then count leading zeros
int zeros = 0;
for int i = 0; HEX == '0'; i++)
zeros++

//then do your recursive binary translation function...
void binary(vector<int>& bin, int dec)
{
if(!dec == 0)
{
bin.push_front(dec%2);
binary(bin, dec/2);
}
return;
}

//then push front the zeros...
for(int i = 0; i <= zeros; i++)
bin.push_front(0);


You get the idea...
 
well the easiest way is to use bit-wise operations in a for loop

char binValue[33];
memset(binValue,0,33);
int hexVal = 0xABCDEF;

for(int i = 31,j=0;i>=0;i--,j++)
{
binValue = hexVal & (1<<j);
}

cout<<binValue;

If you are trying to extract values from either a hex or binary input string (say the user enters a hex value) you can use strtol with base 16 or base 2 for conversions. The return value is in decimal.

Matt
 
well the easiest way is to use bit-wise operations in a for loop

char binValue[33];
memset(binValue,0,33);
int hexVal = 0xABCDEF;

for(int i = 31,j=0;i>=0;i--,j++)
{

FORGOT ONE THING... sorry bout that
binValue = (hexVal & (1<<j) ? '1':'0');

}

cout<<binValue;

If you are trying to extract values from either a hex or binary input string (say the user enters a hex value) you can use strtol with base 16 or base 2 for conversions. The return value is in decimal.

Matt
 
APPARENTLY I FORGOT A FEW THINGS... Like code tags

well the easiest way is to use bit-wise operations in a for loop

Code:
char binValue[33];
memset(binValue,0,33);
int hexVal = 0xABCDEF;

for(int i = 31,j=0;i>=0;i--,j++)
{

FORGOT ONE THING... sorry bout that
   binValue[i] =
(hexVal & (1<<j) ? '1':'0');

}

cout<<binValue;

If you are trying to extract values from either a hex or binary input string (say the user enters a hex value) you can use strtol with base 16 or base 2 for conversions. The return value is in decimal.

Matt
 
hmm... i think you could do this..
first of all if you encounter an A, it's gonna be a 10, and so on.

theres an alogorithm that basically says, take the remainder when you divide by the base you are converting to.
so if you have the number 4.
4%2 = 0
4/2 = 2, 2 goes down.
2%2 = 0
2/2 = 1, 1 goes down.
1%2 = 1
1/2 = 0, 0 goes down
0%2 = 0

so in turn you have 4 equaling 0100, which is correct.

this should work with hex numbers if indeed you convert the letters to numbers.

to make life easier, as you do this, push the modulus to a stack, and to print it out, just pop and print, pop and print, and it'll print it in the right way.

just another way to do it, the other ways are probably much better.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top