PDA

View Full Version : hex convertion

dijan
11-28-2006, 05:35 PM
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?

metron9
11-29-2006, 04:03 AM
Convert the last two bytes and store them in last 5 memory locations of an 8 byte array as numbers 0 thru 9

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!

Bill Chennault
11-29-2006, 05:15 AM
metron9 (http://forums.parallax.com/member.php?u=41825)--

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.

TechnoRobbo
11-29-2006, 11:26 AM
The code below will generate leading zeros. You can easily·to change them to spaces with a do loop.

' {\$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)=\$D C ' 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

terahertz
11-29-2006, 11:32 AM
WOW· http://forums.parallax.com/images/smilies/cool.gif

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.

http://forums.parallax.com/images/smilies/cool.gif

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

metron9
11-29-2006, 02:09 PM
Thanks Bill, I have one daughter with one more year to go at the UofM (ouch on pocketbook) and she will be a music teacher. She understands calculus way above what I understand but I never got the chance to do much but work during what should have been my college years. One of her best friends just took the Mcats? I think they called it and is in the top 1% scores or something like that. My second daughter working on being a surgery tech pulled up one of those tests and said wow dad look at this how the heck can anyone answer these questions. Questions were chemical analysis of mixed elements with SO4 and weird looking formulas. Funny thing though she said lets give it a go, I took a hard look and answered all five questions correct, she was astounded and so was I, I had no idea how I got them right because I never win anything! (I don't even buy lottery tickets unless there is a pool at my company, then I have to buy in because if I don't I may lose all my employees if they win, LOL.

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!

TechnoRobbo
11-29-2006, 07:07 PM
Terahertz,

Will you be offering a Dental plan? 'cause it's all about the benies.

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Have Fun

TR

dijan
12-03-2006, 11:38 PM
Many thanks for the answers.It helped me a lot.

Dijan