确认号字节流进制转换
1.字节流
字节流是由原始字节组成的二进制数据序列,通常用于表示文件、图像、音频等二进制数据。
Python 实现
代码
byte_stream = bytes([0xD2, 0xB9, 0x10, 0xBC]) #字节流
dec_bytes=int.from_bytes(byte_stream,byteorder='big')# int.from_bytes 方法将字节流byte_stream转换为整数。byteorder='big' 表示使用大端字节序来解释字节流,将字节流的内容转换为整数。字节流在Python中是不可变的。
print("hex {} is converted to dec: {}".format(byte_stream, dec_bytes))
结果:
MATLAB实现
代码
% 字节流
byte_stream = uint8([0xD2, 0xB9, 0x10, 0xBC]);
% 使用dec2hex函数将字节流转换为十六进制字符串
hex_strings = dec2hex(byte_stream);
% 合并十六进制字符串为一个字符串
merged_hex_string1 =join(string(hex_strings));
merged_hex_string = replace(merged_hex_string1,' ','');
dec_num=hex2dec(merged_hex_string);
% 显示结果
fprintf('hex %s is converted to dec:%d',merged_hex_string,dec_num)
结果
字符串
字符串是由Unicode字符组成的文本序列,可以包含各种字符(字母、数字、符号等)。
Python Jupter实现
代码:
hex_num = input("please input a hex:")
dec_num = int(hex_num, 16)
print("hex {} is converted to dec: {}".format(hex_num, dec_num))
运行结果:
MATLAB代码实现
代码
%MATLAB实现十六进制转换为十进制
hex_num=input('please input a hex:','s');
dec_num=hex2dec(hex_num);
fprintf('hex %s is converted to dec:%d',hex_num,dec_num)
运行结果: