commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Neal Johnson <>
Subject Re: Base64 encoding problem
Date Wed, 14 Jun 2006 08:26:27 GMT
I have had a similar problem when using strings in this way, The way  
I got around this was to use a to specify the specific encoding when  
constructing the string object, by default the constructer will use  
the operating system character encoding witch can very dramatically  
even though it supposed to be ascii, According to the API reference  
for java 1.4 the byte constructer for string does: "Constructs a new  
String by decoding the specified array of bytes using the platform's  
default charset."  This is what gave me the problem I used the "String 
(byte[] bytes, String charsetName)" costructer instead and translated  
the bytes into the correct encoding for each platform, this worked  
for windows, linux and  osx

On 14 Jun 2006, at 09:10, wrote:

> Selon Priti Menon <>:
> Could you try to remove the encoding/decoding part and performing  
> directly:
>  String decodedString = new String(s.getBytes());
> and to compare the hash codes ?
> If this fails, then it would mean the special character is not  
> valid in the
> default encoding of your Linux box but valid in the (probably  
> different)
> encoding of your Windows box.
> The javadoc for the constructor String(byte[]) says:
>   The behavior of this constructor when the given bytes are not valid
>   in the default charset is unspecified.
> In this case, a possible workaround would be to explicitely specify  
> an encoding
> for which the special character is valid instead of relying on the  
> default
> charset of the system.
> Luc
>> Hi All,
>> I am facing a problem with the Base64 encoding class in the  
>> commons package.
>> i.e. org.apache.commons.codec.binary.Base64
>> I am trying to encode & decode a string back to its original  
>> value. I have
>> tried this on windows & linux.
>> If you compare the hashcodes of the original string with the new  
>> string, it
>> returns different values with Linux.
>> For eg.
>> String s = "Matrix ";
>> System.out.println("s = " + s.hashCode());
>> String s1 = new String(Base64.encodeBase64(s.getBytes()));
>> String decodedString = new String(Base64.decodeBase64(s1.getBytes 
>> ()));
>> System.out.println("decodedString = " + decodedString.hashCode());
>> The output on windows :-
>> s = Matrix
>> s = -1789003553
>> decodedString = -1789003553
>> The output on linux :-
>> s = Matrix?
>> s = -1789003553
>> decodedString = -1789003650
>> Has anyone faced this problem before ?
>> Thanks & Regards,
>> Priti
>> PS. There is a special character in the string after the word  
>> 'Matrix'.
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail:
>> For additional commands, e-mail:
> ---------------------------------------------------------------------
> To unsubscribe, e-mail:
> For additional commands, e-mail:
> ______________________________________________________________________
> This email has been scanned by the MessageLabs Email Security System.
> For more information please visit
> ______________________________________________________________________

LEGAL NOTICE:This e-mail has been sent to you by DataActive
Communications Ltd. This communication, and the information it
contains, is intended for the person(s) or organisation(s) to whom it
is addressed.  Its contents are confidential and may be protected in
law. Unauthorised use, copying or disclosure of any of it may be
unlawful.  If you are not the intended recipient, please notify the
sender by replying with 'Received in error' as the subject and then
delete it from your mailbox. Any views or opinions presented are
solely those of the author and do not necessarily represent those of
DataActive Communications Ltd.  Although this e-mail and any
attachments are believed to be free of any virus or other defects
which might affect any computer or IT system into which they are
received, no responsibility is accepted from DataActive Communications
Ltd, or any of its associated companies for any loss or damage
arising in any way from the receipt or use thereof. We recommend that
you should carry out your own virus checking procedure before opening
any attachment. 

This email has been scanned by the MessageLabs Email Security System.
For more information please visit 
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message