Utility functions

check_expected_data(data, expected_data)

Returns True if the data and expected_data are the same.

Parameters:
  • data (str) – a hexadecimal string
  • expected_data (str) – a hexadecimal string or a string with a list of hexadecimal string with , as separator (“9000, 6Cxx, 6xxx”)
Returns:

bool : True if data matches with the expected data, False otherwize

Note

The character X or x could be set into expected data as a wildcard value

getBytes(data, byteNumber, length=1)

Returns the part of data string from byte number to length bytes

Parameters:
  • data (str) – a hexadecimal string
  • byteNumber (int) – the start offset into the string
  • length (int) – The bytes length to get
Returns:

str bytestr: the hexadecimal string

# get the string from byte 2 with a length of 3
astr = "3B65000  09C11 0101 03"
getBytes(astr, 2, 3) # returns  "650000"
getLength(bytestr, length=1)

Returns a string representing the length of the string as parameter on numberOfBytes bytes

Parameters:
  • bytestr (str) – a hexadecimal string
  • length (int) – the number of byte expected for the string
getLength("A0A40002", 2) # returns "0004"
getLength("A0A40002", 1) # returns "04"
increment(bytestr, value)

This function increments an hexadecimal string with the integer value. The value could be an integer or a string.

Parameters:
  • bytestr (str) – a hexadecimal string
  • value (int) – the value to increment
aStr = '01'
aInt = 0x03
newStr = increment ( aStr, aInt ) # returns  "04"
aInt = '03'
newStr = increment ( aStr, aInt ) # returns  "04"
intToHexString(intValue, len=1)

Returns a hexadecimal string representing an integer

Parameters:
  • intValue (int) – an integer value
  • len (int) – the number of byte expected for the string
Returns:

str bytestr: the hexadecimal string

# get the string representation of the integer
aInt = 10
intToHexString ( aInt, 2 ) #returns  "000A"
lv(bytestring)

Returns a byte String representing the length content preceding by its length.

Parameters:bytestring (str) – a hexadecimal string
Returns:str bytestr: the hexadecimal string preceded by its length

Note

for byte string up to FF bytes, the length is coded with 2 bytes.

# get the string preceded by its length
astr = "3B65000  09C11 0101 03"
lv(astr) # returns  "093B6500009C11010103"
ber_lv(bytestring)

Returns a byte String representing the BER type length content preceding by its length.

Parameters:bytestring (str) – a hexadecimal string
Returns:str bytestr: the hexadecimal string preceded by its length

Note

short form consist of a single byte in which bit 8 is 0. (length: range is 0 ~ 127) long form. Initial octet, bit 8 is 1, and bits 1-7 encode the number of octets that follow.

# get the string preceded by its length
astr = "3B65000  09C11 0101 03"
ber_lv(astr) # returns  "093B6500009C11010103"

# the length of astr is longer than 127. Assume that length is 0x80
astr = "3B65000  09C11 0101 03..... 00"
ber_lv(astr) # returns  "81803B6500009C11010103....00"
der_lv(bytestring)

Returns a byte String representing the length content preceding by its length.

Parameters:bytestring (str) – a hexadecimal string
Returns:str bytestr: the hexadecimal string preceded by its length

Note

short form consist of a single byte (length: range is 0 ~ 127) long from consist of three octec, start with 0xFF(length: rnage is 0x0100 ~ 0xFFFF)

# get the string preceded by its length
astr = "3B65000  09C11 0101 03"
der_lv(astr) # returns  "093B6500009C11010103"

# the length of astr is longer than 255. Assume that length is 0x0100
astr = "3B65000  09C11 0101 03..... 00"
der_lv(astr) # returns  "FF01003B6500009C11010103....00"
remove_space(bytestring)

Removes all whitespace characters of a string.

Parameters:bytestring (str) – The string to manage
Returns:str bytestr: the string without whitepace characters
toByteArray(byteString)

Returns a list of bytes from a byte string

Parameters:bytestring (str) – a hexadecimal string
Returns:list list_byte: the list of bytes
# get the list of bytes from the hexadecimal string
astr = "3B65000  09C11 0101 03"
toByteArray( astr ) # returns  [ 0x3B, 0x65, 0x00, 0x00, 0x9C, 0x11, 0x01, 0x01, 0x03 ]
toHexString(bytes=[])

Returns a hexadecimal string from a list of bytes

Parameters:bytes (list) – a list of bytes
Returns:str bytestr: the hexadecimal string
# get the string from the list of bytes
a_list = [ 0x3B, 0x65, 0x00, 0x00, 0x9C, 0x11, 0x01, 0x01, 0x03 ]
toHexString( a_list ) # returns  "3B6500009C11010103"