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

14,565

Solution 1

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?

Solution 2

Take a look HERE

uint8_t dataArray[dataString.length()];
dataString.toCharArray(dataArray, dataString.length())
Share:
14,565
Engo
Author by

Engo

Updated on June 14, 2022

Comments

  • Engo
    Engo almost 2 years

    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

  • Gareth McCaughan
    Gareth McCaughan about 8 years
    Do you see the bit that goes "@jB"? Those characters' ASCII codes, in hex, are 40 6A 42. Those are three bytes of that addr64 of yours. So let me point you again at my last bullet point. I conjecture that a ZBRxResponse contains various other things besides the actual data you sent, including in particular (some of?) that address you provided, and that what you're pulling data out of the wrong bit of the ZBRxResponse. See what getDataOffset returns. See what happens if you start there instead of at offset 0.
  • Gareth McCaughan
    Gareth McCaughan about 8 years
    (You should fix the sizeof thing too, but in your code as written the effect of that bug is likely to be less spectacular.)
  • Engo
    Engo about 8 years
    Thank you very much for your detailed explanation! I found multiple mistakes in my code. My improved code works properly! :) I will update my question to my working code...