math hex dec

Programming, for all ages and all languages.
Post Reply
User avatar
kmtdk
Member
Member
Posts: 263
Joined: Sat May 17, 2008 4:05 am
Location: Cyperspace, Denmark
Contact:

math hex dec

Post by kmtdk »

Hi

i have been thinking about the convertion beteween hex and dec, mostly from hex to dec.

i know that the normal way is to convert hex to bin and then to dec ( i dont know 100% how you do that)

but is there not any other solutions ( FPU, or another calculation way ???)



KMT dk
well, what to say, to much to do in too little space.
when it goes up hill, increase work, when it goes straight, test yourself but when going down, slow down.
DeletedAccount
Member
Member
Posts: 566
Joined: Tue Jun 20, 2006 9:17 am

Re: math hex dec

Post by DeletedAccount »

Hi,
Hex to bin :
Take each digit and convert to bin in groups of 4
eg 6A (hex) = 0110 1010 (bin)
bin to hex :
Take digits in groups of four and convert to hex
eg 1111 0101 (bin) = F5 (hex)
hex to dec :
Summation of For i = 0 to No digits-1 (i+1) th digit * 16^ i
eg FF (hex) = (15 )* 16^1 + ( 15) * 16 ^ 0 = 255(dec)
oct to bin :
similar to hex , but take in groups of 3
bin to oct :
similar to hex , but take in groups of 3
oct to dec :
Summation of For i = 0 to No digits-1 (i+1) th digit * 8^ i
55 (oct) = 5 * 8 ^ 1 + 5 * 8 ^ 0 = 45 (dec)

Hope you get the pattern :D . However do your reading first , any text book can teach this
Regards
Shrek
User avatar
kmtdk
Member
Member
Posts: 263
Joined: Sat May 17, 2008 4:05 am
Location: Cyperspace, Denmark
Contact:

Re: math hex dec

Post by kmtdk »

hehe
eh ..
minor mistake from my side ( DOH )

im talking about convering from hex, using hex ( os code)

of cause i have my "own" way, but it is not too good with big numbers ( and is generaly slow)

KMT dk
well, what to say, to much to do in too little space.
when it goes up hill, increase work, when it goes straight, test yourself but when going down, slow down.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: math hex dec

Post by Love4Boobies »

Does this make any sense to anyone?
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: math hex dec

Post by Troy Martin »

I think what the OP is trying to do is this:

Code: Select all

; AX = 0xF00D
mov ax,0xF00D
call hex_to_dechex_word

; DX:AX (or EAX) = 0x61453 (decimal for 0xF00D encoded in hexadecimal
call print_hex_long ; prints out DX:AX or EAX on the screen
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: math hex dec

Post by Love4Boobies »

If that's the case (although I really doubt that's what he meant) he could probably use a little trick like converting to decimals and storing them as BCDs and then just reading the bytes from memory. My trick however only works for x86 and architectures that support BCDs.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
User avatar
Firestryke31
Member
Member
Posts: 550
Joined: Sat Nov 29, 2008 1:07 pm
Location: Throw a dart at central Texas
Contact:

Re: math hex dec

Post by Firestryke31 »

Any architecture that can store and operate on at least one half byte of data supports BCDs. It's just faster if it supports it in hardware.
Owner of Fawkes Software.
Wierd Al wrote: You think your Commodore 64 is really neato,
What kind of chip you got in there, a Dorito?
User avatar
Troy Martin
Member
Member
Posts: 1686
Joined: Fri Apr 18, 2008 4:40 pm
Location: Langley, Vancouver, BC, Canada
Contact:

Re: math hex dec

Post by Troy Martin »

Any architecture that supports operations on a single byte of data and can bitwise shift a single byte of data around supports BCDs. Having a stack helps too.
Image
Image
Solar wrote:It keeps stunning me how friendly we - as a community - are towards people who start programming "their first OS" who don't even have a solid understanding of pointers, their compiler, or how a OS is structured.
I wish I could add more tex
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: math hex dec

Post by Love4Boobies »

Well sure. Any system supporting a keyboard and a little monkey that does additions can add numbers. It's still not a good idea to do it that way. x86 has special BCD instructions.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
Post Reply