Shop OBEX P1 Docs P2 Docs Learn Events
I am having trouble understanding left shift and bytes — Parallax Forums

I am having trouble understanding left shift and bytes

Don MDon M Posts: 1,652
edited 2014-09-22 22:00 in Propeller 1
Let's say I have a byte buffer called buffer [10].

And in that buffer is this: A0000DBB7E

I can display this buffer like this:
repeat i from 0 to 9
  serial.tx(buffer[i])
and it shows exactly as expected: A0000DBB7E

So I am trying to assemble the first 2 characters (A0) into another byte buffer so I tried this: newbyte := (byte[@buffer[0]] << 4) | (byte[@buffer[1]]).

I then tried to display it like this: serial.tx(newbyte) and I expected to see A0 but only see 0.

What am I not understanding correctly here?

With the Windows calculator I enter A LSH 4 = and I get A0. I then press the OR button and enter 0 then press =. I get A0

Thanks.
Don

Comments

  • tonyp12tonyp12 Posts: 1,951
    edited 2014-09-22 20:37
    So the buffer is the ascii values of the letters "A" and "0"......?

    A real byte is a pair of hex characters. e.g 0x00 to 0xFF

    newbyte := (buffer[0]-48) << 4) + (buffer[1]-48)
    would be close, but no cigar as there is a gap in hex chars from 58 to 64.

    a=buffer[0]-48
    if a>9 then a=a-7
    a=a<<4

    b=buffer[1]-48
    if b>9 then b=b-7
    a=a+b
  • Don MDon M Posts: 1,652
    edited 2014-09-22 20:40
    tonyp12 wrote: »
    Looks more like Nibbles, a real byte is a pair of hex characters. e.g 0x00 to 0xFF

    Is the buffer ascii values of the letters "A" and "0"......?

    Yes I think it is the ascii values of each of the 10 characters. So what do I need to do?
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-09-22 21:39
    Don M wrote: »
    Yes I think it is the ascii values of each of the 10 characters. So what do I need to do?

    The method "StrToBase" in the object "Parallax Serial Terminal.spin" will convert a zero terminated string to a number. You'll need to break your string apart since the number will likely exceed 32-bits.
  • JonnyMacJonnyMac Posts: 9,108
    edited 2014-09-22 22:00
    I try to write methods that can be re-used -- you might consider something like this; you pass a pointer to your ASCII buffer and a character count (1..8), the method converts the string to a value.
    var
    
      byte  tag[5]
    
    
    dat
    
      RawBuf      byte      "A0000DBB7E"
    
    
    pub main | idx
    
      setup
    
      term.rx
      term.tx(CLS)
    
      repeat idx from 0 to 4
        tag[idx] := hexstr2val(@RawBuf+(idx<<1), 2)
    
      repeat idx from 0 to 4
        term.hex(tag[idx], 2)
        term.tx(" ")
    
    
    pub hexstr2val(p_str, n) | val, c
    
    '' Convert hex string to decimal value
    
      val := 0
      
      repeat (1 #> n <# 8)
        c := byte[p_str++]
        case c                            
          "0".."9":
            val := (val << 4) + (c - "0")
                          
          "a".."f":
            val := (val << 4) + (c - "a") + 10    
          
          "A".."F": 
            val := (val << 4) + (c - "A") + 10     
    
          other:
            quit
    
      return val
    
  • Don MDon M Posts: 1,652
    edited 2014-09-22 22:00
    Ok I think I have it working now... here's what I put together thanks to your help.
    i := 0
    repeat 5
        a := (byte[@buffer[i]] - 48)
        if a > 9
          a := a - 7
    
        b := (byte[@buffer[++i]] - 48)
        if b > 9
          b := b - 7
    
        c := ((a << 4) | b)
        chksum := chksum + c
        
        serial2.hex(chksum, 4)                         ' Watch checksum build
        serial2.tx(SP)
        i++
        
      serial2.tx(CR)
      
      repeat i from 1 to 10                               ' Display each of the 10 unique identification bytes             
        serial2.tx(RFIDdata[i])
    
      serial2.tx(SP)
      serial2.str(string("Checksum = "))
      serial2.hex((chksum & $FF), 2)  
    
    
Sign In or Register to comment.