Reputation: 1793
1.There is some data in String:
String data = "some......";
2.And use MD5 covert it into bytes:
byte [] result = MD5.toMD5(data);
3.Now I encode it into String:
String encodeString = new String(result,"ISO-8895-1");
4.And then decode it to bytes:
byte [] decodeBytes = encodeString.getBytes("ISO-8859-1");
Here my question is : Will the decodeBytes
be equal to result
?
My confusion is whether there will be Zero
in result
and will it cause truncation in Step3 ?
If there is any problem to let decodeBytes
equal to result
, and if I limit the data type of String in Step1 such as just allow Letters and Numbers , will the problem can be avoid?
Upvotes: 1
Views: 423
Reputation: 31300
There is no reason why a byte value should not be decoded to a character if ISO-8859-1, which is a 8 bit character code. Although 65 code points (for control characters) are not included, the String methods handle these as if the conrols defined in ISO/IEC 6429 were part of that character set.
Round tripping byte values 0 to 255 works perfectly, also with byte[].
byte[] bs = new byte[256];
String encode() throws Exception {
return new String( bs, "ISO-8859-1" );
}
byte[] decode( String s ) throws Exception{
return s.getBytes( "ISO-8859-1" );
}
void set(){
for( int i = 0; i < bs.length; ++i ){
bs[i] = (byte)i;
}
}
boolean cmp( byte[] x ){
for( int i = 0; i < bs.length; ++i ){
if( bs[i] != x[i] ){
System.out.println( i + ": " + bs[i] + " != " + x[i] );
return false;
}
}
return true;
}
void round() throws Exception{
String s = encode();
if( s.length() != 256 ) throw new IllegalStateException();
byte[] res = decode( s );
if( ! cmp( res ) ) System.out.println( "false" );
}
}
Upvotes: 2