Results 1 to 9 of 9
  1. #1
    sachinj13 is offline Member
    Join Date
    Aug 2008
    Posts
    9
    Rep Power
    0

    Default writting extended ascii chars on socket........or Endianness Issue......??

    Dear All,

    This is small problem i am facing while writting a some extended ascii characters( range between 128 -160 vendor dependent representation of characters) on socket.

    When i write them as HEX 80 the other side program which is in C++ and on unix m/c read it as HEX 3F
    The unix side program is in C++. Conversion is like this

    128 Dec 80 Hex 10000000

    63 Dec 3F Hex 00111111

    below code I am using..
    Java Code:
    String Data =new String ((char)128);
    InetSocketAddress sockAddr = 
    new InetSocketAddress("1.1.1.1",50071);
    Socket socket = new Socket(); 
    socket.connect(sockAddr,100000); 
    BufferedReader rd1 =
    new BufferedReader(new InputStreamReader(socket.getInputStream())); 
    String[] codePages =
     {"CP437", "CP737", "CP775", "CP850",
    "CP852", "CP855", "CP857", "CP860",
    "CP861", "CP862", "CP863", "CP864",
    "CP865", "CP866", "CP869", "CP874",
    "CP856","CP858","CP868","CP870"};
    BufferedWriter wr1 =
     new BufferedWriter(new OutputStreamWriter
    (socket.getOutputStream(),codepage[0])); 
    wr1.write(data); 
    wr1.flush();

    As I searched java is always Big Endian and other side program might be Little Endian ByteOrder may be different.So this might be the reason .

    Or the issue is related to character set issue i.e code pages supported by platform.

    But i tried with all above character sets and its still not giving proper result..

    Does Java NIO will solve the problem but never worked with it ??

    Does Stream or Writer classes has to do something with it...???

    Thanx ....All...

    .. Sachin ..

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    Writers often do char conversions correctly, but do not address the endian issue. You are mixing Streams and Writers, that cannot be good. Possibly we are getting byte to char conversions rather than endianess being at issue.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,318
    Rep Power
    25

    Default

    String Data =new String ((char)128);
    How do you compile this?

  4. #4
    sachinj13 is offline Member
    Join Date
    Aug 2008
    Posts
    9
    Rep Power
    0

    Default

    sorry its like
    Java Code:
    char c[]={(char)128,(char)129,....};
    String data=new String(c);

  5. #5
    sachinj13 is offline Member
    Join Date
    Aug 2008
    Posts
    9
    Rep Power
    0

    Default

    Hi Nicholas,,
    Can you plz elaborate on the same or
    Where I am actually doing mistake....

    Thanx...
    Sachin

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,318
    Rep Power
    25

    Default

    The 3F is a ?. Different encodings use that when they do NOT have a valid char to use.
    Here's a quick demo program that shows how different encodings generate different results:
    Java Code:
    // Writing chars > ASCII or ???
    
    import java.io.*;
    
    public class Test8 {
    
       public static void main(String[] args) {
          String data =new String("\u00a7\u0080");
          // \u0080 written to file as 3F (?) with encoding CP437
          // \u0080 written to file as 3F (?) with encoding CP1254
          // \u0080 written to file as 80  with encoding 8859_1
    
          System.out.println("data=" + data); //data=?
          String[] codePages = {// "8859_1", "CP1254", //NCR added these
                     "CP437", "CP737", "CP775", "CP850",
                     "CP852", "CP855", "CP857", "CP860",
                     "CP861", "CP862", "CP863", "CP864",
                     "CP865", "CP866", "CP869", "CP874",
                     "CP856", "CP858", "CP868", "CP870"
                    };
    
          try {
             FileOutputStream fos = new FileOutputStream("Text8Output.txt"); // write to file
             BufferedWriter wr1 = new BufferedWriter(
                                    new OutputStreamWriter(fos, codePages[0])); 
    //java.io.UnsupportedEncodingException: Cp437       with 1.4.2  OK with 1.6.0
             wr1.write(data); 
             wr1.flush();
             wr1.close();
          }catch(Exception x) {
             x.printStackTrace();
          }
       } // end main()
    } // end class

  7. #7
    sachinj13 is offline Member
    Join Date
    Aug 2008
    Posts
    9
    Rep Power
    0

    Default

    Hi Norm,,

    I tried all char sets..or Code pages around(130)....
    But still the same problem persist....
    And tried with all unicodes also...

    As i tried OutputStream also and not writer...

    Does byteorder or Endianness is causing this???

    As my client socket is on java and server is on C++.....

    Thanx...
    Sachin...

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,318
    Rep Power
    25

    Default

    Did you try 8859_1? It works in the sample code I posted.
    If the char maps/encodes to a single byte, the Endianness is not relevant. one byte is one byte.

  9. #9
    sachinj13 is offline Member
    Join Date
    Aug 2008
    Posts
    9
    Rep Power
    0

    Default

    Thanx Norm,,,

    Its workin now....

    Thank u very much.....

    Sachin......

Similar Threads

  1. ASCII to EBCDIC conversion
    By satish kumar in forum Advanced Java
    Replies: 3
    Last Post: 06-14-2013, 01:50 PM
  2. Can we append more than 255 chars to <a href>??
    By freddieMaize in forum Advanced Java
    Replies: 22
    Last Post: 07-18-2008, 04:04 PM
  3. Getting ASCII codes of character
    By gapper in forum New To Java
    Replies: 1
    Last Post: 02-02-2008, 09:42 AM
  4. abstract extended and hiding variable??
    By Ace_Of_John in forum New To Java
    Replies: 5
    Last Post: 12-23-2007, 02:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •