c - How to convert binary int array to hex char array? -
say have 32 bit long array of 32 binary digits , want output in hex form. how that? have right long , don't know how compare 4 binary digits corresponding hex number
this have right break 32 bit number 4 bit binary , try find matching number in binarydigits
char hexchars[16] ={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}; char * binarydigits[16] = {"0000","0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"}; int binarynum[32]= {'0','0','1','0','0','0','0','1','0','0','0','0','1','0','0','1','0','0','0','0','0','0','0','0','0','0','0','0','1','0','1','0'}; int currentblock, hexdigit; int a=0, b=1, i=0; while (a<32) { for(a=i+3;a>=i;a--) { current=binarynum[a]; temp=current*b; currentblock=currentblock+temp; b*=10; } i=a; while(match==0) { if(currentblock != binarydigits[y]) y++; else { match=1; hexdigit=binarydigits[y]; y=0; printf("%d",hexdigit); } } } printf("\n%d",currentblock);
i apologize if isn't crux of issue, say
i have 32
bit
long array of 32 binary digits
however, int binarynum[32]
32-integer
long array (4 bytes
per int
, 8 bits
per byte
= 4 * 8 * 32
(1024
bits)). making things unclear.
further, assigning ascii character values '0'
(which 0x30
hex or 48
decimal) , '1'
(0x31
, 49
) each location in binarynum
. can it, , gymnastics compare each value form
32
bit
long array of 32 binary digits
but if have, why not write way? (as binary constant). give 32-bit
binary value. example:
#include <stdio.h> int main (void) { unsigned binarynum = 0b00100001000010010000000000001010; printf ("\n binarynum : 0x%8x (%u decimal)\n\n", binarynum, binarynum); return 0; }
output
$ ./bin/binum32 binarynum : 0x2109000a (554237962 decimal)
if not difficulty lies, please explain further, or again, trying accomplish.
Comments
Post a Comment