2

I'm translating some of my old Ruby scripts into Python and I have trouble finding a function in Python that belongs to Ruby: http://www.ruby-doc.org/core/classes/Array.src/M002222.html - which keeps the array in little endian byte order. Is there any Python module that can help me?

wishi
  • 7,188
  • 17
  • 64
  • 103

2 Answers2

1

The array module of Python's standard library may offer somewhat-similar functionality (though it's definitely not an exact match). It does not force endianness (you can swap items' endianness with method byteswap) and does not intrinsically keep items as "a string" (you can convert back and forth with methods tostring and fromstring) but it may be worth looking at, depending on how you're using the Ruby pack function I guess.

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
1

The Python memoryview built-in allows you to "cast" an array or any bytes-like object to different endianess. Se the .cast method. The great thing about memoryviews is that they operate without copying bytes: they share memory with the source data structure.

Luciano Ramalho
  • 1,981
  • 18
  • 22