#### Archived

This topic is now archived and is closed to further replies.

# crazy hard c problem

This topic is 5764 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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?

##### Share on other sites
This isn''t that hard.

Sounds like a homework problem.

##### Share on other sites
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.

##### Share on other sites
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]

##### Share on other sites
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):

  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!

##### Share on other sites
That''s right about the cannot use scanf or any functions like
that. My guess is that you have to read it in character by character and some how access the ascii codes to convert it.
It''s pretty tricky without any of those nice helper functions.

##### Share on other sites
Hmmm...you wouldn''t happen to be applying for a job at a big game company, would you?

##### Share on other sites
I don't know. Are you?

[edited by - nick316 on October 3, 2002 9:38:50 PM]

sweet.

1. 1
Rutin
24
2. 2
3. 3
JoeJ
18
4. 4
5. 5

• 38
• 23
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631706
• Total Posts
3001835
×