Skip to content

NULL bytes removed from end of string in NumPy array (Trac #2024) #2617

@numpy-gitbot

Description

@numpy-gitbot

Original ticket http://projects.scipy.org/numpy/ticket/2024 on 2012-01-19 by trac user edcjones, assigned to unknown.

I use Debian stable on a PC. The distribution uses Python 2.6.6 and
NumPy 1.4.1.

In Python and I assume in NumPy, strings are not NULL-terminated.
The following program strips the zero bytess from the end of a string:

! /usr/bin/env python

import numpy

DT = numpy.dtype([('hashcode', numpy.str_, 16)])
badstring = 4 * chr(0) + 'ABCDEFGH' + 4 * chr(0)
print repr(badstring)
arr = numpy.array((badstring,), dtype=DT)
print repr(arr)
s = str(arr['hashcode'])
print len(s), repr(s)

The output is:

'\x00\x00\x00\x00ABCDEFGH\x00\x00\x00\x00'
array(('\x00\x00\x00\x00ABCDEFGH',),
dtype=[('hashcode', '|S16')])
12 '\x00\x00\x00\x00ABCDEFGH'

It appears that the string is being treated like a C-string somewhere in
NumPy.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions