Search code examples
cstringarduinoxbeeuint8t

How to convert a string into an uint8_t array on Arduino?


I have a string that contains both numbers and character values like "p1200" for example. I need to convert this string into a uint8_t array, because I need to send it from my xBee.

How can I convert

String dataString = "p1200"

into

uint8_t dataArray[]

?

I tried to send this string using the following code:

power = ((360 * pulseCount) / 60);
String dataString = "p" + power;
char dataArray[sizeof(dataString)];
dataString.toCharArray(dataArray, sizeof(dataString));
XBeeAddress64 addr64 = XBeeAddress64();
addr64.setMsb(0x13A200);
addr64.setLsb(0x406A42B7);
ZBTxRequest zbTx = ZBTxRequest(addr64, (uint8_t *)dataArray, sizeof(dataArray));
xbee.send(zbTx);

And receive the string using the following code:

String incomingData;
xbee.readPacket();
if (xbee.getResponse().isAvailable()) {
    Serial.println(xbee.getResponse().getApiId());
    if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE) {
        xbee.getResponse().getZBRxResponse(rx);
        for (int i = 0; i < rx.getDataLength(); i++) {
            incomingData += (char)rx.getData(i); 
        }
    }
}

When I print incomingData, I get a strange ouput... I thought it was caused by the conversion from string to uint8_t


Solution

  • Use getBytes (https://www.arduino.cc/en/Reference/StringGetBytes) to copy the bytes from your string into an array. That expects a byte[], and unless https://www.arduino.cc/en/Reference/byte is actively misleading that should be the same as a uint8_t[].

    You will need the array to exist already and be large enough. You can find the length of a string with its length method.

    (I think getBytes is preferred over toCharArray if it's definitely a uint8_t[] you're wanting.)

    Some comments on your code

    The above was written before Engo posted some code, and attempts simply to answer the question. But here are some comments on the code.

    • Calling sizeof on the String is almost certainly not what you want. A String object may include other things (e.g., length information) and may not include the actual bytes (which might e.g. be behind a pointer). There's a length method; use it. (Remembering that your buffer will need to be one byte bigger because of the terminating null character.)
    • I've no idea what's going on with the magic numbers in addr64 (I don't know anything about xBee) but will assume what you're doing there makes sense -- but it looks like the kind of thing that's worth checking really carefully.
    • You say you "get a strange output", but it would be more useful if you told us exactly what sort of strange output, and perhaps how (if at all) it changes when you change the string you're trying to transmit.
    • Your code references what I take to be a variable, called rx, but you haven't shown us how it's declared.
    • ZBRxResponse appears to have a method called getDataOffset. Again, I don't know anything about this stuff, but is the stuff you're actually trying to pull out of the response perhaps not starting at offset 0 but at the offset given by calling getDataOffset?