Skip to content

Pack hex string with (), with(), ...

Jaume Olivé Petrus edited this page Oct 28, 2017 · 15 revisions

This block is a Reporter block that encodes a serie of specified values into a hexadecimal string.

A hexadecimal string is a representation of a serie of bytes in text format, in which every byte is converted to it's hexadecimal representation in text. For example, the integer value 127 are represented by the "7F" string.

The encoded string contain the information about the data type of each packed value and it's value, and must be decoded using the "Unpack hex string () to (), to (), ..." block.

The encoding mechanism

Assumptions:

  • Data is in little-endian format (least significant byte, is at the lowest address)
  • Integers have a length of 32 bits
  • Floats are represented in IEEE 754 single-precision format, and have a length of 32 bits
  • Booleans are represented in a byte (1 = true, 0 = false)
  • Strings are ended by the NULL char (0x00)

Data is packed in an hexadecimal's string representation, with this format:

  • Header

    • First byte: number of values packed in the string
    • N bytes with the information about the data type of each packed value. Each byte have information about 2 packed values, one for each nibble. Data types are coded in this way:
      • 0b0000: Number
      • 0b0001: Integer
      • 0b0010: Nil
      • 0b0011: Boolean
      • 0b0100: String
  • N bytes of data

Example:

03 13 20 01000000 01 04000000

03 is the number of packed values, in this case 3
13 is the type of the first and second packed values: integer and boolean
21 is the type of the third and fourth packed values: nil and integer

01000000: 1 (integer)
01      : true (boolean)
04000000: 4 (integer)

Observe that the nil value is only present in the header and has not
representation in the data.

Shape

Example

In this example the values 10.2 and 25.3 are packed and then unpacked.

Run this example in The Whitecat IDE.

Clone this wiki locally