A
Andrew
Hello. I have a general question in regards to String.getBytes and
toString() methods (in this case when applied to a byte array).
I have the following code:
byte[] syncML ={
(byte) 0x0002, (byte) 0x01, (byte) 0x006a, (byte) 0x0000,
(byte) 0x006d, (byte) 0x006c
, (byte) 0x0071,(byte) 0x0003, (byte) '1', (byte) '.', (byte) '1',
(byte) 0x0000,(byte) 0x0001,
(byte) 0x0072, (byte) 0x0003, (byte) 'S', (byte) 'y',
(byte) 'n',
(byte) 'c', (byte) 'M', (byte) 'L', (byte) 0x0000,
(byte) 0x0001 ,
(byte) 0x0001,(byte) 0x0001};
String str = syncML.toString();
byte[] bytesafterString = str.getBytes();
System.out.println(syncML.length + " " +
bytesafterString.length);
I realised that the result is not as I was expecting. In fact, the
lengths of the two byte arrays were not identical !!!! Result was:
25 9
How can a byte array been converted to a string and then back to an
array and be different ????????????????????? I really cant understand
this !!!! Shouldnt it be the same ?? If no, how can I make sure my
data is exactly the same before and after string conversion ?
Many thanks in advance
Andrew
toString() methods (in this case when applied to a byte array).
I have the following code:
byte[] syncML ={
(byte) 0x0002, (byte) 0x01, (byte) 0x006a, (byte) 0x0000,
(byte) 0x006d, (byte) 0x006c
, (byte) 0x0071,(byte) 0x0003, (byte) '1', (byte) '.', (byte) '1',
(byte) 0x0000,(byte) 0x0001,
(byte) 0x0072, (byte) 0x0003, (byte) 'S', (byte) 'y',
(byte) 'n',
(byte) 'c', (byte) 'M', (byte) 'L', (byte) 0x0000,
(byte) 0x0001 ,
(byte) 0x0001,(byte) 0x0001};
String str = syncML.toString();
byte[] bytesafterString = str.getBytes();
System.out.println(syncML.length + " " +
bytesafterString.length);
I realised that the result is not as I was expecting. In fact, the
lengths of the two byte arrays were not identical !!!! Result was:
25 9
How can a byte array been converted to a string and then back to an
array and be different ????????????????????? I really cant understand
this !!!! Shouldnt it be the same ?? If no, how can I make sure my
data is exactly the same before and after string conversion ?
Many thanks in advance
Andrew