Jump to content
  • Advertisement

Archived

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

nick316

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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!


-G|aD-

Share this post


Link to post
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 this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!