Shop OBEX P1 Docs P2 Docs Learn Events
Convert letters to hex — Parallax Forums

Convert letters to hex

marclamarcla Posts: 19
edited 2008-02-11 23:47 in Propeller 1
Hello!

I have a question about convert letters to hex.
example.

string text contains "AAABBB"
and then i wont string hextext to contain "414141424242"
41 for A (ascii in hex)
42 for B (ascii in hex)

How do i do that?

Martin

Comments

  • Fred HawkinsFred Hawkins Posts: 997
    edited 2008-02-11 12:35
    ?
    It's already in that form. Just look at the memory dump.
  • AleAle Posts: 2,363
    edited 2008-02-11 18:41
    To get another string...
    So,

    You have to get every byte and convert it to the appropriate two digit equivalent, but you knew that.
    In Spin, I do not know, there is no String handling... but... if you use a pointer to a byte array, that will/may work:

    mystr : is your string...
    myhex: is your hex result
    i := 0
    repeat i < mystrlength
      a := byte[noparse][[/noparse]mystr + i]
      if ((a >> 4) > 9) byte[noparse][[/noparse]myhex + i*2] := 55 + (a >> 4)
      else byte[noparse][[/noparse]myhex + 2*i] := 48 + (a>>4)
      if ((a & 15) > 9) byte[noparse][[/noparse]myhex + i*2] := 55 + (a & 15)
      else byte[noparse][[/noparse]myhex + 2*i] := 48 + (a & 15)
    
    <<terminate string somehow>>
    
    



    Other better and more optimized versions could exist burger.gif
  • Paul BakerPaul Baker Posts: 6,351
    edited 2008-02-11 20:03
    Here is my set_preferences method for my digital storage scope. I have included the entire routine and·highlighted the code pertainent·for your purposes: (NMSK = $0F)

    PRI Set_Preferences | field, position, keyok, k, in_value, process_char, change_position, vptr, form_exit, new_field, wmask, wval, wtime {Trigger Mask&Value, Edge Mask, Slow Delta}
      Display.out(cls)
      color(white)
      wmask := tmask                                        'copy global variables into
      wval  := tval                                         'local variables
      wtime := sval
      Display.hex(tmask,8)                                  'display the current values
      Display.str(string(" Mask"))
      gotoxy(0,1)
      Display.hex(tval,8)
      Display.str(string(" Value"))
      gotoxy(0,2)
      Display.hex(sval,8)
      Display.str(string(" Time"))
      gotoxy(0,FrameBottom - 2)
      Display.str(string("Press <Enter> to accept"))
      gotoxy(6,FrameBottom - 1)
      Display.str(string("<Esc>   to cancel"))
      gotoxy(0,FrameBottom)
      color(highlight)
      Display.str(string("Trigger pin mask, 00000000=off  "))'display first help line
      display_nibble(0,0,tmask>>28)                         'set current nibble to inverse
      color(white)
      position := 0                                         'initialize field and position being edited
      field := 0
    [color=red]  repeat
        repeat
          keyok := false                                    'clear working flags
          process_char := false
          change_position := false
          form_exit := false
          k := key.key                                      'get a keystroke
          case k
            Tab, UpArrow, DownArrow, LeftArrow, RightArrow: 'position changing keystrokes
              change_position := true
              keyok := true
            Enter, Esc:                                     'leave form keystrokes
              form_exit := true
              keyok := true
            "0".."9":
              in_value := k-"0"                             'hexadecimal charactor
              process_char := true
              keyok := true
            "A".."F":
              in_value := k-"A"+ $A
              process_char := true
              keyok := true
            "a".."f":
              in_value := k-"a"+ $A
              process_char := true
              keyok := true
    [/color]
        until keyok
    [color=black]    if(process_char)
          'record char
          vptr := lookupz(field: @wmask, @wval, @wtime)       'retrieve pointer to variable
    [/color][color=red]      if(position//2 == 0)                                'change high nibble
            BYTE[noparse][[/noparse]vptr][noparse][[/noparse]3-position/2] &= NMSK                  'clear high nibble
            BYTE[noparse][[/noparse]vptr][noparse][[/noparse]3-position/2] |= in_value << 4         'set high nibble value
          else                                                'change high nibble
            BYTE[noparse][[/noparse]vptr][noparse][[/noparse]3-position/2] &= !NMSK                 'clear low nibble
            BYTE[noparse][[/noparse]vptr][noparse][[/noparse]3-position/2] |= in_value              'set low nibble value
    [/color]      'display char in normal
          color(white)
          display_nibble(position,field,in_value)             'display the new nibble value
          'advance position, boundry check/change field
          if(++position > 7)
            position := 0
            if(++field > 2)
              field := 0
          'highlight new char
          vptr := lookupz(field: @wmask, @wval, @wtime)       'retrieve pointer to variable
          if(position//2 == 0)                                'read high nibble
            in_value := BYTE[noparse][[/noparse]vptr][noparse][[/noparse]3-position/2] >> 4
          else                                                'read low nibble
            in_value := BYTE[noparse][[/noparse]vptr][noparse][[/noparse]3-position/2] & NMSK
          color(highlight)
          display_nibble(position,field,in_value)             'highlight next nibble
          'if new field, display in context help line
          if position == 0
            gotoxy(0,FrameBottom)
            case field
              0:
                Display.str(string("Trigger mask, $00000000=off     "))
              1:
                Display.str(string("Trigger value for bits in mask  "))
              2:
                Display.str(string("Clocks/sample for slow aquire   "))
        new_field := false                                     'reset new field flag
        if(change_position)
          color(white)
          vptr := lookupz(field: @wmask, @wval, @wtime)        'retrieve pointer to variable
          if(position//2 == 0)                                 'read high nibble
            in_value := (BYTE[noparse][[/noparse]vptr][noparse][[/noparse]3-position/2] & !NMSK) >> 4
          else                                                 'read low nibble
            in_value := BYTE[noparse][[/noparse]vptr][noparse][[/noparse]3-position/2] & NMSK
          display_nibble(position,field,in_value)              'set nibble in normal text
          case k
            Tab:
              ++field                                          'increment feild
              field //= 3                                      'wrap around
              position:=0                                      'set position to zero
              new_field := true                                'indicate new field reached
            UpArrow:
              --field                                          'decrement feild being edited
              field #>= 0                                      'boundry limit
              new_field := true                                'indicate new field reached
            DownArrow:
              ++field                                          'increment feild being edited
              field <#= 2                                      'boundry limit
              new_field := true                                'indicate new field reached
            LeftArrow:
              --position                                       'decrement position being edited
              position #>= 0                                   'boundry limit
            RightArrow:
              ++position                                       'increment position being edited
              position <#= 7                                   'boundry limit
          color(highlight)
          if new_field                                         'if new field get new variable
            vptr := lookupz(field: @wmask, @wval, @wtime)
          if(position//2 == 0)                                 'retrieve high nibble
            in_value := (BYTE[noparse][[/noparse]vptr][noparse][[/noparse]3-position/2] & !NMSK) >> 4
          else                                                 'retrieve low nibble
            in_value := BYTE[noparse][[/noparse]vptr][noparse][[/noparse]3-position/2] & NMSK
          display_nibble(position,field,in_value)              'highlight new nibble
          'if new field, display in context help line
          if new_field
            gotoxy(0,FrameBottom)
            case field
              0:
                Display.str(string("Trigger pin mask, 00000000=off  "))
              1:
                Display.str(string("Trigger value for bits in mask  "))
              2:
                Display.str(string("Clocks/sample for slow aquire   "))
                                                                                                                               
        if(form_exit)                                       'exit the form
          case k
            Enter:                                          'if enter 
              tmask := wmask                                'update global variables
              tval  := wval & wmask                         'use only bits same as mask
              sval  := wtime #> $12                         'ensure waitcnt miss wont occur
          return
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • BEEPBEEP Posts: 58
    edited 2008-02-11 23:47
    Nexa/proove ?
Sign In or Register to comment.