Splitting HEX into multiple bytes?

Hi All,

I am talking to a microcontroller via a serial port. It expects byte sized values sent to it in hex, so to send e.g. 171 i send “AB”. However some values are larger than what fits a byte so it wants 2 bytes, an “MSB” and an “LSB”. So if i were to send 43981 which is ABCD, I have to split it into AB and CD and send them separately in decimal ( 171(AB) and 205(CD) ).

Text handling seems to work but is there any more elegant way built in?

Cheers!

“byte size” infers an 8 bit value… “AB” is a TWO character string…
an 8 bit value (UINT8) has a numeric value…

dim x as UINT8
x=&HAB
x=171

are IDENTICAL in what they produce

So… do you need to send 8 bit values? or do you need to send two charcter STRING values that represent a HEX character

dim s as string
dim x as integer
x=43981
s=hex(x)
if floor(len(s)/2)<>ceil(len(s)/2) then s="0"+s // must be an even # of characters
for i=1 to len(s) step 2
transmit val("&H"+mid(s,i,2))
next i

@Dave S thanks,

I have to send each 8 bit value (with MemoryBlock.Int8Value())

so achieve that I do hex(43981) which becomes a string ABCD, split it into AB and CD, add &H, and then send val(&hAB) and val(&hCD) seems pretty much like your code here does. So this is the way to do it?

Why not just send the memory block directly ?

At least skip the step where you turn it into a string
Just send the Int8 values from the memoryblock one after the other

[quote=265074:@Norman Palardy]Why not just send the memory block directly ?

At least skip the step where you turn it into a string
Just send the Int8 values from the memoryblock one after the other[/quote]

a “STRING” that represents a hex value would still need to be tranformed into the bytes it represents
the OP does not want to sent “A”,“B”,“C”,“D” but 0xAB and 0xCD

@Norman Palardy the MCU wants the bytes in particular order when I write the memoryblock to serial.

So to send 65000 (FDE8) I have to fill the MB something like this:

tMB.Int8Value(0) = &h55 ’ Start of frame
tMB.Int8Value(1) = &hFD ’ MSB
tMB.Int8Value(2) = &hE8 ’ LSB
tMB.Int8Value(3) = Whatever other value is supposed to be here
etc.

No Dave
Just try this code

  dim mb as new memoryblock(2)
  mb.LittleEndian = false
  mb.Int16Value(0) = 43981
  
  break

mb now hold the 2 bytes represented by &hAB &hCD

So just send the mb as is instead of turning it back into a string then sending the VAL of each byte which is a contorted way to just send the MB bytes

Sure - either little endian or big endian
Stuff the bytes into the MB the just write the MB to the serial port

Ah… I was working under the premise that the OP HAD a string, even though my sample assumed otherwise, but that was to illustrate the conversion

The issue is that the MCU wants multiple values, some take one byte and some have to be split to an MSB and LSB. There’s actually like 30 bytes in the memoryblock that have all to be written at once otherwise MCU ignores.

So
mb.Int8Value(1) = &hAB ’ MSB
mb.Int8Value(2) = &hCD ’ LSB

isn’t the same as

mb.Int16Value(0) = 43981

is it?

Basically each byte must be in a specific position in the memoryblock when it’s sent…

can be if you look at my code

so If i use

dim mb as new memoryblock(2)
mb.LittleEndian = false
mb.Int16Value(0) = 43981

then mb.Int8Value(0) will be equivalent to &hAB and mb.Int8Value(1) will be &hCD?

did you run the code I posted & look in the debugger - you can look into the values IN the memoryblock as you debug

I got it, it effectively does the job of splitting the 16 bit value to two 8 bit ones.

mb.LittleEndian = false
mb.Int16Value(0) = 43981

mb.Byte(0).ToHex is AB
mb.Byte(1).ToHex is CD

Much cooler than parsing text. Thanks to all!