Converting a set of strings to a byte[] array?

Using Byte. ParseByte may help making your second snippet work But, unless you have some specific reason to use that kind of representation, I'd encode strings to byte arrays using the Java methods mentioned in other answers public static byte convertStr(String ln) { System.out. Println(ln); String st = ln.

Split(" * "); byte byteArray = new byte23; for(int I = 0; I.

Using Byte. ParseByte may help making your second snippet work. But, unless you have some specific reason to use that kind of representation, I'd encode strings to byte arrays using the Java methods mentioned in other answers.

Public static byte convertStr(String ln) { System.out. Println(ln); String st = ln. Split(" * "); byte byteArray = new byte23; for(int I = 0; I Length; i++) { byteArrayi = Byte.

ParseByte(sti); } return byteArray; }.

This should work: byte bytes = " GetBytes("UTF-8"); String hello = new String(bytes, "UTF-8"); The above example uses the UTF-8 encoding and just serves as an example. Use the character encoding you expect in your message input.(This 'answer' wasn't an answer to the question...) Edit So we need a conversion from byte to String and back to byte. Me123 added delimiters between the (and in front of) the values.As others already explained, 1.

The regexp for the split has to be " \\* " and 2. The magic method is Byte. ParseByte(sti) Here is an alternative without using a delimiter but a fixes width for the byte entries.

The StringToByte converter shows a pretty fast solution just based on the strings char array. Public static String convertByte(byte msg) { StringBuilder sb = new StringBuilder(); for (byte b:msg) { sb. Append(String.

Format("%02x", b)); } return sb.toString(); } public static byte convertStr(String ln) { System.out. Println(ln); char chars = ln.toCharArray(); byte result = new byteln.length()/2; for (int I = 0;i Length; i++) { resulti = (byte) hexToInt(chars2*i, chars2*i+1); } return result; } private static int hexToInt(char c1, char c2) { return ((c1.

He wants to convert a set of Strings into a byte array, not a single String. – Fortega Jan 13 '10 at 14:54 1 No, this will not work since he's using his own function to convert the byte array to a string, e.g. {0, 1, 2, 3} -> "0 * 1 * 2 * 3 * " – jarnbjo Jan 13 '10 at 15:17 Stupid me, yes you're right - correct answer for a totally different problem ;) thx for the hint. – Andreas_D Jan 13 '10 at 19:08.

I don't understand why you are trying to get those bytes by characters. GetBytes() and its variants will give you a byte array for whole string at once. However, if you want to see how characters are encoded, your approach my be good, but you have to keep in mind, that one character could be encoded in e.g. One to four bytes in some encodings, thus you need a byte array for every character.

If you are certain the String will be small enough to fit in one byte, you could do sti.getBytes()0; however, in most of the cases, your String will probably be bigger, so in these cases it is not possible...

If you are sure that the string contains 1 byte, you can do: byteArrayi = sti.getbytes()0.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions