ASCII characters in a string gets converted, why?

Joined
Dec 18, 2008
Messages
2
Reaction score
0
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:
Joined
Dec 18, 2008
Messages
2
Reaction score
0
Solved, but I still want explanation

Run yourself and see the problem and solution:
Code:
public class Main
{
  public static 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();
  }

  static public String byteToHex( byte[] b )
  {
    String returnString = "";
    for ( int i = 0; i < b.length; i ++ )
    {
      returnString += (char)(b[i] & 0xff);
    }
    return returnString;
  }

  public static void main( String[] args )
  {
    try
    {
      String test1 = "";
      test1 += (char)0x01;
      test1 += (char)0x22;
      test1 += (char)0x44;
      test1 += (char)0x88;
      test1 += (char)0xCC;
      test1 += (char)0xFE;
      test1 += (char)0xDA;

      System.out.println( stringToHex( test1 ) );

      byte[] test2 = new byte[7];
      test2[0] += 0x01;
      test2[1] += 0x22;
      test2[2] += 0x44;
      test2[3] += 0x88;
      test2[4] += 0xCC;
      test2[5] += 0xFE;
      test2[6] += 0xDA;

      System.out.println( stringToHex( new String( test2, 0, test2.length ) ) );
      System.out.println( stringToHex( byteToHex( test2 ) ) );
    }
    catch ( Exception e )
    {
      e.printStackTrace();
    }
  }
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top