Professional Web Applications Themes

Hex to bin - UNIX Programming

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...

  1. #1

    Default Hex to bin

    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
    Josh Guest

  2. #2

    Default Re: Hex to bin

    Josh Parker <ecu.edu> wrote: 
     
     

    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 ___ fu-berlin.de
    \__________________________ http://www.toerring.de
    Jens.Toerring@physik.fu-berlin.de Guest

  3. #3

    Default Re: Hex to bin



    fu-berlin.de wrote:
     
    >
    >  
    >
    >  
    >
    >
    > 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[/ref]

    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
    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.




    --

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

    Nick Guest

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139