Nico
Nico

Reputation: 1803

byte[] to String not working correctly

Now from the previous question I got the NTLM handshake working. Now when I convert from the byte[] to a String I can't filter out the whitespaces. My result looks like this:

CHAR: A
CHAR:
CHAR: B
CHAR: 
CHAR: C

USERNAME: A B C

And this is the code producing this output:

username = new String( token, offset, length, "ISO-8859-1" );
username = username.trim();

char[] test = username.toCharArray();
for ( char t : test )
{
  if ( !Character.isWhitespace( t ) )
  {
    System.out.println( "CHAR: " + t );
  }
}
System.out.println( "USERNAME: " + username );

I even checked with String.valueOf(t).isEmpty() or String.valueOf(t).equals(" "). All the time it seems to be not the case and the chars are printed. I even used all at once with || but everyone is really "correct".

I get the Input like this from another class:

String auth = httpServletRequest.getHeader( "Authorization" );
String username = authService.getUserNameFromNTLM( auth.substring( 5 ));

and convert the String to an byte[] like this:

byte[] token = Base64.getDecoder().decode( msg );

The output I need is ABC. Can somebody tell me why my procedure is wrong?

Upvotes: 0

Views: 172

Answers (1)

Henry
Henry

Reputation: 43738

The username in the message is encoded in UTF-16LE rather than ISO-8859-1.

What you currently see in the string are NUL characters originating from the wrong decoding.

Upvotes: 1

Related Questions