hex convertion
dijan
Posts: 2
i need to convert a·3 byte hexadecimal number into the corresponding decimal value. As i just have to print this number (in decimal), the result (that is larger than a word) may be stored as separate ascii-bytes in memory.
e.g. "FC 15 DC" (=16520668 decimal) should become in memory:31h 36h 35h 32h 30h 36h 36h 38h·Can somebody help me how to do it? or even better: has someone a piece of code to do so?
e.g. "FC 15 DC" (=16520668 decimal) should become in memory:31h 36h 35h 32h 30h 36h 36h 38h·Can somebody help me how to do it? or even better: has someone a piece of code to do so?
Comments
Set up a loop to add 6-5-5-3-6 to those 5 bytes as many times as the third bytes value
For example 01 FF FF
65535
+65536
131071
Each column added separately carrying the overflow to the next byte, (long hand addition) (store the overflow in a separate byte variable)
So in the first run through the loop you add 5+6=11, carry the 1 over and store 1 in the memory location
Then you add 3+3+1 and store 9 in next byte to the left
column3 from the right is 5+5 so you store 0 and carry the 1 again
Same thing as longhand addition only the computer logic is doing it.
Final result will be the 8 byte array has the real number values for each place, then add 48 to each location to convert to ascii
Done.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Think Inside the box first and if that doesn't work..
Re-arrange what's inside the box then...
Think outside the BOX!
When I began my higher education career, I used to teach the stuff you just wrote about. Today, I am a college dean and the CIO and about to retire.
I could not have done better. On my best day, I might have done as well.
--Bill
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
You are what you write.
' {$STAMP BS2}
' {$PBASIC 2.5}
'four words hold the Hex number
outstring VAR Byte(10)
inHex VAR Word(4)
idx VAR Byte
borrow VAR Word
Main:
DEBUG HOME, "$FC15DC converted to ASCII",CR
'breakup number into bytes· - example $FC15DC
inHex(0)=$00:inHex(1)=$FC:inHex(2)=$15:inhex(3)=$DC ' Load them as byte into scratch words
'the magic happens here
FOR idx=9 TO 0
· borrow = inHex(0)//10 * 256
· inHex(0) = inHex(0) / 10
· inHex(1) = inHex(1) + borrow
· borrow = inHex(1)//10 * 256
· inHex(1) = inHex(1) / 10
· inHex(2) = inHex(2) + borrow
· borrow = inHex(2)//10 * 256
· inHex(2) = inHex(2) / 10
· inHex(3) = inHex(3) + borrow
· outstring(idx) = inHex(3)//10 + 48 'add ASCII Zeros
· inHex(3) = inHex(3) / 10
NEXT
DEBUG· STR outstring\10,CR
END
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Have Fun
TR
Post Edited (TechnoRobbo) : 11/29/2006 4:41:32 AM GMT
1)··A technical question.
2)· An amazing mathematical answer.
3)· A compliment from someone who understands the question and answer.
4)· Someone to write the code to solve the problem.
If I owned a company I'd hire all 4 of you to do something.
Post Edited (terahertz) : 11/29/2006 4:39:23 AM GMT
My worst nightmare is to memorize the winning ticket then not be able to find it, where the heck did I put that 20 million dollar winning ticket anyway...
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Think Inside the box first and if that doesn't work..
Re-arrange what's inside the box then...
Think outside the BOX!
Will you be offering a Dental plan? 'cause it's all about the benies.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Have Fun
TR
Dijan