Hello,
I am stuck transforming a char-array to a String.
When using:
byte[] inBuffer = new byte[60];
myPort.readBytesUntil(lf, inBuffer);
tring myString = new String(inBuffer);
there seems to be some data after the “lf” in the string. How do I remove this? The length of the byte is variable, so 60 byte is maximum. How can I make shure to convert only the bytes until “lf” (from the readUntil). I think I have to add a tremination byte to the array or something.
I hope you understand what I mean.
1 Like
Hell @leclerke,
Assuming you are using this:
A simple example of the data that you are sending will help.
I would expect the array to be initialized filled with zeroes.
In the context of the code example you provided this may work:
byte [] inBuffer = {'T', 'e', 's', 't', '\r','\n','0','0','0','0','0'};
String s = new String(inBuffer);
println(s); // This will also print the \r\n from above!
int i = s.indexOf("\r");
println(s.substring(0, i));
A bit or research will reveal that there are other ways to do this.
:)