crazy hard c problem

Started by
17 comments, last by nick316 21 years, 6 months ago
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?
Advertisement
This isn''t that hard.

Sounds like a homework problem.
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.
______________________________"Man is born free, and everywhere he is in chains" - J.J. Rousseau
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]

[twitter]warrenm[/twitter]

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!


-G|aD-
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.
Hmmm...you wouldn''t happen to be applying for a job at a big game company, would you?
I don't know. Are you?

[edited by - nick316 on October 3, 2002 9:38:50 PM]
Nope. I already work there.
sweet.

This topic is closed to new replies.

Advertisement