crazy hard c problem
How would a person go about entering a string like
"0xFF" and convert it to an int variable without using the
standard c library functions.
Any takers?
Without using the C library functions? Do you mean any library function? Does this include scanf? Because if you can''t use any library function, I would think it would be impossible, unless you wrote an assembly routine to access the hardware directly.
Here's a bit of help....
EDIT: On second thought, that code is really ugly. This isn't a hard problem. Why not invoke your problem-solving skills or at least show us evidence that you've tried to work it out?
[edited by - zealouselixir on October 3, 2002 9:19:31 PM]
EDIT: On second thought, that code is really ugly. This isn't a hard problem. Why not invoke your problem-solving skills or at least show us evidence that you've tried to work it out?
[edited by - zealouselixir on October 3, 2002 9:19:31 PM]
Here''s an example how to convert hex to integer in general:
0xFF = 16*15+15 = 255
0xB5 = 16*11+5 = 181
The following is an example function to convert hex string to integer (byte):
The same idea is used for any number types -- integers, longs, etc.) just use a loop and evaluate the hex number accordingly.
Hope this helps!
-G|aD-
0xFF = 16*15+15 = 255
0xB5 = 16*11+5 = 181
The following is an example function to convert hex string to integer (byte):
unsigned char getHexValue(char chHex) { chHex = toupper(chHex); if (chHex >= ''0'' && chHex <= ''9'') return (unsigned char) chHex-''0''; if (chHex >= ''A'' && chHex <= ''F'') return (10+(unsigned char) chHex-''A''); return 0; }unsigned char hexStringToByte(char* strHex) { int iStart = 0; if (strHex[0] == ''0'' && strHex[1] == ''x'') iStart++; return getHexValue(strHex[iStart])*16 + getHexValue(strHex[iStart+1]); }
The same idea is used for any number types -- integers, longs, etc.) just use a loop and evaluate the hex number accordingly.
Hope this helps!
-G|aD-
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement