Separating hex string into 3 separate octets

I am inputting a number via text box and converting to a hex string. I need to separate that hex string into 3 different octets. For example the number 200632 is 30FB8, what I need to do is have the hex value be
3 0F B8.

I am imputing the number as follows,

Var V as Int32 // number to be imported
Var H as String // Hex number
Var W as String // lower hex value
Var X as String // middle hex value
Var X as String // upper hex value
Var H as String // Hex number

V = Dec_Number.Text.ToInteger // this snippet works to display converted int to hex.in a message box.
H = V.ToHex(3)
MessageBox(H)

Use H.Length Mod 2 to determine if the length is even or odd.
Grab the first one or two characters off the front of the string.
Then walk through the rest of the string using H.Middle.

Per your suggestion I added code to separate the HEX number. Couple of issues is when there is no value in the middle hex I get an incorrect set of hex values.
For example if I input
200632 I get 3 0F B8 which is correct.
5 results are 0 (correct) 05 (incorrect) 05 (correct)
5 results should be 0 00 05
1234 results are 4 (incorrect) D2 (incorrect) D2 (correct)
1234 results should be 0 04 D2
65536 I get 1 00 00 which is correct

I will always want to have three hex values even if it is zero.

Var V as Int32 // number to be imported
Var H as String // Hex number
Var W as String // lower hex value
Var X as String // middle hex value
Var Y as String // upper hex value

V = Dec_Number.Text.ToInteger
H = V.ToHex(3)
MessageBox(H)

W = H.RightBytes(2)
MessageBox(W)

X = H.Middle(1,2)
MessageBox(X)

Y = H.LeftBytes(1)
MessageBox(Y)

Tohex(5) or probably 6

working as expected,
thanks

1 Like

Well, a complete 32 bit value uses 4 bytes. The 4th will be always 0 ?

In case of the user preferring converting it to an array at once…


Var theval As Integer = &h11223344

Var m As New Memoryblock(4)
m.LittleEndian = True
m.Int32Value(0) = theVal // Set the value here

Var hexVal() As String = EncodeHex(m.StringValue(0,4),True).Split

Break  // hexVal(i) layout is RTL, that means hexVal(0) = "44", hexVal(3) = "11"

thanks

1 Like

At it again…

I am trying to take a number that is inputted in a desktop text field box and dividing that number into three hex numbers. Upper 4 bits, middle and LSB. For example the number 200632 is brought into the program and converted to hex (3 0F B8). This part is working. Once the number is separated, I need to add them together which equals 3+0F+B8=C8. Tried just adding the 3 hex numbers together and of course that does not work since hex numbers are strings. So then I am attempting to convert the hex to an integer so it can be summed but am having problems with the code working. Below is the code. When running the program the upper_4_bits hex to integer works but the ID_MSB and ID_LSB does not. I also was looking for a way to rotate the integer but have not found a way to do that in XOJO.
Any suggestions? Thanks.

var Start as Int16 = 84 // Equ of 54 hex Indicates we want to write to EEPROM
var Start_Hex As String
var Length as Int16 = 21 // Length of message 15hex
var Length_Hex as String

var Input_ID as Int64 // Full ID
var Input_ID_Hex as String

var ID_Upper_4Bits as Int16
var ID_Upper_4Bits_Hex as String
var ID_MSB as Int64
var ID_MSB_Hex as String
var ID_LSB as Int64
var ID_LSB_Hex as String

var ID as Int64 = 0 // Serial number Byte 1 (MSB)
var ID_Hex as String

Var TotalSum As Int64 // Used to add all values.
Var TotalSum_Hex As String

Var Sum_of_Id As Int64 // Used to add tag ID values

Var Inverted_Id As Int64 //Int16 // Used to invert value

Var Parity As String

Var Serial_Out_Box As String // Use to send data to serial port

ID = Input_ID_Field.Text.ToInteger
ID_Hex = ID.ToHex(5)

Unsigned sum discarding overflows?

Something seems off here 03+0F+C8 = CA (3+15+184=202)


Var num As Uint32 = &h030FB8  // 200632

num = num And &h0FFFFFF // cut the value 2 lower bytes and half higher

Var sum As UInt8 = (num And &h0FF)+((num And &h0FF00) \ &h0100)+(num \ &h010000)

break

var xyz as int32
V = Dec_Number.Text.ToInteger
H = V.ToHex(3)
MessageBox(H)
W = H.RightBytes(2)
MessageBox(W)
X = H.Middle(1,2)
MessageBox(X)
Y = H.LeftBytes(1)
MessageBox(Y)
xyz = int32(h+w+x+y)

Once you have your octets, use Integer.FromHex to get the integer values and add those.

Tried that instruction before and it did not work for some reason, but this time it did. But thanks all is working as expected.