Results 1 to 2 of 2
- 09-23-2010, 05:40 AM #1Member
- Join Date
- Jul 2010
- Lima, Peru
- Rep Power
Problem with bytes and characters
Okay this is a simple problem but it is confusing me.
String str = "a";
byte bytes = str.getBytes();
Theoretically characters are two bytes each because java uses Unicode. However, the array that I get is of length 1. In other words it only has one byte.
Why on earth is it doing this? I want to get two bytes representing my character, not just one.
Actually, thinking about it I think it is using Ascii characters. But why would it use Ascii? I thought Java used unicode??
I think I fixed it. I had to write byte bytes = str.getBytes("UTF-16"); to get the proper encoding.
Last edited by PrinceSendai; 09-23-2010 at 06:16 AM. Reason: I FIXED IT
Yeah my first step when encountering this type of problem is to go to the API. It usually sets me straight in no time.
- By sankar.s.mca in forum Java ServletReplies: 0Last Post: 06-07-2010, 07:48 AM
- By sm123 in forum New To JavaReplies: 1Last Post: 04-21-2010, 06:49 PM
- By ze snow in forum New To JavaReplies: 1Last Post: 02-23-2010, 11:47 PM
- By lumpy in forum New To JavaReplies: 2Last Post: 02-15-2010, 07:49 AM
- By sagarsway in forum JDBCReplies: 1Last Post: 12-30-2008, 05:39 PM