I am working on a project with Bluetooth commands between various devices. I would like to try and convert a string to a DataView Object on Arduino, so I can send it over BLE, much like this function written in Javascript:

function str2DV(str) {
  var buf = new ArrayBuffer(str.length);
  var bufView = new Uint8Array(buf);
  for (var i=0, strLen=str.length; i<strLen; i++) {
    bufView[i] = str.charCodeAt(i);
  var DV = new DataView(buf);
  return DV;

Is this possible with Arduino? I have yet to find a solution....


1 Answer 1


There is no use for a DataView in the Arduino environment. In the JavaScript language, a string and an array of bytes are two very different things. That's why you need something like this str2DV() function to convert between them. In C++, they are one and the same thing: no need to convert. Sometimes the type uint8_t (also named byte) is used in preference of char for storing binary data, but you can just typecast between these types. The typecast typically compiles to zero machine instructions, as the underlying data representation is the same.

For example:

extern some_lib_function(const uint8_t *buffer, int buffer_length);

const char *my_message = "Hello, World!\n";
some_lib_function((uint8_t *) my_message, strlen(my_message));

If your string is in the form of a String object, rather than a plain C-string, then use the c_str() method to get the plain string back:

String my_message = "Hello, World!\n";
some_lib_function((uint8_t *) my_message.c_str(), my_message.length());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.