Skip to content

How to create an NDArray from pointer and NPTypeCode? #390

Closed

Description

@LarryThermo

First of all thanks for this project.

My code calls into lower level C++ code and I wish to represent the returned array as an NDArray in C#. I have a pointer to the data, the length of the data in bytes, as well as type code. How would I go about creating an NDArray for this case? Something like:

NDArray CreateNDArray(void* data,int dataLengthBytes,NPTypeCode typeCode)
{
var storage = new UnmanagedStorage( "something here" );
var nda = new NDArray(storage);
return nda;
}

Thanks,

Lars

Activity

  1. changed the title [-]How to create and NDArray from pointer and NPTypeCode[/-] [+]How to create an NDArray from pointer and NPTypeCode?[/+] on Jan 24, 2020
  2. Oceania2018 commented on Jan 24, 2020

    @Oceania2018
    Member

    @LarryThermo This snippet of code will help you.

  3. LarryThermo commented on Jan 27, 2020

    @LarryThermo
    Author
  4. Nucs commented on Sep 7, 2026

    @Nucs
    Member

    Use np.frombuffer (0.70.0, #628) to wrap a byte buffer as an NDArray of a given dtype:

    byte[] raw = new byte[16];
    BitConverter.GetBytes(1.0).CopyTo(raw, 0);
    BitConverter.GetBytes(2.0).CopyTo(raw, 8);
    
    var nd = np.frombuffer(raw, "float64");   // also "int32" / "<f8" / NPTypeCode.Double
    Console.WriteLine(nd);   // [1. 2.]

    For a raw T* pointer, wrap it in an UnmanagedMemoryBlock<T> and build the NDArray from that storage. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions