Python:将十六进制值从大端转换为小端,然后转换为十进制

时间:2019-12-03 09:07:02

标签: python hex

我正在读取文件并从文件中提取数据,我能够获取ASCII数据和整数数据。

我正在尝试将8个字节的数据从Big Endian转换为Little Endian,然后转换为十进制值。

输入文件具有此数据

  

00 00 00 00 00 00 f0 3f

此值必须转换为0x3ff0000000000000,以便hex_to_double(0x3ff0000000000000)返回值1.0

我试图将上述值转换​​为little-endian和十进制的代码是

const CourseSchema = new mongoose.Schema({
    name:{type:String,required:true},
    status:{type:String,required:true},
    subject:{
        type:mongoose.Schema.Types.ObjectId,
        ref :'subjects'
    },
    teacher:{
        type:mongoose.Schema.Types.ObjectId,
        ref :'users'
    },
    sessions:[
        {
         Date:{type:Date,required:true},
         timeStart:{type:String,required:true},
         timeEnd  :{type:String,required:true},
         students :[{
             type:mongoose.Schema.Types.ObjectId,
             ref :'users'
         }]   
        }
    ]
})

任何建议都会有所帮助。

1 个答案:

答案 0 :(得分:2)

您的数据已经在base16中。因此,您应该先将其转换为二进制格式,然后再将其解压缩为十进制:

>>> data = binascii.unhexlify(b'000000000000f03f')
>>> data
b'\x00\x00\x00\x00\x00\x00\xf0?'
>>> struct.unpack('<d', data)
(1.0,)