I wanted to find a solution which only uses a lookup table and benchmark that against a solution that uses arithmetics instead.
We end up with the following test cases: arithmetic solution 256 entries lookup table 32768 entries lookup table 65536 entries lookup table, the first lookup table is easy to generate using some python: #!/usr/bin/env python import sys, struct dout.
Write unsigned char base16_decoding_table1256 n for i in xrange(256 try: j str(int(chr(i 16) except: j '0' dout.
Firstly, we green farm 3 cheats want to have the whole test data in memory so that we avoid disk I/O influencing the test.Write n l "0" for a in for b in lstruct.Printf "char *testdata cat /dev/urandom tr -d -c " abcdefabcdef" dd count100 iflagfullblock bs1M; printf n" testdata.Unpack H ab)0 str(int(ab, 16) line "unsigned char base16_decoding_table3d 256*256) for e in l: line e if len(line) 70: dout.Next, we create the lookup tables.Thanks for the help, guys!Unpack H ab)0 str(int(ab, 16) line "unsigned char base16_decoding_table2d 128*256) for e in l: line e if len(line) 70: dout.Once again, this comes down to me not being worth anything with address manipulation.Either the lookup table maps individual ascii hex characters to half bytes or it maps two hex characters to a full byte.

However, the answer he marked "correct" puts the lie to that argument.
Binary A 1010 B 1011 C 1100 D 1101 E 1110 F 1111 Hexadecimal to binary table Hexadecimal to binary conversion examples: For example we want to convert or change hexadecimal number 65B2 to binary.
Below is my modification to the given code, with the function call and the output.
Up vote 1 down vote, there are many ways to solve this question that use some arithmetics to convert from ascii character ranges 0-9 and a-f (or A-F) to binary.
Possible Duplicate: Convert a long hex string in to int array with sscanf.What am I doing wrong?In the former case, the lookup table has to have 256 entries.Edit, okay, so I solved that problem, and I have another question (I didn't want to open up a new thread!).Answering chustar (the OQ'er) in the comments, I'd have to agree that if you understand how to do it, how/why it works, and how to figure it out yourself in the future, then yes, that would be a good thing.For this we will replace each hexadecimal digit to binary values using the above table: Hexadecimal number: 6 5 B 2 Binary values: So (65B2)16 ( )2.So the code is: int hex2bin (int n) int i,k,mask; mask 1 i; tdu 2 patch 3 k n mask; k0?printf 0 printf 1 return 0; If you want to display any input(such as "0x10 0x4 you can declare a function like this: int hex2bin (const char *str) char.So we only need a lookup table of entries.C And run it:./a.out The result: arithmetic solution: 437.17 s 256 entries lookup table: 117.80 s 32768 entries lookup table:.33 s 65536 entries lookup table:.66 s This we can conclude lookup tables beat arithmetic solutions any time and that wasting memory for bigger.