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!