cengal.data_manipulation.conversion.hex.versions.v_0.hex

  1#!/usr/bin/env python
  2# coding=utf-8
  3
  4# Copyright © 2012-2024 ButenkoMS. All rights reserved. Contacts: <gtalk@butenkoms.space>
  5# 
  6# Licensed under the Apache License, Version 2.0 (the "License");
  7# you may not use this file except in compliance with the License.
  8# You may obtain a copy of the License at
  9# 
 10#     http://www.apache.org/licenses/LICENSE-2.0
 11# 
 12# Unless required by applicable law or agreed to in writing, software
 13# distributed under the License is distributed on an "AS IS" BASIS,
 14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15# See the License for the specific language governing permissions and
 16# limitations under the License.
 17
 18
 19__all__ = [
 20    'bytes__to__hex_string',
 21    'hex_string__to__bytes',
 22    'bytes__to__solid_hex_string',
 23    'solid_hex_string__to__bytes',
 24    'hex_dword_to_int',
 25    'int_to_hex_dword',
 26]
 27
 28
 29"""
 30Module Docstring
 31Docstrings: http://www.python.org/dev/peps/pep-0257/
 32"""
 33
 34__author__ = "ButenkoMS <gtalk@butenkoms.space>"
 35__copyright__ = "Copyright © 2012-2024 ButenkoMS. All rights reserved. Contacts: <gtalk@butenkoms.space>"
 36__credits__ = ["ButenkoMS <gtalk@butenkoms.space>", ]
 37__license__ = "Apache License, Version 2.0"
 38__version__ = "4.4.1"
 39__maintainer__ = "ButenkoMS <gtalk@butenkoms.space>"
 40__email__ = "gtalk@butenkoms.space"
 41# __status__ = "Prototype"
 42__status__ = "Development"
 43# __status__ = "Production"
 44
 45
 46from cengal.system import PLATFORM_NAME
 47from cengal.code_flow_control.none_or import none_or
 48
 49import binascii
 50
 51
 52# HEX STRING - BYTES:
 53
 54
 55def bytes__to__hex_string(input_data: bytes, delimiter: str=None)->str:
 56    delimiter = none_or(delimiter, ' ')
 57    # 48508.80358249831 inputs per second
 58    fake_start = '00'
 59    fake_start_len = len(fake_start)
 60    hex_string = fake_start + binascii.hexlify(input_data).decode()
 61    result = delimiter.join(map(''.join, zip(*[iter(hex_string)]*2)))[fake_start_len:]
 62    result = result.strip()
 63    return result
 64
 65# Very high memory consumption! Use .replace(' ', '') and then solid_hex_string__to__bytes() instead.
 66if 'PyPy' == PLATFORM_NAME:
 67    def hex_string__to__bytes(input_data: str, delimiter: str=None)->bytes:
 68        delimiter = none_or(delimiter, ' ')
 69        result = b''.join(binascii.unhexlify(b.encode()) for b in input_data.split(delimiter))
 70        return result
 71else:
 72    def hex_string__to__bytes(input_data: str, delimiter: str=None)->bytes:
 73        delimiter = none_or(delimiter, ' ')
 74        result = b''.join(binascii.unhexlify(b) for b in input_data.split(delimiter))
 75        return result
 76
 77
 78def bytes__to__solid_hex_string(input_data: bytes)->str:
 79    return binascii.hexlify(input_data).decode()
 80
 81if 'PyPy' == PLATFORM_NAME:
 82    def solid_hex_string__to__bytes(input_data: str)->bytes:
 83        return binascii.unhexlify(input_data.encode())
 84else:
 85    def solid_hex_string__to__bytes(input_data: str)->bytes:
 86        return binascii.unhexlify(input_data)
 87
 88
 89# HEX DWORD:
 90
 91
 92def hex_dword_to_int(dword: str, byteorder=None, signed=False)->bytes:
 93    """
 94    "0x008A151D" is big endian; so dword = '008A151D', byteorder = 'big'
 95    :param dword: str(); '008A151D'
 96    :param byteorder: str(); 'big' / 'little'
 97    :param signed: bool();
 98    :return: 9049373
 99    """
100    byteorder = byteorder or 'big'
101    bin_dword = binascii.unhexlify(dword)
102    result = int.from_bytes(bin_dword, byteorder=byteorder, signed=signed)
103    return result
104
105
106def int_to_hex_dword(int_value, byteorder=None, signed=False):
107    byteorder = byteorder or 'big'
108    result = (binascii.hexlify(int_value.to_bytes(4, byteorder=byteorder, signed=signed))).decode()
109    return result
def bytes__to__hex_string(input_data: bytes, delimiter: str = None) -> str:
56def bytes__to__hex_string(input_data: bytes, delimiter: str=None)->str:
57    delimiter = none_or(delimiter, ' ')
58    # 48508.80358249831 inputs per second
59    fake_start = '00'
60    fake_start_len = len(fake_start)
61    hex_string = fake_start + binascii.hexlify(input_data).decode()
62    result = delimiter.join(map(''.join, zip(*[iter(hex_string)]*2)))[fake_start_len:]
63    result = result.strip()
64    return result
def hex_string__to__bytes(input_data: str, delimiter: str = None) -> bytes:
73    def hex_string__to__bytes(input_data: str, delimiter: str=None)->bytes:
74        delimiter = none_or(delimiter, ' ')
75        result = b''.join(binascii.unhexlify(b) for b in input_data.split(delimiter))
76        return result
def bytes__to__solid_hex_string(input_data: bytes) -> str:
79def bytes__to__solid_hex_string(input_data: bytes)->str:
80    return binascii.hexlify(input_data).decode()
def solid_hex_string__to__bytes(input_data: str) -> bytes:
86    def solid_hex_string__to__bytes(input_data: str)->bytes:
87        return binascii.unhexlify(input_data)
def hex_dword_to_int(dword: str, byteorder=None, signed=False) -> bytes:
 93def hex_dword_to_int(dword: str, byteorder=None, signed=False)->bytes:
 94    """
 95    "0x008A151D" is big endian; so dword = '008A151D', byteorder = 'big'
 96    :param dword: str(); '008A151D'
 97    :param byteorder: str(); 'big' / 'little'
 98    :param signed: bool();
 99    :return: 9049373
100    """
101    byteorder = byteorder or 'big'
102    bin_dword = binascii.unhexlify(dword)
103    result = int.from_bytes(bin_dword, byteorder=byteorder, signed=signed)
104    return result

"0x008A151D" is big endian; so dword = '008A151D', byteorder = 'big' :param dword: str(); '008A151D' :param byteorder: str(); 'big' / 'little' :param signed: bool(); :return: 9049373

def int_to_hex_dword(int_value, byteorder=None, signed=False):
107def int_to_hex_dword(int_value, byteorder=None, signed=False):
108    byteorder = byteorder or 'big'
109    result = (binascii.hexlify(int_value.to_bytes(4, byteorder=byteorder, signed=signed))).decode()
110    return result