2022-05-31 05:19:23 +00:00
|
|
|
# SPDX-FileCopyrightText: 2018-2022 Espressif Systems (Shanghai) CO LTD
|
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
2018-07-30 16:10:10 +00:00
|
|
|
#
|
|
|
|
|
|
|
|
# Convenience functions for commonly used data type conversions
|
2021-01-26 02:49:01 +00:00
|
|
|
|
2022-06-22 09:44:19 +00:00
|
|
|
def bytes_to_long(s: bytes) -> int:
|
|
|
|
return int.from_bytes(s, 'big')
|
2018-07-30 16:10:10 +00:00
|
|
|
|
2018-12-04 12:46:48 +00:00
|
|
|
|
2022-06-22 09:44:19 +00:00
|
|
|
def long_to_bytes(n: int) -> bytes:
|
|
|
|
if n == 0:
|
|
|
|
return b'\x00'
|
|
|
|
return n.to_bytes((n.bit_length() + 7) // 8, 'big')
|
2018-07-30 16:10:10 +00:00
|
|
|
|
2018-12-04 12:46:48 +00:00
|
|
|
|
2022-06-22 09:44:19 +00:00
|
|
|
# 'deadbeef' -> b'deadbeef'
|
|
|
|
def str_to_bytes(s: str) -> bytes:
|
|
|
|
return bytes(s, encoding='latin-1')
|
|
|
|
|
|
|
|
|
|
|
|
# 'deadbeef' -> b'\xde\xad\xbe\xef'
|
|
|
|
def hex_str_to_bytes(s: str) -> bytes:
|
|
|
|
return bytes.fromhex(s)
|