esp-idf/tools/esp_prov/utils/convenience.py

24 lines
798 B
Python
Raw Normal View History

# SPDX-FileCopyrightText: 2018-2022 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: Apache-2.0
#
# Convenience functions for commonly used data type conversions
2020-11-10 22:31:17 -05:00
import binascii
2020-11-10 22:31:17 -05:00
from future.utils import tobytes
2018-12-04 07:46:48 -05:00
def str_to_hexstr(string):
# Form hexstr by appending ASCII codes (in hex) corresponding to
# each character in the input string
return binascii.hexlify(tobytes(string)).decode('latin-1')
2018-12-04 07:46:48 -05: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:
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
return binascii.unhexlify(tobytes(hexstr)).decode('latin-1')