Hex to bin
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > Hex to bin




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Hex to bin  
Josh Parker


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-26-04 07:34 PM

What would be the best way to convert Hex to bin?
I am using this code to convert a Dec to Hex:

char hexstring[10];
unsigned int number = 800;
sprintf(hexstring, "%x", number);

What would be the next step for me to take?  I have to do it from Dec
to Hex and then from Hex to Binary?  Any suggestions would be really
appreciated.  Thank you very much





[ Post a follow-up to this message ]



    Re: Hex to bin  
Jens.Toerring@physik.fu-berlin.de


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-26-04 07:34 PM

Josh Parker <jcp1217@mail.ecu.edu> wrote:
> What would be the best way to convert Hex to bin?
> I am using this code to convert a Dec to Hex:

> char hexstring[10];
> unsigned int number = 800;
> sprintf(hexstring, "%x", number);

> What would be the next step for me to take?  I have to do it from Dec
> to Hex and then from Hex to Binary?  Any suggestions would be really
> appreciated.  Thank you very much

First of all that's not decimal to hex but what you do there is create
a string representation of the number stored in memory (in whatever
respresentation the machine likes, but you can be rather sure that
it's binary;-). You should also be careful, ints could be longer than
9 chars on some machines, in which case you might past the end of the
array for the string!

To get a binary string representation you write your own function,
something like this (but take care, it's completely untested):

char bin_string[ CHAR_BIT * sizeof( unsigned int ) + 1 ];
char *p = bin_string;
unsigned int number = 800;
unsigned int cur = 1 << ( CHAR_BIT * sizeof( unsigned int ) - 1 );

while ( cur )
{
*p++ = ( number & cur ) ? '1' : '0';
cur >>= 1;
}
*p = '\0';

Things get a tiny bit more complicted when you don't want leading
zeros, but that shouldn't be too difficult to figure out...

Regards, Jens
--
\   Jens Thoms Toerring  ___  Jens.Toerring@physik.fu-berlin.de
\__________________________  http://www.toerring.de





[ Post a follow-up to this message ]



    Re: Hex to bin  
Nick Landsberg


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
02-26-04 08:34 PM



Jens.Toerring@physik.fu-berlin.de wrote:

> Josh Parker <jcp1217@mail.ecu.edu> wrote:
>=20 
>=20
>=20 
>=20
>=20 
>=20
>=20
> First of all that's not decimal to hex but what you do there is create
> a string representation of the number stored in memory (in whatever
> respresentation the machine likes, but you can be rather sure that
> it's binary;-). You should also be careful, ints could be longer than
> 9 chars on some machines, in which case you might past the end of the
> array for the string!
>=20
> To get a binary string representation you write your own function,
> something like this (but take care, it's completely untested):
>=20
> char bin_string[ CHAR_BIT * sizeof( unsigned int ) + 1 ];
> char *p =3D bin_string;
> unsigned int number =3D 800;
> unsigned int cur =3D 1 << ( CHAR_BIT * sizeof( unsigned int ) - 1 );
>=20
> while ( cur )
> {
>     *p++ =3D ( number & cur ) ? '1' : '0';
>     cur >>=3D 1;
> }
> *p =3D '\0';
>=20
> Things get a tiny bit more complicted when you don't want leading
> zeros, but that shouldn't be too difficult to figure out...
>=20
>                                        Regards, Jens

Actually, I've found that a lookup table consisting of
16 entries (assuming that char is 8 bits) works a lot
faster.  Convert the single char in the hex string to some int=20
representation (e.g. 'f' or 'F' converts to 15) and use that
as in index into the lookup table, which has an array
of strings ( char *table[] ) with binary
representation for that hex character as a null terminated
string.




--=20
=D1
"It is impossible to make anything foolproof because fools are so=20
ingenious" - A. Bloch






[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 04:12 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register