1

I'm trying to convert a Unicode String to UTF-16 on Arduino to send SMS messages in Greek (using the USC2 mode on the Sim800).

It requires that you provide the message in UTF16 format. For example you need to convert "Καλημέρα" (Good morning for the curious) to "039a03b103bb03b703bc03ad03c103b1" (\u039a\u03b1\u03bb\u03b7\u03bc\u03ad\u03c1\u03b1)

What I've accomplished so far, is to be able to convert a hardcoded wchar_t array to the correct format using this:

String stringtoHex() {
  const wchar_t arr[] = L"Καλημέρα";
  int len =  sizeof(arr)/sizeof(wchar_t);
  String hexString = "";

  for(int idx = 0; idx <len-1; idx ++ ){
    int c_val = arr[idx];
    char tempstring[4];
    sprintf( tempstring, "%04X", c_val );
    hexString += tempstring;
  }

  Serial.print(hexString);
  return hexString;
}

Now this works perfectly, but I need to be able to actually take a String parameter in the function and use that. And here is my problem:

I cannot find how to convert the unicode String to a wchar_t array in Arduino. Anyone has an idea how this will work?

So basically I need to make the function accept as a parameter a Unicode String, then convert it to a string of UTF16 and return it.

String stringtoHex(String message) {
    String hexString = "";
    ......
    //parse message and convert it to UTF16
    ......
    return hexString;
}
Lefteris
  • 14,550
  • 2
  • 56
  • 95
  • 1
    This doesn't look like C. Are you using C++? – melpomene Apr 21 '17 at 18:36
  • 1
    `sprintf( tempstring, "%04X", c_val );` is a buffer overflow. – melpomene Apr 21 '17 at 18:37
  • Arduino AFAIK supports both C and C++. Why is sprintf( tempstring, "%04X", c_val ) creating a buffer overflow ? – Lefteris Apr 21 '17 at 18:42
  • 1
    That doesn't answer the question of whether this code is C or C++. It's a buffer overflow because you're writing 5 chars to an array of size 3. – melpomene Apr 21 '17 at 18:58
  • It's a mix of C and C++ – Lefteris Apr 21 '17 at 19:14
  • In C, `return hexString;` is UB as it returns the address of an soon to be invalid pointer. – chux - Reinstate Monica Apr 21 '17 at 19:40
  • @Lefteris `sprintf( tempstring, "%04X", c_val )` attempts to write at least 5 `char` into an array with only room for 3. – chux - Reinstate Monica Apr 21 '17 at 19:41
  • It can't be a mix. You're either using a C compiler or a C++ compiler. And since you're using `+=` for string concatenation, it's not C. – melpomene Apr 21 '17 at 19:43
  • the Arduino language is merely a set of C/C++ functions that can be called from your code. Your sketch undergoes minor changes (e.g. automatic generation of function prototypes) and then is passed directly to a C/C++ compiler (avr-g++). All standard C and C++ constructs supported by avr-g++ should work in Arduino. For more details, see the page on the Arduino build process. – Lefteris Apr 21 '17 at 20:23
  • See http://stackoverflow.com/questions/148403/utf8-to-from-wide-char-conversion-in-stl – Mark Ransom Apr 21 '17 at 21:21

0 Answers0