Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

String getBytes()

Thread view: 
eeh - 23 Apr 2007 04:09 GMT
Hi,

How to make b[0] to get 0xaa?

String s=""+(char)0xaa;
byte[] b=s.getBytes();
System.out.println(b[0]);<==result is not 0xaa

Thanks!
Knute Johnson - 23 Apr 2007 06:25 GMT
> Hi,
>
[quoted text clipped - 5 lines]
>
> Thanks!

String str = "\u00aa";

Signature

Knute Johnson
email s/nospam/knute/

Tom Hawtin - 23 Apr 2007 09:46 GMT
> How to make b[0] to get 0xaa?
>
> String s=""+(char)0xaa;
> byte[] b=s.getBytes();
> System.out.println(b[0]);<==result is not 0xaa

You seem to be confused between chars and bytes.

From API docs to String.getBytes:

"Encodes this String into a sequence of bytes using the platform's
default charset..."

What's the platform default charset? Anyone's guess. It's an utterly
pointless method. Always use a version with of the method that takes an
explicit charset. But what you usually want to do is to stick with chars:

String str = Character.toString((char)0xaa);
char[] cs = str.toCharArray();
System.out.println((int)cs[0]);

Tom Hawtin
Lothar Kimmeringer - 23 Apr 2007 23:24 GMT
> String s=""+(char)0xaa;
> byte[] b=s.getBytes();

Try s.getBytes("8859_1"); instead

> System.out.println(b[0]);<==result is not 0xaa

Then it should.

Dependent on your original problem, this might still not
be the solution of what you intend to solve.

Regards, Lothar
Signature

Lothar Kimmeringer                E-Mail: spamfang@kimmeringer.de
              PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
                questions!



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.