Shop OBEX P1 Docs P2 Docs Learn Events
Trying to figure out how to convert these hex bytes into an ASCII string — Parallax Forums

Trying to figure out how to convert these hex bytes into an ASCII string

Don MDon M Posts: 1,652
edited 2014-04-28 13:03 in Propeller 1
I have 2 byte variables called Temp1 & Temp2
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

  • T ChapT Chap Posts: 4,223
    edited 2014-04-26 18:07
    One idea is to use CASE
    PUB getAscii(val)
            Case Val
                  $30 : 'display the ascii value 
                  $31 : 'display the ascii value
    
    etc
  • r.daneelr.daneel Posts: 96
    edited 2014-04-26 20:31
    Since you want to use lookup or lookdown, how about:
    repeat i from 0 to 5
      left := $30 + offset(byte[@temp1[i]] >> 4 & $f)
      right := $30 + offset(byte[@temp1[i]] & $f)
    
    ...
    ...
    
    
    PRI offset(index)
      return lookupz(index: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 17, 18, 19, 20, 21, 22)
    

    You could add the $30 to the values in the lookup table if you wanted.
  • kwinnkwinn Posts: 8,697
    edited 2014-04-26 22:12
    For the ls nibble you and it with hexF and add $30
    For the ms nibble shift right 4 and add $30
  • r.daneelr.daneel Posts: 96
    edited 2014-04-26 22:27
    kwinn wrote: »
    For the ls nibble you and it with hexF and add $30
    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".
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2014-04-26 22:50
    Don,

    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
  • kwinnkwinn Posts: 8,697
    edited 2014-04-26 22:52
    r.daneel wrote: »
    That doesn't work for $a-$f. $30+$a = $3a which is the ascii ":" character, not "A".

    You're right.
    Should have been add $30
    Compare to see if the result was greater than $39 and add $8 if so.
  • Don MDon M Posts: 1,652
    edited 2014-04-27 04:22
    Thanks everyone. Got it sorted out.
      debug.str(string("String: ")) 
      repeat i from 0 to 5
        Temp2 [i*2] := lookupz((byte[@Temp1[i]] >> 4 & $f): $30, $31, $32, $33, $34, $35, $36, $37, $38, $39, $41, $42, $43, $44, $45, $46)
        Temp2 [(i*2)+1] := lookupz((byte[@Temp1[i]] & $f): $30, $31, $32, $33, $34, $35, $36, $37, $38, $39, $41, $42, $43, $44, $45, $46)
      Temp2[12] := 0
      debug.str(@Temp2)
      debug.tx(13)  
    
    
  • Don MDon M Posts: 1,652
    edited 2014-04-27 04:45
    This works too...
      debug.str(string("String: ")) 
      repeat i from 0 to 5
        Temp2 [i*2] := lookupz((byte[@Temp1[i]] >> 4 & $f): "0123456789ABCDEF")
        Temp2 [(i*2)+1] := lookupz((byte[@Temp1[i]] & $f): "0123456789ABCDEF")
      Temp2[12] := 0
      debug.str(@Temp2)
      debug.tx(13)
    
    
  • Mike GMike G Posts: 2,702
    edited 2014-04-27 06:27
    I must be missing something... Why create a secondary buffer to hold ASCII values when you can just convert the values on the fly? Less Smile to worry about.
    CON
      _clkmode = xtal1 + pll16x     
      _xinfreq = 5_000_000
    
      CR            = 13
    
    VAR
    
    DAT
      workspace byte   $0[16]
    
    
    OBJ
      pst             : "Parallax Serial Terminal"
    
    
    
    PUB Main | wizver, p1
    
      pst.Start(115_200) 
      pause(500)
    
      InsertTestValues
      PrintHexValues
    
    
    PUB PrintHexValues | i
      pst.str(string(CR, "Workspace = "))
      repeat i from 0 to 15
        pst.hex(workspace[i], 2)
      pst.char(CR)
    
    PUB InsertTestValues | seed, i
      seed := cnt
      repeat i from 0 to 15
        workspace[i] := ?seed & $FF
    
    PRI pause(Duration)  
      waitcnt(((clkfreq / 1_000 * Duration - 3932) #> 381) + cnt)
      return
    
  • Don MDon M Posts: 1,652
    edited 2014-04-27 06:32
    Mike G wrote: »
    I must be missing something... Why create a secondary buffer to hold ASCII values when you can just convert the values on the fly? Less Smile to worry about.
    CON
      _clkmode = xtal1 + pll16x     
      _xinfreq = 5_000_000
    
      CR            = 13
    
    VAR
    
    DAT
      workspace byte   $0[16]
    
    
    OBJ
      pst             : "Parallax Serial Terminal"
    
    
    
    PUB Main | wizver, p1
    
      pst.Start(115_200) 
      pause(500)
    
      InsertTestValues
      PrintHexValues
    
    
    PUB PrintHexValues | i
      pst.str(string(CR, "Workspace = "))
      repeat i from 0 to 15
        pst.hex(workspace[i], 2)
      pst.char(CR)
    
    PUB InsertTestValues | seed, i
      seed := cnt
      repeat i from 0 to 15
        workspace[i] := ?seed & $FF
    
    PRI pause(Duration)  
      waitcnt(((clkfreq / 1_000 * Duration - 3932) #> 381) + cnt)
      return
    

    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.
  • Mike GMike G Posts: 2,702
    edited 2014-04-27 13:02
    Huh? So the original question is now to use the lookup and lookdown command...
  • r.daneelr.daneel Posts: 96
    edited 2014-04-27 16:37
    Well, he did say:
    Don M wrote: »
    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".

    So I assumed he wanted to work with the bytes in Temp2, not just display them.
  • infoinfo Posts: 31
    edited 2014-04-27 20:49
    Can you do
    if nibble < $0A, add $30
    if nibble > $09 add $37
    ?
  • infoinfo Posts: 31
    edited 2014-04-27 21:12
    BTW, you can also check how other programs do convertions. There are several programs on obex to convert number base, string, etc.

    I used serial port driver from obex to display Dec, Hex and ASCII for i2c eeprom hex dump.
  • Mike GMike G Posts: 2,702
    edited 2014-04-28 03:36
    So I assumed he wanted to work with the bytes in Temp2, not just display them.
    Right, and my suggestion was intended to help Don create reusable and clean code without dependencies. Why work with Temp2 when you already have a the working values in Temp1?

    Anyway, I guess I'm becoming a curmudgeon.
  • r.daneelr.daneel Posts: 96
    edited 2014-04-28 13:03
    Mike G wrote: »
    Right, and my suggestion was intended to help Don create reusable and clean code without dependencies. Why work with Temp2 when you already have a the working values in Temp1?

    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...
Sign In or Register to comment.