Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

Use the bytes function:

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.