2022-05-31 01:19:23 -04:00
|
|
|
# SPDX-FileCopyrightText: 2018-2022 Espressif Systems (Shanghai) CO LTD
|
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
2018-07-30 12:10:10 -04:00
|
|
|
#
|
|
|
|
|
|
|
|
# Convenience functions for commonly used data type conversions
|
2020-11-10 22:31:17 -05:00
|
|
|
import binascii
|
2021-01-25 21:49:01 -05:00
|
|
|
|
2020-11-10 22:31:17 -05:00
|
|
|
from future.utils import tobytes
|
2018-07-30 12:10:10 -04:00
|
|
|
|
2018-12-04 07:46:48 -05:00
|
|
|
|
2018-07-30 12:10:10 -04:00
|
|
|
def str_to_hexstr(string):
|
|
|
|
# Form hexstr by appending ASCII codes (in hex) corresponding to
|
|
|
|
# each character in the input string
|
2022-05-31 01:19:23 -04:00
|
|
|
return binascii.hexlify(tobytes(string)).decode('latin-1')
|
2018-07-30 12:10:10 -04:00
|
|
|
|
2018-12-04 07:46:48 -05:00
|
|
|
|
2018-07-30 12:10:10 -04:00
|
|
|
def hexstr_to_str(hexstr):
|
|
|
|
# Prepend 0 (if needed) to make the hexstr length an even number
|
2018-12-04 07:46:48 -05:00
|
|
|
if len(hexstr) % 2 == 1:
|
2018-07-30 12:10:10 -04:00
|
|
|
hexstr = '0' + hexstr
|
|
|
|
# Interpret consecutive pairs of hex characters as 8 bit ASCII codes
|
|
|
|
# and append characters corresponding to each code to form the string
|
2022-05-31 01:19:23 -04:00
|
|
|
return binascii.unhexlify(tobytes(hexstr)).decode('latin-1')
|