Hex to integers

Started by
2 comments, last by White Crow 20 years, 9 months ago
Hi I had to write a function that convert a string of hexadecimal digits into it''s equivalent integers value. Now I do not know how that hexadecimal system works so I am asking if someone could explain ti to me or point me to a website or something. Whitecrow
~~~~~~I'll make you an offer you can't refuse...
Advertisement
http://www.google.com/search?q=hexadecimal




"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
The easy way:
#include <stdio.h>int main() {  int var;  printf("Enter the number in hex: ");  scanf("%x", &var);  printf("The number in decimal is: %d\n", var); }


An actual function:
// example, HexToDec("abc") will return 2748.unsigned int HexToDec(const char *s) {  unsigned int result = 0;    for(; *s ; ++s)   if(*s >= ''0'' && *s <= ''9'')    result = result*16+(*s-''0'');   else if(*s >= ''a'' && *s <= ''f'')    result = result*16+(*s-''a''+10);   else if(*s >= ''A'' && *s <= ''F'')    result = result*16+(*s-''A''+10);   else    break;    return result; }
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
quote:Original post by White Crow
Hi

I had to write a function that convert a string of hexadecimal digits into it''s equivalent integers value. Now I do not know how that hexadecimal system works so I am asking if someone could explain ti to me or point me to a website or something.

Whitecrow


There''s tons of information all over the internet, but I''m feeling charitable.

Hexadecimal is base 16. Decimal, our normal number system, is base 10.

Under base decimal, the number 5453 is really:
5x10^3 + 4x10^2 + 5x10^1 + 3x10^0

Under hexadecimal, it''s:
5x16^3 + 4x16^2 + 5x16^1 + 3x16^0

And since there are only 10 Arabic numerals (0123456789), to represent 10, 11, 12, 13, 14 and 15 as single digits the letters ABCDEF are used.

So the number 26 is 26 in decimal, and is 1A in hexadecimal.
-------------"On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question."- Charles Babbage (1791-1871)

This topic is closed to new replies.

Advertisement