Jump to content
• Advertisement

Public Group

Converting an inputted hex to decimal

This topic is 4474 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

Say I have an int: int i = 0x122; How can I convert it so that i = 122?

Share this post

Share on other sites
Advertisement
You're going to have to be a lot more specific. There's no reason to convert from 0x122 to 122, unless one or both of them is a string. Is that the case?

CM

Share this post

Share on other sites
First of all, Hex 0x122 != decimal 122, it in fact == decimal 290, and if you are trying to get a decimal value from a hex value, from a string to a integer, then personally I prefer to use lookup tables for each value [last time I did URL decoders, thats how I did it anyway] and just multiply the current value by 16 before adding the result of the next value to it. so you get something like

(((((1) * 16) + 2)* 16) + 2) = 290 = (1 * (16^2)) + (2 * (16^1)) + (2 * (16^0))
for 0x122

Share this post

Share on other sites
say if you did want to convert 0x122 to just 122, you could do this:

(in C)
char text[256];int num = 0x122;int newnum;sprintf(text,"%x",num);newnum = atof(text);

I think thats how you'd do it.

Share this post

Share on other sites
Sounds like you want to do BCD conversion.

Just use a BCD conversion function, like so:

Decimal = (BCD >> 12) * 1000 + (BCD >> 8 & 0xf) * 100 + (BCD >> 4 & 0xf) * 10 + (BCD & 0xf);

That will work for 16-bit values, you can of course turn it into a loop for arbitrary-sized numbers. Of course, if your hex values have invalid BCD numbers in them (ie, digits A-F), then it will give you nonsense data out. GIGO! :)

Going the other way is a little more expensive, as you have to use division/modulus operations to sort out the decimal digits.

Share this post

Share on other sites
Quote:
 Original post by TalanSounds like you want to do BCD conversion.Just use a BCD conversion function, like so: Decimal = (BCD >> 12) * 1000 + (BCD >> 8 & 0xf) * 100 + (BCD >> 4 & 0xf) * 10 + (BCD & 0xf);That will work for 16-bit values, you can of course turn it into a loop for arbitrary-sized numbers. Of course, if your hex values have invalid BCD numbers in them (ie, digits A-F), then it will give you nonsense data out. GIGO! :)Going the other way is a little more expensive, as you have to use division/modulus operations to sort out the decimal digits.

Thanks, that's what I was looking for.

Share this post

Share on other sites
just wondering whats the most practical use for BCD's in game programming?

Share this post

Share on other sites

• Advertisement

Announcements

• Advertisement
• Popular Tags

• Advertisement

• 34
• 12
• 10
• 9
• 9
• Forum Statistics

• Total Topics
631354
• Total Posts
2999505
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!