Kost
Kost

Reputation: 21

Special chars in the java String

I am looking for solution for insert special 1 byte chars in the java String like 0xFE, 0xFF ( > 127).

I need use String instead byte array because method of class need String type only.

What I need in the String = {0x07 0xAA 0x03 FF}. Yes it is special chars which uses for device control.

And I tried to use String constructur as:

byte[] arr = {0x07, (byte) 0xAA, 0x03, (byte) FF};
String s = new String(arr, "UTF-8"); 

//utf-8 uses for 1byte chars, and also I tried ascII, but result was the same

All works up to value 127(dec) of char in array, but after it give strange result like 1 or 2 additional bytes in the String, ofcourse it occured because byte type has max value 127. But how fix this situation?? Need help.

How I understood I can't to add char[] to the String constructor

Upvotes: 2

Views: 352

Answers (1)

Kevin Cruijssen
Kevin Cruijssen

Reputation: 9336

One part of your question is incorrect. UTF-8 only stores the printable ASCII characters in the range [32,126] as single bytes (beyond that it's 2 or even 3 bytes per character) even though encodings are usually 256 bytes long. Since in Java all Strings are automatically stored and printed as UTF-8 by default, they will remain more than 1 byte when printing.

What you need is an encoding that stores all [1,256] first bytes as single bytes when accessing the bytes. For example, with your current code:

byte[] arr = {0x07, (byte) 0xAA, 0x03, (byte) 0xFF};
String s = new String(arr, "UTF-8");

System.out.println(s.getBytes().length); // This prints 8
System.out.println(s.getBytes("UTF-8").length); // This prints 8 as well (since the default is UTF-8)
System.out.println(s.getBytes("Windows-1252").length); // This prints 4

Try it online.

ISO-8859-1 (also known as Code Page 1252 or Windows-1252) or Code page 437 are two pretty well-known encodings.

Upvotes: 2

Related Questions