3

I have String:

String str = "KITTEN"; 

And I would like to make a byte[] from it, but just:

byte[] mybyte = new byte[] { 'K', 'I', 'T', 'T', 'E', 'N'} 

Anyone knows how to change String to that byte[]? Everything what I can find is about actual converting - please note that I don't want to have hex representation of string.

PS. Title of question may be wrong - I'm sorry for that, didn't know how to write it.

EDIT:

byte[] myBytes = str.getBytes(); 

Displaying:

byte[] display = new byte[] { 0x00, 0, 0, 'K', 'I', 'T'}; 

works, and displaying:

byte[] display = new byte[] { 0x00, 0, 0, myBytes[0], myBytes[1], myBytes[2] }; 

doesn't work. I try to display it on the screen of my device connected to the android phone by NFC. Do you know what's make the problem?

13
  • 2
    What about String.getBytes() Commented Sep 28, 2012 at 12:08
  • 3
    Don't know what you mean by "converting", but the characters in a String are 16 bit and bytes are 8 bit, so some conversion is obviously required. Commented Sep 28, 2012 at 12:08
  • 1
    "it throws exception" is a very poor description. What exception? Commented Sep 28, 2012 at 12:11
  • 2
    @Doszi89: I strongly suspect you're very confused about what data you've actually got at any one time. The code you've written will give the same results for both arrays, assuming the platform default encoding isn't particularly unusual. (And assuming your string really is "KITTEN" and not something containing some Unicode control characters.) How they're displayed in the debugger is a different matter. getBytes() works fine - it's either your diagnostics or what you're trying to do that's wrong. Unfortunately you still haven't really given us enough context. Commented Sep 28, 2012 at 14:34
  • 1
    "UnsupportedEncodingException" suggests that your String contains a character that is not in the default character set. Either that or you've somehow (elsewhere) specified the default character set to be an invalid value. (If you print the getMessage() result from the exception you'll get some detailed cause info.) Commented Sep 28, 2012 at 16:21

4 Answers 4

7

You are converting, and must convert - a char isn't the same as a byte.

You could use "ISO-8859-1" as the encoding to pass to String.getBytes(), which would get you a single byte per character, valid only for U+0000 to U+00FF (probably including characters in the "hole"; characters which aren't really in ISO-8859-1, but hey...) You'd get the ASCII representation of '?' for any characters not in that range though.

Wanting to do this is probably an indication that you've gone wrong earlier on though. In particular, if you're trying to get back some arbitrary binary data which you originally converted into a string without using base64 or something similar, you may well have already lost data.

Sign up to request clarification or add additional context in comments.

7 Comments

The “hole” is present in latin-1 (which indeed maps to the range U+0000 to U+00FF in Unicode), but it is not in cp-1252.
@SébastienLeCallonnec: I guess I meant "characters in the hole" - will edit. My point is that on some platforms, a lazy ISO-8859-1 implementation just assumes it's a mapping of U+0000-U+00FF to bytes 0x00 to 0xff.
My point is that the same “hole” exists in Unicode: unicode.org/charts/PDF/U0080.pdf.
@Jon.. Man, how do remember all those stuffs?? +1
@rohit he doesn't. He invented everything himself in the first place.
|
1

getBytes() works for me:

String str = "KITTEN"; byte[] bytes = str.getBytes(); for(byte b : bytes) { System.out.print("'" + (char) b + "' "); } 'K' 'I' 'T' 'T' 'E' 'N' 

Everything else what @Jon writes is still valid, as getBytes() is also doing a conversion:

Encodes this String into a sequence of bytes using the platform's default charset, storing the result into a new byte array.

Since using getBytes() introduces some platform dependencies, you should better explicitly pass an appropriate Charset:

byte[] bytes = str.getBytes(StandardCharsets.ISO_8859_1); 

5 Comments

Thank you very much. The problem is that your example made me myBytes[] = {K, I, T, T, E, N}, I need exactly myBytes[] = {'K', 'I', 'T', 'T', 'E', 'N'}, any idea?
What do you mean with that? 'K' is simply the readable representation of character at position 75 in the ISO8859-1 character set (U+0075 in Unicode). In your byte array, you always have signed 8 bit values - to print them they even need to be cast to char (what I do in the System.out.println() above). Otherwise you would get the output as numbers, like 75 73 84 84 69 78.
All I want is to get byte array like that: byte[] display = new byte[] { 0x00, 0x10, 'K', 'I', 'T' }; because in your solution I don't have a char acrually and I can't display it on the screen.
I don't get it :) How can your input String be transformed into this byte array at all? Maybe you should be more explicit about what you want to achieve. In any case, always be aware of the internal representation of numbers (like signed byte with a range of -128 to 127) and how the number is represented. The byte with the value 32 can really mean the number 32 (in which case you can for example divide it by 2 and get 16), or by mapping it through some character set it can mean a space character, or it can even be a pixel color.
I edited my question, maybe now it will be more clear? Check it please.
1

byte is a keyword so you can't use it as a variable name:

byte[] byte = new byte[] { 'K', 'I', 'T', 'T', 'E', 'N'}; 

You could use:

byte[] myBytes = new byte[] { 'K', 'I', 'T', 'T', 'E', 'N'}; 

Anyway to do a straight conversion from String str you could do:

byte[] myBytes = str.getBytes(); 

5 Comments

Thank you very much. The problem is that your example made me myBytes[] = {K, I, T, T, E, N}, I need exactly myBytes[] = {'K', 'I', 'T', 'T', 'E', 'N'}, any idea?
A byte can hold only one character. Do you want to display quotes? the last 2 byte array examples are equivalent.
I know that, problem is diffrent. Displaying byte[] display = new byte[] { 0x00, 0, 0, 'K', 'O', 'K', 'O', 'S'}; works, and displaying byte[] display = new byte[] { 0x00, 0, 0, myBytes[0], myBytes[1], myBytes[2] }; doesn't work. I try to display it on the screen of my phone by NFC. Do you know what's make the problem?
Both are valid byte arrays. It looks like a display issue now. I would suggest perhaps a new posting. :)
@Reimeus Very valid suggestion. Doszi89, I suggest that you take some time, rethink your issue while considering the input from the posts here, and then post a new more detailed question.
0

The projection: char to byte is invalid due to the information loss. A char is an Unicode character which has encoding information. An byte is a number. Without the encoding, the number of the byte has no meaning.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.