skifbrt
skifbrt

Reputation: 21

Arduino escape character \x

I'm using Arduino Pro Mini and OLED display .96" with u8glib library. So I have to use this code to output Ukrainian word "Тест".

String str = "\xc2\xd5\xe1\xe2";
u8g.print(str);

It works fine. But when I transmit the same string via Bluetooth from Android it outputs "\xc2\xd5\xe1\xe2" :(

while (BTSerial.available() > 0){     
   char recieved = BTSerial.read();
   inData += recieved;
   if (recieved == '\n'){
      u8g.print(inData);
   }
}

Why Arduino does not "see" escape characters \x that came from the Android? I've tried to read 2 characters and do something like this:

inData += "\x" + char1 + char2;

But have Error: \x used with no following hex

Android code

String str = "Тест";
String result = "";
for (int i = 0; i < str.length(); i++) {
   char c = str.charAt(i);
   int code = (int)c;
   if (code >= 1025){
      code -= 864;
      result += "\\x" + Integer.toHexString(code);
   }else {
      result += c;
   }
}
byte[] msgBuffer = result.getBytes();
String b = "\n";
byte[] n = b.getBytes();
try {
   mmOutStream.write(msgBuffer);
   mmOutStream.write(n);
}catch (IOException e){
   e.printStackTrace();
}

Upvotes: 2

Views: 6619

Answers (1)

Les
Les

Reputation: 10605

The string that gets initialized by Arduino into 'str' is converted to the actual characters. It does not really contain \xc2\xd5\xe1\xe2, it contains the characters c2, d5, e1, and e2.

From Android, you would need to send the same "unescaped" values.

When you initialize a string in code, like:

String str = "\xc2\xd5\xe1\xe2";
u8g.print(str);

The \x is an escape to tell the compiler that the next characters are to be treated as the hex representation of the character to initialize with. u8g.print(str) does not see an escaped string, it sees the actual 4 characters (and null) that the compiler converted it to.

Edit: Based on your update showing the Android code, it looks like you will not be sending \x at all. Try changing getBytes() to getBytes("ISO 8859-5") which is the code for Cryllic

Edit2: In Java, a char is an unsigned 16 bit integer, so my last edit was flawed.

You are likely wasting time by escaping characters on the Android side of the transaction. Instead, I think that the returned value of BTSerial.read() is being truncated when assigned to char. Try changing the type of "received" to "int".

Here is a thread on the Arduino board that might help you deal with Unicode better.

Upvotes: 2

Related Questions