Code:
public String stringToHex( String base )
{
StringBuffer buffer = new StringBuffer();
int intValue;
for( int x = 0; x < base.length(); x++ )
{
intValue = base.charAt( x );
String hex = Integer.toHexString( intValue );
if( hex.length() == 1 )
{
buffer.append( "0" + hex + " " );
}
else
{
buffer.append( hex + " " );
}
}
return buffer.toString();
}
Code:
public void run ( InputStream serialportInputStream )
{
byte[] buffer = new byte[1024];
int len = -1;
try
{
while ( ( len = serialportInputStream.read(buffer)) > -1 )
{
System.out.println( "Raw: " + stringToHex( new String(buffer,0,len) ) );
}
}
catch ( Exception e )
{
e.printStackTrace();
}
}
My serial device writes the port of my computer the following data (in hex):
10 01 01 ff 24 00 00 00 0e 55 b5 12 34 56 78 90 00 00 00 00 00 00 88 f1 16 10 02
But why my java application is printing this to a console:
Raw: 10 01 01 fffd 24 00 00 00 0e 55 fffd 12 34 56 78 fffd 00 00 00 00 00 00 fffd fffd 16 10 02
(and many times 00 00 00 00 00 00 ... because new byte[1024])
So why ff, b5, 90, 88 and f1 is converted into fffd ?
Is this some common unicode problem? How can I fix it.
From ht tp://w ww.fileformat.info/info/unicode/char/fffd/index.htm
Unicode Character 'REPLACEMENT CHARACTER' (U+FFFD)
used to replace an incoming character whose value is unknown or unrepresentable in Unicode
compare the use of U+001A as a control character to indicate the substitute function
So why this java code is trying to convert character into UTF-8 and how I can avoid this?
-
-
Last edited: