Results 1 to 2 of 2
  1. #1
    PrinceSendai is offline Member
    Join Date
    Jul 2010
    Location
    Lima, Peru
    Posts
    47
    Rep Power
    0

    Default 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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. Replies: 0
    Last Post: 06-07-2010, 07:48 AM
  2. Problem reading from socket using read(bytes[])
    By sm123 in forum New To Java
    Replies: 1
    Last Post: 04-21-2010, 06:49 PM
  3. Problem with writing unicode characters in a file
    By ze snow in forum New To Java
    Replies: 1
    Last Post: 02-23-2010, 10:47 PM
  4. Replies: 2
    Last Post: 02-15-2010, 06:49 AM
  5. Replies: 1
    Last Post: 12-30-2008, 04:39 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •