2
votes

What is the correct way to get the length of a string in Python, and then convert that int to a byte array? What is the right way to print that to the console for testing?

2

2 Answers

4
votes

Use struct.

import struct

print struct.pack('L', len("some string")) # int to a (long) byte array
1
votes

using .Net:

byte[] buffer = System.BitConverter.GetBytes(string.Length)
print System.BitConverter.ToString(buffer)

That will output the bytes as hex. You may have to clean up the syntax for IronPython.