Bug 572 : Serial com.write(String) Not working with char (-127 ...)
Last modified: 2007-06-01 16:37



Assigned To:

Attachment Type Created Size Actions

Description:   Opened: 2007-05-27 16:20
It seems that the conversion between String to Bytes is not accurate inside
the serial Library. When I convert the String to a Byte[], it works fine,
but using String with char as char a = (char)129 --> 0x39 is sent (?)

Could it be a problem with Java to C unsigned conversion?

It has been tested on windows XP with arduino on P5v124.
Serial Lib is dated: 04-02-2007

String s;

for(byte i=0; i<50; i++)
s += (char)(i+124);


(input s) --> |}~?�???????????�?��????????????�?? ¡¢£¤¥¦§¨©ª«¬­
(output)---> |}~???????????????????????????????? ¡¢£¤¥¦§¨©ª«¬­

String s;

for(byte i=0; i<50; i++)
s += (char)(i+124);

for(int i=0; i<s.length(); i++)

(input bytes) --> |}~?�???????????�?��????????????�?? ¡¢£¤¥¦§¨©ª«¬­­
(output)---> |}~?�???????????�?��????????????�?? ¡¢£¤¥¦§¨©ª«¬­
Additional Comment #1 From gll 2007-05-27 16:26
(Ok, it's hard to explain on html) I'll use int to represent the char.


With com.write(String)

)(50|2)(124||)(125|})(126|~)(127|)(63|?)(63|?)(63|?)(63|?)(63|?)(63|?)(63|?)(63|?)(63|?)(63|?)(63|?)(63|?)(63|?)(63|?)(63|?)(63|?)(63|?)(63|?)(63|?)(63|?)(63|?)(63|?)(63|?)(63|?)(63|?)(63|?)(63|?)(63|?)(63|?)(63|?)(63|?)(63|?)(160| )(161|¡)(162|¢)(163|£)(164|¤)(165|¥)(166|¦)(167|§)(168|¨)(169|©)(170|ª)(171|«)(172|¬)(173|­)(254|þ)

With com.write((byte)s.charAt(i));

|0)(2|50)(||124)(}|125)(~|126)(|127)(?|-128)(?|-127)(?|-126)(?|-125)(?|-124)(?|-123)(?|-122)(?|-121)(?|-120)(?|-119)(?|-118)(?|-117)(?|-116)(?|-115)(?|-114)(?|-113)(?|-112)(?|-111)(?|-110)(?|-109)(?|-108)(?|-107)(?|-106)(?|-105)(?|-104)(?|-103)(?|-102)(?|-101)(?|-100)(?|-99)(?|-98)(?|-97)( |-96)(¡|-95)(¢|-94)(£|-93)(¤|-92)(¥|-91)(¦|-90)(§|-89)(¨|-88)(©|-87)(ª|-86)(«|-85)(¬|-84)(­|-83)(þ|-2)

Additional Comment #2 From fry 2007-06-01 16:02
*** Bug 573 has been marked as a duplicate of this bug. ***
Additional Comment #3 From fry 2007-06-01 16:37
a char is a unicode character, my guess would be unicode 129 is garbage, so
String.getBytes() is giving you garbage.

if you want to send bytes, send a byte array, not a String array. there's
more about this in the Serial class javadoc:

* Write a String to the output. Note that this doesn't account
* for Unicode (two bytes per char), nor will it send UTF8
* characters.. It assumes that you mean to send a byte buffer
* (most often the case for networking and serial i/o) and
* will only use the bottom 8 bits of each char in the string.
* (Meaning that internally it uses String.getBytes)
* If you want to move Unicode data, you can first convert the
* String to a byte stream in the representation of your choice
* (i.e. UTF8 or two-byte Unicode data), and send it as a byte array.