Trying to figure out how to convert these hex bytes into an ASCII string
Don M
Posts: 1,652
I have 2 byte variables called Temp1 & Temp2
In Temp 1 I have these bytes- $00, $07, $80, $7F, $A8, $D3
I'm trying to figure out how to break apart the hex bytes to store the individual nibbles (?) as their ascii equivalent in Temp2
In Temp2 I want to have these bytes- $30, $30, $30, $37, $38, $30, $37, $46, $41, $38, $44, $33 so that I can work with it and display it as a string "0007807FA8D3".
I tried using this:
But it won't work with the letters A - F.
Is there a way to do this with a lookdown or lookup?
Thanks.
Don
var byte Temp1[6] byte Temp2[14]
In Temp 1 I have these bytes- $00, $07, $80, $7F, $A8, $D3
I'm trying to figure out how to break apart the hex bytes to store the individual nibbles (?) as their ascii equivalent in Temp2
In Temp2 I want to have these bytes- $30, $30, $30, $37, $38, $30, $37, $46, $41, $38, $44, $33 so that I can work with it and display it as a string "0007807FA8D3".
I tried using this:
Temp2[0] := ((byte[@Temp1][0] & $F) + $30) Temp2[1] := ((byte[@Temp1][0] >> 4) + $30) and so on... Temp2[12] := 0
But it won't work with the letters A - F.
Is there a way to do this with a lookdown or lookup?
Thanks.
Don
Comments
You could add the $30 to the values in the lookup table if you wanted.
For the ms nibble shift right 4 and add $30
That doesn't work for $a-$f. $30+$a = $3a which is the ascii ":" character, not "A".
Do you ever refer to the Propeller Manual before you come to the forum for answers? If you had, you would be familiar with the lookdownz command. Look it up!
-Phil
You're right.
Should have been add $30
Compare to see if the result was greater than $39 and add $8 if so.
Mike,
Actually I do convert on the fly. I just use this type of example for forum posting and in some test programs on a Quickstart.
So I assumed he wanted to work with the bytes in Temp2, not just display them.
if nibble < $0A, add $30
if nibble > $09 add $37
?
I used serial port driver from obex to display Dec, Hex and ASCII for i2c eeprom hex dump.
Anyway, I guess I'm becoming a curmudgeon.
Well, I can't say if you're becoming a curmudgeon, but what if Don wanted to use the converted bytes in some sort of loop millions of times, but retain the original values? Why wouldn't he convert once and put the result in a buffer like Temp2? Without knowing what he wants to do with the result I can't say if it would be better to convert on the fly or not...