#### Archived

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

# crazy hard c problem

## 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.

Which company?

##### Share on other sites
I can''t tell you now, or everyone would know one of the questions we ask on the programmers test.

##### Share on other sites
You can always send me an email.

I thought so.

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

##### Share on other sites

  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.

• ### Forum Statistics

• Total Topics
628300
• Total Posts
2981894

• 9
• 9
• 11
• 10
• 10