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

24 wiersze
798 B
Python
Czysty Zwykły widok Historia

# 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-11 03:31:17 +00:00
import binascii
2020-11-11 03:31:17 +00:00
from future.utils import tobytes
2018-12-04 12:46:48 +00: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 12:46:48 +00:00
def hexstr_to_str(hexstr):
# Prepend 0 (if needed) to make the hexstr length an even number
2018-12-04 12:46:48 +00: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')