epm/User/test/pkg/common.py

76 lines
1.5 KiB
Python

'''
FilePath: \hart\test\pkg\common.py
Author: shenghao.xu
Date: 2023-03-15 10:17:55
LastModifiedBy: shenghao.xu
LastEditTime: 2023-05-15 14:42:43
Descripttion:
'''
from ctypes import *
import ctypes
import os
import struct
import sys
def print_hex_data_space(bytes, uppercase=False):
l = ['{:02x} '.format(int(i)) for i in bytes]
if uppercase:
l = [i.upper() for i in l]
print("".join(l))
pass
def print_hex_space(bytes):
l = ['0x{:02x} '.format(int(i)) for i in bytes]
print("".join(l))
pass
# 十六进制打印
def print_hex(bytes):
l = [hex(int(i)) for i in bytes]
print("".join(l))
pass
# 数组异或操作
def xor(bytes):
result = 0
for i in bytes:
result ^= i
return result
# bytearray 转 ctypes
def bytes_to_ctypes(bytes):
return (c_ubyte * len(bytes))(*bytes)
def float_to_hex_str(f): # 浮点数转十六进制字符串
fp = ctypes.pointer(ctypes.c_float(f))
cp = ctypes.cast(fp, ctypes.POINTER(ctypes.c_long))
return hex(cp.contents.value)
class HiddenPrints:
def __enter__(self):
self._original_stdout = sys.stdout
sys.stdout = open(os.devnull, 'w')
def __exit__(self, exc_type, exc_val, exc_tb):
sys.stdout.close()
sys.stdout = self._original_stdout
class LimitedArray:
def __init__(self, capacity):
self.capacity = capacity
self.array = []
def append(self, element):
if len(self.array) >= self.capacity:
self.array.pop(0)
self.array.append(element)