1 Star 0 Fork 0

aisen/cipher_project

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
gt_encode.py 1.58 KB
一键复制 编辑 原始数据 按行查看 历史
senai 提交于 2024-07-26 00:35 . xs
from ctypes import string_at
from sys import getsizeof
from binascii import hexlify
# def decode_num(self, encode_num):
# decoded_value = encode_num.decode()
# return decoded_value
#
#
# def encode_num(self, number):
# # rnd = random.SystemRandom().randrange(1, self.max_int)
# encoded_value = FixedPointNumber.encode(number, self.n, self.max_int, precision=None)
# return encoded_value
def encoding(number, bit_lenth=32):
# type(number) = int
if isinstance(number, int):
int_bit_list = [0 for x in range(bit_lenth)]
abs_number = abs(number)
for i in range(bit_lenth):
bit = abs_number & 1
abs_number = abs_number >> 1
int_bit_list[i] = bit
int_bit_list.reverse()
if number < 0:
int_bit_list[0] = 1
else:
int_bit_list[0] = 0
return int_bit_list
# type(number) = float
elif isinstance(number, float):
buffer = hexlify(string_at(id(number), getsizeof(number)))
buffer_float = buffer[len(buffer) - 16:]
temp = [buffer_float[i:i + 2] for i in range(0, len(buffer_float), 2)]
flt_bin_list = ''.join([bin(int(temp[i].decode(), 16))[2:].rjust(8, '0')
for i in range(len(temp) - 1, -1, -1)])
flt_bin_list = list(map(int, flt_bin_list))
return flt_bin_list
else:
raise TypeError("Don't know the precision of type %s." % type(number))
if __name__ == '__main__':
enc_result = encoding(-0.5)
print(encoding(0.5))
print(enc_result)
print(len(enc_result))
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/senai/cipher_project.git
git@gitee.com:senai/cipher_project.git
senai
cipher_project
cipher_project
master

搜索帮助