Archived

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

nick316

crazy hard c problem

Recommended Posts

nick316    100
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
Cold_Steel    835
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
ZealousElixir    256
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
Gladiator    127
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
nick316    100
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
Taulin    100
HardGround,
Yep, you better not say anything or all of these people here on the boards will rush out and apply for a job at where you work. If you confirmed one interview question, everyone is bound to pass, and the influx of good programmers will cause you to get laid off too.

I believe Nick361 meant you can not use any of the standard conversion functions. I.E. you need to prove you understand conversion. This is indeed a homework problem found in most beginner CS coarses and books.

Share this post


Link to post
Share on other sites
Cold_Steel    835
quote:
Original post by Taulin
I believe Nick361 meant you can not use any of the standard conversion functions. I.E. you need to prove you understand conversion. This is indeed a homework problem found in most beginner CS coarses and books.


Ohhhh...then that''s easy. I was gonna say, it''s going to be mighty hard to get the characters without being able to use any functions to access standard input

Share this post


Link to post
Share on other sites
nick316    100
As for the Gladiators response, that is not what I was looking for because I phrased the question wrong.

In C you can declare an integer and give it a hex value like this:
int var;
var = 0x34A;

My question should have been can you read a string from
standard input and put it in an integer variable like above?

Sorry for the confusion.













[edited by - nick316 on October 4, 2002 10:28:33 AM]

Share this post


Link to post
Share on other sites
davepermen    1047
this is a really easy question and i bet you could find the solution on the web as well.
i will not try to solve your problem as long as you don''t show me your ideas on how to solve it yourself.
thats the way it works here..

"take a look around" - limp bizkit
www.google.com

Share this post


Link to post
Share on other sites
Stoffel    250

  
int hexToInt (const char *str)
// string must be in format "0x####", hex letters must be caps.

// Stops at first unrecognized char or end of string.

// No overflow protection.

{
int val = 0;
str += 2; // skip "0x"

while (true)
{
if (*str >= ''0'' && *str <= ''9'')
{
val = (val << 4) | (*str - ''0'');
}
else if (*str >= ''A'' && *str <= ''F'')
{
val = (val << 4) | (*str - ''A'' + 10);
}
else
return val;
++str;
}
}

Bare-bones, but solves the problem. BTW, if this is "crazy hard", no way you''d be able to pass our interview.

Share this post


Link to post
Share on other sites