Public Group

#### Archived

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

# Hex to integers

This topic is 5523 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

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

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

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.

1. 1
2. 2
3. 3
Rutin
18
4. 4
JoeJ
14
5. 5

• 14
• 10
• 23
• 9
• 33
• ### Forum Statistics

• Total Topics
632633
• Total Posts
3007543
• ### Who's Online (See full list)

There are no registered users currently online

×