Jump to content
  • Advertisement

Archived

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

BeatOne

from decimal to hex, the journey?

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

Hi folks! I was just wondering, I have always been given the code for decimal/octal/binary to hex converters, never coded them myself. How do I do? I have no clue.. I have serached google, no results... Is it some kind of formula or something? Object Pascal (delphi) and C++ are the languages I use.. Thanks!

Share this post


Link to post
Share on other sites
Advertisement
the algo is really easy, just think a bit about it:
- let num be the dec number you want to convert.
- store num % 16 as the last cipher in the hex number
- num = num / 16
- repeat

it's pretty straightforward, just run it trough on paper and you'll see
as you see if you want to convert to another base just replace the 16 in the algo with it.

if you want to see code to convert to/from/between arbitrary bases look here

hope that helps

Edit: just to make that clearer, in the second cycle of the loop num % 16 is stored as the second-to last number and so on, and you use A for 10, B for 11 and so on, but i guess you know that
also, the algo finishes once num is smaller then 16, then num itself is the first cipher of the hex number


My Homepage

[edited by - burning_ice on April 19, 2003 7:38:28 PM]

Share this post


Link to post
Share on other sites
Here''s some pseudo code:

num_str is the string with the number you want to convert

base is an integer that says what base num_str is in (binary=2, octal=8, decimal=10)

len is the length of num_str

also for the sake of simplicity, lets have num_str be in order from least-significant to most-significant, so if num_str = "12345", then num_str[0] is ''5'' and num_str[4] is ''1'' (this is the reverse of a normal string)

so,

  
int answer = 0;
for (n = 0; n < len; n++)
int this_digit = convertToInteger(num_str[n])
answer = answer + this_digit * (base to the power of n)
end for


then you have ''answer'' which contains the value of the input string, so then if you want to convert it to a string with a different base, do this:

again, base is an integer (since you want to convert to hex, use 16)


  
int current_char = 0
string new_string = ""

while (answer != 0)
int this_digit = answer mod base
char this_char = convertToCharacter(this_digit)
new_string[current_char] = this_char
answer = answer / base
current_char++
end


which will create a string in the same ''reverse'' order as before.

have fun

Share this post


Link to post
Share on other sites
quote:
Original post by Mathematix
Search google for Euclid's Algorithm.


umm..isn't that one for finding the gcd of two numbers?
Edit: i think the one for converting bases is called horner's algorithm


My Homepage

[edited by - burning_ice on April 19, 2003 7:47:55 PM]

Share this post


Link to post
Share on other sites
Burning_Ice,

Yep, you''re right. But modifying it slightly yields a method for converting a decimal into any of the bases required. A very good universal method. If required, I shall explain in a later post.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!