在 Python 中将二进制数组转换为十进制(相当于 R readBin?)
Posted
技术标签:
【中文标题】在 Python 中将二进制数组转换为十进制(相当于 R readBin?)【英文标题】:Converting Array of Binary to Decimals in Python (Equivalent of R readBin?) 【发布时间】:2018-08-30 21:23:05 【问题描述】:所以,我将旧的 R 脚本重构为 Python,将十六进制字符串转换为数字数组(十六进制字符串来自专有数据库,数字最终成为坐标)。
我已经成功重构了所有代码,除了将原始二进制输出转换为十进制数。
我确定有一种简单的方法可以做到这一点?
R 脚本的相关部分:
fourByteFloatHexToNumeric <- function(hexCharacterVector)
rawString <- hexToRaw(hexCharacterVector) #returns as.raw(c(0x00, 0xfc, 0x3f, 0x46, 0x00, 0xcc, 0x3c, 0x46, 0x00, 0x90, 0x3c, 0x46, 0x00, 0x30, 0x3c, 0x46,...)
result <- readBin(rawString, what = "numeric", size = 4, n = nchar(hexCharacterVector) / 8) #returns arr c(12287, 12083, 12068, 12044, 12019, 11968, ....)
Python 脚本:
import base64
def readFromHexBlob(hex):
while(len(hex)%8 != 0):
hex = '0' + hex #pad the hex with leading 0's if it's stored in the dB wrong
array = base64.b16decode(hex)
#how to get to decimal array??
readFromHexBlob('FC3F4600CC3C4600903C4600303C4600CC3B4600003B4600943946008837460058354600E8324600E42F4600B82C4600482A460080294600B8294600E42A4600F82A4600902A4600C42A4600242B4600A82B46000C2C46007C2C4600AC2C4600582D4600CC2E46007C304600703246002035460028384600103B4600E03C4600383D4600DC3A4600EC3946005C39460040384600F83646009835460008344600983246009C314600AC304600402F4600642D4600742B4600E0284600B0254600A8204600301D46002C1B4600D01C4600841E4600841F4600D8204600B0224600CC2446003827460088294600642B4600B82C4600CC2D4600EC2E4600843046007C314600B4334600E0364600983A4600D43E4600A8424600D444460010464600A4474600D447460098484600144A4600004C4600944E460074504600A4514600205246006052460004534600605446009855460018574600985A4600FC5E460084624600946646005C6546005C644600C8634600B0614600586246005C6346005064460014654600DC654600A46646001067460018674600C0674600AC694600A06B46002C6D4600FC6E46002C7146008072460048734600CC7346009875460028784600047946000478460024774600007746000077460010774600F87646000C76460054744600B0714600786F4600F06D4600EC6B4600F869460088684600DC66460048654600FC634600286346003864460058664600A4674600A4684600A0694600346A4600DC6A4600A46C4600406F4600D4714600147446003876460058784600247A4600DC7B4600F47D46001C804600E6804600BC804600C47E4600947C4600B87A4600A8794600547946007C7846002877460068754600AC7246000C704600406E4600B86C4600286B4600E469460088684600D866460094644600FC614600405F4600A85A4600C4574600D8564600EC554600FC5546007C5646000456460080544600A45246007C504600E44D4600F04C46003C4C4600584B4600B44A4600EC494600B44846001C4746007443460044414600E43D4600B03C4600203F4600744146003843460080434600A4424600B4414600B0404600FC3F468FC2F53C8FC2753DB81E053ECDCC4C3E0000803E1F85EB3EC3F5283FF6285C3FC3F5883F295CAF3F9A99D93F85EB014066661640D7A33040A4704D40713D6A401F8583401F859340A470A5403D0AB740D7A3C8401F85DB40EC51F0405C8F0241CDCC0C411F8517413333234152B82E4166663A417B14464148E152411F855F41F6286C41CDCC7841C3F5824114AE8941E17A9041B81E974100009E41C3F5A44185EBAB41CDCCB2410AD7B94148E1C04185EBC741C3F5CE410000D6413D0ADD417B14E4413D0AEB417B14F241C3F5F8410AD7FF41295C03420AD7064233330A421F850D4248E11042713D1442A4701742D7A31A42CDCC1D42000021423D0A244200002742C3F52942C3F52C42CDCC2F421F853242713D3542C3F537425C8F3A42C3F53C4266663F42CDCC4142B81E444233334642EC51484266664A42A4704C42F6284E4248E14F42D7A35142295C534252B854423D0A564266665742CDCC58428FC259428FC25A4252B85B4214AE5C42EC515D4285EB5D42E17A5E423D0A5F42295C5F425C8F5F4252B85F4248E15F4248E15F42D7A35F42A4705F4233335F4248E15E42AE475E4214AE5D427B145D4266665C4266665B42A4705A42E17A5942E17A5842B81E5742CDCC5542E17A5442F6285342E17A5142CDCC4F42F6284E421F854C421F854A421F8548421F8546421F854442EC5142420000404214AE3D42295C3B4248E13842AE47364214AE33427B143142295C2E421F852B42D7A32842CDCC254248E12242CDCC1F4252B81C42D7A319421F851642AE4713420000104252B80C42E17A09427B140642D7A302425C8FFE418FC2F741CDCCF0410AD7E94148E1E2410AD7DB41CDCCD4418FC2CD41D7A3C6419A99BF41E17AB841A470B1416666AA41295CA341EC519C411F859541D7A38E4114AE8741C3F58041CDCC744114AE674152B85A419A994D41A4704141AE473541AE472941B81E1D417B14124133330741C3F5F8403333E34014AECF40B81EBD405C8FAA4000009840295C8740D7A3704033335340B81E3540713D1A40E17A0440A470DD3F85EBB13F713D8A3FAE47613F1F852B3F1F85EB3E713D8A3ECDCC4C3EB81E053E8FC2753D8FC2F53C')
【问题讨论】:
Convert hex string to int in Python的可能重复 【参考方案1】:第一件事:你不应该命名一个变量hex
,这是python的内置函数
要将表示十六进制数字的字符串转换为数字,可以使用int
:
>>> int('FC3F4600CC3C460090', 16)
46531388391247507621280
【讨论】:
谢谢,但是 R 的 readBin 在 R 中所做的是获取一个二进制流并从该流中读取 n 个字节并将其转换为数字(或其他数据类型)。所以在Python中我需要一些如何获取base64解码的十六进制并以4个字节块的形式遍历它并将每4个字节转换为int? rdocumentation.org/packages/base/versions/3.4.3/topics/readBin【参考方案2】:你在找这个吗?
import base64
def readFromHexBlob(hex):
while(len(hex)%8 != 0):
hex = '0' + hex #pad the hex with leading 0's if it's stored in the dB wrong
array = base64.b16decode(hex)
return array
hexstring = 'FC3F4600CC3C4600903C4600303C4600CC3B4600003B4600943946008837460058354600E8324600E42F4600B82C4600482A460080294600B8294600E42A4600F82A4600902A4600C42A4600242B4600A82B46000C2C46007C2C4600AC2C4600582D4600CC2E46007C304600703246002035460028384600103B4600E03C4600383D4600DC3A4600EC3946005C39460040384600F83646009835460008344600983246009C314600AC304600402F4600642D4600742B4600E0284600B0254600A8204600301D46002C1B4600D01C4600841E4600841F4600D8204600B0224600CC2446003827460088294600642B4600B82C4600CC2D4600EC2E4600843046007C314600B4334600E0364600983A4600D43E4600A8424600D444460010464600A4474600D447460098484600144A4600004C4600944E460074504600A4514600205246006052460004534600605446009855460018574600985A4600FC5E460084624600946646005C6546005C644600C8634600B0614600586246005C6346005064460014654600DC654600A46646001067460018674600C0674600AC694600A06B46002C6D4600FC6E46002C7146008072460048734600CC7346009875460028784600047946000478460024774600007746000077460010774600F87646000C76460054744600B0714600786F4600F06D4600EC6B4600F869460088684600DC66460048654600FC634600286346003864460058664600A4674600A4684600A0694600346A4600DC6A4600A46C4600406F4600D4714600147446003876460058784600247A4600DC7B4600F47D46001C804600E6804600BC804600C47E4600947C4600B87A4600A8794600547946007C7846002877460068754600AC7246000C704600406E4600B86C4600286B4600E469460088684600D866460094644600FC614600405F4600A85A4600C4574600D8564600EC554600FC5546007C5646000456460080544600A45246007C504600E44D4600F04C46003C4C4600584B4600B44A4600EC494600B44846001C4746007443460044414600E43D4600B03C4600203F4600744146003843460080434600A4424600B4414600B0404600FC3F468FC2F53C8FC2753DB81E053ECDCC4C3E0000803E1F85EB3EC3F5283FF6285C3FC3F5883F295CAF3F9A99D93F85EB014066661640D7A33040A4704D40713D6A401F8583401F859340A470A5403D0AB740D7A3C8401F85DB40EC51F0405C8F0241CDCC0C411F8517413333234152B82E4166663A417B14464148E152411F855F41F6286C41CDCC7841C3F5824114AE8941E17A9041B81E974100009E41C3F5A44185EBAB41CDCCB2410AD7B94148E1C04185EBC741C3F5CE410000D6413D0ADD417B14E4413D0AEB417B14F241C3F5F8410AD7FF41295C03420AD7064233330A421F850D4248E11042713D1442A4701742D7A31A42CDCC1D42000021423D0A244200002742C3F52942C3F52C42CDCC2F421F853242713D3542C3F537425C8F3A42C3F53C4266663F42CDCC4142B81E444233334642EC51484266664A42A4704C42F6284E4248E14F42D7A35142295C534252B854423D0A564266665742CDCC58428FC259428FC25A4252B85B4214AE5C42EC515D4285EB5D42E17A5E423D0A5F42295C5F425C8F5F4252B85F4248E15F4248E15F42D7A35F42A4705F4233335F4248E15E42AE475E4214AE5D427B145D4266665C4266665B42A4705A42E17A5942E17A5842B81E5742CDCC5542E17A5442F6285342E17A5142CDCC4F42F6284E421F854C421F854A421F8548421F8546421F854442EC5142420000404214AE3D42295C3B4248E13842AE47364214AE33427B143142295C2E421F852B42D7A32842CDCC254248E12242CDCC1F4252B81C42D7A319421F851642AE4713420000104252B80C42E17A09427B140642D7A302425C8FFE418FC2F741CDCCF0410AD7E94148E1E2410AD7DB41CDCCD4418FC2CD41D7A3C6419A99BF41E17AB841A470B1416666AA41295CA341EC519C411F859541D7A38E4114AE8741C3F58041CDCC744114AE674152B85A419A994D41A4704141AE473541AE472941B81E1D417B14124133330741C3F5F8403333E34014AECF40B81EBD405C8FAA4000009840295C8740D7A3704033335340B81E3540713D1A40E17A0440A470DD3F85EBB13F713D8A3FAE47613F1F852B3F1F85EB3E713D8A3ECDCC4C3EB81E053E8FC2753D8FC2F53C'
s = readFromHexBlob(hexstring)
test = int(hexstring,16)
print(test)
注意我怎么不使用你的功能
你也可以看看Convert hex string to int in Python
【讨论】:
是的,所以目标不是将非常长的十六进制字符串转换为非常大的整数。 readBin 在 R 中所做的是获取一个二进制流并从该流中读取 n 个字节并将其转换为数字(或其他数据类型)。 rdocumentation.org/packages/base/versions/3.4.3/topics/readBin谢谢你的屈尊 @user2657811 如果我的回答以这种方式出现,我并没有试图屈尊道歉,我只是指出我没有使用你的代码,即使我重新发布了它......我会推荐修改你的问题以反映你真正想要的,因为我和 Sylvain 都很困惑。以上是关于在 Python 中将二进制数组转换为十进制(相当于 R readBin?)的主要内容,如果未能解决你的问题,请参考以下文章