I am having trouble understanding left shift and bytes
Don M
Posts: 1,652
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.