[Numpy-discussion] numpy.ascontiguousarray on byteswapped data !?

Sebastian Haase haase at msg.ucsf.edu
Fri Aug 11 14:22:01 CDT 2006

```On Thursday 10 August 2006 21:32, Sebastian Haase wrote:
> Travis Oliphant wrote:
> > Sebastian Haase wrote:
> >> Hi,
> >> Does numpy.ascontiguousarray(arr) "fix" the byteorder when arr is
> >> non-native byteorder ?
> >>
> >> If not, what functions does ?
> >
> > It can if you pass in a data-type with the right byteorder (or use a
> > native built-in data-type).
> >
> > In NumPy, it's the data-type that carries the "byte-order"
> > information.    So, there are lot's of ways to "fix" the byte-order.
>
> So then the question is: what is the easiest way to say:
> give me the equivalent type of dtype, but with byteorder '<' (or '=') !?
> I would be cumbersome (and ugly ;-) ) if one would have to "manually
> assemble" such a construct every time ...

I just found this in  myCVS/numpy/numpy/core/tests/test_numerictypes.py
<code>
def normalize_descr(descr):
"Normalize a description adding the platform byteorder."

out = []
for item in descr:
dtype = item[1]
if isinstance(dtype, str):
if dtype[0] not in ['|','<','>']:
onebyte = dtype[1:] == "1"
if onebyte or dtype[0] in ['S', 'V', 'b']:
dtype = "|" + dtype
else:
dtype = byteorder + dtype
if len(item) > 2 and item[2] > 1:
nitem = (item[0], dtype, item[2])
else:
nitem = (item[0], dtype)
out.append(nitem)
elif isinstance(item[1], list):
l = []
for j in normalize_descr(item[1]):
l.append(j)
out.append((item[0], l))
else:
raise ValueError("Expected a str or list and got %s" % \
(type(item)))
return out
</code>

Is that what I was talking about !?  It's quite a big animal.
Would this be needed "everytime" I want to get a "systembyte-ordered version"
of a given type !?

- Sebastian

>
> > Of course there is still the difference between "fixing" the byte-order
> > and simply "viewing" the memory in the correct byte-order.  The former
> > physically flips bytes around, the latter just flips them on calculation
> > and presentation.
>
> I understand. I need something that I can feed into my C routines that
> are to dumb to handle non-contiguous or byte-swapped data .
>
> - Sebastian

```