将 ascii hexbin 转换成 4 位二进制数的方法如下:
代码示例:
def ascii_to_hexbin(ascii_str):
"""将 ascii 字符串转换成 4 位二进制数的字符串表示"""
hex_str = ascii_str.encode('hex')
bin_str = bin(int(hex_str, 16))[2:]
return bin_str.zfill(4)
# 示例
ascii_str = 'A'
bin_str = ascii_to_hexbin(ascii_str)
print(bin_str) # 输出 '0100'
function asciiToHexbin(asciiStr) {
// 将 ascii 字符串转换成 4 位二进制数的字符串表示
const hexStr = Buffer.from(asciiStr, 'ascii').toString('hex')
const binStr = parseInt(hexStr, 16).toString(2)
return binStr.padStart(4, '0')
}
// 示例
const asciiStr = 'A'
const binStr = asciiToHexbin(asciiStr)
console.log(binStr) // 输出 '0100'
上一篇:ASCII还是UTF-8?