Ironpython Array[byte] to byte string

ⅰ亾dé卋堺 提交于 2019-12-11 02:56:26

问题


Within IronPython 2.7, I am running a few calls to a .Net dll :

from System.Guid import NewGuid, ToByteArray
from System import Array, Byte
import clr
clr.AddReferenceToFile( ThePathToDLLFile )
from MyDLL import *
...
g = NewGuid()
buffer = ToByteArray(g)
ret = myDllMethodToFillBuffer( buffer )

Some methods like 'myDllMethod' return a buffer of type Array[byte]:

>>> type(buffer)
<type 'Array[Byte]'>

I am strugling to find the right way to convert the Array[byte] to Python 'string byte', IE '\x01\x02\x03 ...'

After a bit of googling I found the way to do the opposite conversion (string byte to byte[array])

byteArray = Array[Byte](ord(c) for c in byteList)

How should I convert a variable length byte[array] to string byte?


回答1:


Use the bytes function:

>>> bytes(Guid.NewGuid().ToByteArray())
b'\x14\x15\xd4\x05\xe4\xc2\xa8N\x9a\x99\t\x82\xe41r\xb3'


来源:https://stackoverflow.com/questions/25047101/ironpython-arraybyte-to-byte-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!