java - String constructor to handle byte array producing weird result -
this question has answer here:
i using implicit string constructor handle byte array..but results weird. have more proper output..? appreciated. here code..
byte[] arr = { 23, 34, 20, 65, 88, 95 }; string s1 = new string(arr); system.out.println("first: "+s1); string s2 = new string(arr, 1, 3); system.out.println("second: "+s2);
it prints:
first: ?"?ax_
second: ?ax
the string()
constructor using interpreting bytes in array according default character set java environment. can differ.
if have specific character encoding want use, "us-ascii" or "utf-8", should specify character set using different constructor, such as: string(byte[] bytes, string charsetname).
Comments
Post a Comment