Results 1 to 13 of 13
Thread: Converting string to byte[]
- 09-10-2010, 08:55 AM #1
Member
- Join Date
- Sep 2010
- Posts
- 37
- Rep Power
- 0
- 09-10-2010, 09:01 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 09-10-2010, 09:09 AM #3
Member
- Join Date
- Sep 2010
- Posts
- 37
- Rep Power
- 0
It just concatenate three byte array.here's the code:
private static byte[] ConcatBytes(byte[] a1e, byte[] a2e, byte[] a3e) {
byte concatenatedBytes[] = new byte[a1e.length + a2e.length + a3e.length];
for (int i=0; i<a1e.length; i++)
concatenatedBytes[i] = a1e[i];
for (int j=0; j<a2e.length; j++)
concatenatedBytes[j+a1e.length] = a2e[j];
for (int j=0; j<a3e.length; j++)
concatenatedBytes[j+a1e.length+a2e.length] = a3e[j];
return concatenatedBytes;
}
I convert Byte[] M1e to String and then convert it to Byte[] again, but the result isn't equal with M1e.
What I have to do?
- 09-10-2010, 10:26 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
I'd say don't convert your byte[] to a String and back again. The conversion to a String (internally a char[]) decodes the byte array and when you convert that String back again to a byte[] probably another (non-matching) encoding is used. If you insist in doing byte[] --> String --> byte[] both then encoding and decoding algorithms must match. Read the API documentation for the String class; it has methods that allow you to specify the en/decoding; utf-8 is a good candidate.
kind regards,
Jos
- 09-10-2010, 01:39 PM #5
I found that UTF-8 was symmetrical(what goes in comes out) up to 0X7F.
ISO-8859-1 was symmetrical past 0x7F
Java Code:// Test if String can hold 256 different values from a byte array import java.nio.charset.*; public class TestByteToString { public static void main(String args[]) { byte[] all256 = new byte[256]; // fill array with 0-255 for(int i=0; i < 256; i++) { all256[i] = (byte)i; } Charset charset = Charset.forName("ISO-8859-1"); //<<< this gives 100% equality String aStr = new String(all256, charset); int cnt = 0; for(int i=0; i < 256; i++) { if(i != aStr.charAt(i)) { System.out.println("not equal i=" + i + ", char=" + Integer.toHexString(aStr.charAt(i))); }else{ cnt++; } } // end for(i) System.out.println("matched OK for cnt=" + cnt); } }Last edited by Norm; 09-10-2010 at 01:42 PM.
- 09-10-2010, 03:59 PM #6
Member
- Join Date
- Sep 2010
- Posts
- 37
- Rep Power
- 0
thank u, but my array size is 384...
I also use utf-8, but it doesn't work right.
byte[] M1e = ...;
String msg = new String(M1e , "utf-8");
System.out.println(msg.getBytes("utf-8"));
I have a byte array in the client side, and want to send it to server..That's why I'm converting it to string and at the server side I have to convert it to byte array again..
regards,
- 09-10-2010, 04:02 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 09-10-2010, 04:05 PM #8
Not relevant. Its the contents that matters. My array has 256 elements that hold the values from 0 to 255. My test was to see if what went in (values 0-255) came out with the same value. It did for ISO-8859-1. It does NOT using UTF-8 for bytes > 0x7Fbut my array size is 384...
What is the range of values in your String? Are any higher than 0XFF in value? If they are, then the number of bytes required to hold the String will be greater than the String length.
- 09-10-2010, 04:37 PM #9
Member
- Join Date
- Sep 2010
- Posts
- 37
- Rep Power
- 0
I sent byte array but in the sever I receive string..
how to receive byte??
here's my server code:
String x = "";
ServerSocket srvr = new ServerSocket(4444);
Socket skt = srvr.accept();
System.out.print("Server has connected!\n");
BufferedReader in = new BufferedReader(new InputStreamReader(skt.getInputStream()));
while (!in.ready()) {}
x = in.readLine();
Regards,
- 09-10-2010, 04:42 PM #10
Member
- Join Date
- Sep 2010
- Posts
- 37
- Rep Power
- 0
I don't know the values because my array is the output of my encryption function.
here's my encryption function:
public byte[] RSAEncrypt(byte[] data ,String filename) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException{
PublicKey pubKey = readKeyFromFile("pub" + filename + ".key");
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
byte[] cipherData = cipher.doFinal(data);
return cipherData;
}
- 09-10-2010, 05:36 PM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 09-10-2010, 05:39 PM #12
If you look at the type of the array(byte[] cipherData), you will see it is byte. The max value of a byte is 255.I don't know the values because my array ...
- 09-10-2010, 09:10 PM #13
Member
- Join Date
- Sep 2010
- Posts
- 37
- Rep Power
- 0
Similar Threads
-
converting byte array to bmp file
By Moorkh in forum New To JavaReplies: 2Last Post: 09-07-2010, 02:58 PM -
Need help converting int to a 4 byte array
By kook04 in forum Advanced JavaReplies: 5Last Post: 02-26-2010, 08:59 PM -
Converting Image to byte array[] ?
By afflictedd2 in forum CLDC and MIDPReplies: 0Last Post: 04-11-2009, 11:33 PM -
Converting a byte[] into Sound
By savage82 in forum Advanced JavaReplies: 1Last Post: 11-21-2007, 11:57 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks