Skip to content

JS Arraybuffer to std::string #14662

Description

@ssg3d

I have an ArrayBuffer in JS like so:
[0 1 2 3 ... 127 128 129 ... 255]

I see from the emscripten documents that ArrayBuffer is bound to std::string.
My emscripten code is

void set(const std::string& buffer);
EMSCRIPTEN_BINDINGS(...)
{
function("set", &set);
}

When I call this from JS
set(buffer) // where buffer is an ArrayBuffer with [0 1 2 3 ... 127 128 129 ... 255] values

accessing the std::string in a for loop char by char using
std::cout << static_cast(buffer[i]) << std::endl;
yields
0 1 2 3 ... 127 -127, -126 -125 ... 1

Its understandable, since thats how chars beyond 127 are interpreted in JS, and it seems JS is first converting the ArrayBuffer to a string and passing it to emscripten. I however thought the documentation said ArrayBuffer -> string binding is supported, so I would expect the string c++ values to contain [0 .. 255].

Any suggestions please? Else I have to send an integer one by one from the arraybuffer to emscripten.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions