# Convert String to unsigned Char \[16\];

**URL:** <https://forum.arduino.cc/t/convert-string-to-unsigned-char-16/1039323>\
**Category:** Programming\
**Created:** [October 7, 2022, 6:10am UTC](https://forum.arduino.cc/t/convert-string-to-unsigned-char-16/1039323 "2022-10-07T06:10:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![userarduino06](https://avatars.discourse-cdn.com/v4/letter/u/9de053/32.png) [@userarduino06](https://forum.arduino.cc/u/userarduino06)\
**Post date:** [October 7, 2022, 6:10am UTC](https://forum.arduino.cc/t/convert-string-to-unsigned-char-16/1039323/1 "2022-10-07T06:10:19Z")

</div>

How to convert a String var to unsigned char var[16]; the variables I will use to decrypt the value.  
The code is :  
void komunikasiSerial(void) {  
if (Serial2.available() \> 0) {  
String baca = Serial2.readString();  
char dataChar[16];  
baca.toCharArray(dataChar, sizeof(baca));  
unsigned char tes = (unsigned char)dataChar  
Serial.println(tes);  
}  
}

-- Error --  
if (Serial2.available() \> 0) {  
^~  
exit status 1  
cast from 'char\*' to 'unsigned char' loses precision [-fpermissive]

---

<div class="post-metadata">

**Author:** ![sterretje](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sterretje/32/391294_2.png) [@sterretje](https://forum.arduino.cc/u/sterretje)\
**Post date:** [October 7, 2022, 6:19am UTC](https://forum.arduino.cc/t/convert-string-to-unsigned-char-16/1039323/2 "2022-10-07T06:19:43Z")

</div>

You're missing a semi-colon at _`unsigned char tes = (unsigned char)dataChar`_. After that your function compiles (with warnings). If it does not compile, post your complete sketch; please use code tags when posting code (and error messages) as described in [How to get the best out of this forum](https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum/679966).

_dataChar_ is an array, _test_ is a character, hence the cast warning. This will work

```cpp
unsigned char *tes = (unsigned char*)dataChar;

```

But you can't print _tes_ that way; use a for-loop to print all elements.

---

<div class="post-metadata">

**Author:** ![userarduino06](https://avatars.discourse-cdn.com/v4/letter/u/9de053/32.png) [@userarduino06](https://forum.arduino.cc/u/userarduino06)\
**Post date:** [October 7, 2022, 6:31am UTC](https://forum.arduino.cc/t/convert-string-to-unsigned-char-16/1039323/3 "2022-10-07T06:31:43Z")

</div>

wait i will try it

---

<div class="post-metadata">

**Author:** ![red\_car](https://avatars.discourse-cdn.com/v4/letter/r/b77776/32.png) [@red\_car](https://forum.arduino.cc/u/red_car)\
**Post date:** [October 7, 2022, 7:20am UTC](https://forum.arduino.cc/t/convert-string-to-unsigned-char-16/1039323/4 "2022-10-07T07:20:59Z")

</div>

The 2nd parameter of toCharArray() is the size of the target array, not the source String.

Try this...

```arduino
String str = "Hello World!";

char array[16];

void setup()
{
  Serial.begin(115200);
  
  str.toCharArray(array, sizeof(array));
  
  Serial.println(array);
}

void loop()
{}

```

No idea what you're trying to do with this statement...

> [@userarduino06](#):
>
> unsigned char tes = (unsigned char)dataChar

You don't need it to convert to a char array.

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [October 7, 2022, 8:35am UTC](https://forum.arduino.cc/t/convert-string-to-unsigned-char-16/1039323/5 "2022-10-07T08:35:42Z")

</div>

> [@userarduino06](#):
>
> String var to unsigned char var[16];

why not read the string directly into char array

possible output

```arduino
charlie
 63 68 61 72 6C 69 65

```

```arduino
char buf [80];

void
loop (void)
{
    if (Serial.available ()) {
        int n = Serial.readBytesUntil ('\n', buf, sizeof(buf)-1);
        buf [n] = '\0';

        Serial.println (buf);

        for (int i = 0; i < n; i++) {
            Serial.print (" ");
            Serial.print (buf [i], HEX);
        }
        Serial.println ();
    }
}

void
setup (void)
{
    Serial.begin (9600);
}

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 5, 2023, 8:36am UTC](https://forum.arduino.cc/t/convert-string-to-unsigned-char-16/1039323/6 "2023-04-05T08:36:14Z")

</div>

Closed on April 5, 2023, 8:36am UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
