I am having trouble understanding left shift and bytes
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:
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
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
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
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.
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 vali := 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)