rogersce / cnpy

library to read/write .npy and .npz files in C/C++
MIT License
1.34k stars 301 forks source link

Add support for signed types #65

Open sammymax opened 4 years ago

sammymax commented 4 years ago

Even though "int" by default is signed, "signed int" has a different typeid. Although most developers probably won't use "signed int" directly, a lot of compilers define int8_t, int16_t, etc. with these signed types. For example, the following code gives an invalid .npy file (compiled with GCC 7.4.0 on Ubuntu 20.04):

#include <cstdint>
#include <vector>
using namespace std;

#include "cnpy.h"

int main() {
  vector<int8_t> vec(20);
  for (int i = 0; i < 20; i++) vec[i] = 2 * i;
  cnpy::npy_save("hi.npy", vec.data(), {20});
}

When I try to load hi.npy in Python, I get the following error: ValueError: descr is not a valid dtype descriptor: '<?1' because cnpy couldn't decide if int8_t was signed or not.