|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

09-22-2008, 01:48 PM
|
|
Member
|
|
Join Date: Aug 2008
Posts: 9
|
|
|
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..
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 ..
|
|

09-22-2008, 03:41 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Southwest
Posts: 789
|
|
|
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.
__________________
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. .
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
|
|

09-22-2008, 04:50 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
|
|
String Data =new String ((char)128);
How do you compile this?
|
|

09-22-2008, 06:03 PM
|
|
Member
|
|
Join Date: Aug 2008
Posts: 9
|
|
sorry its like
char c[]={(char)128,(char)129,....};
String data=new String(c);
|
|

09-22-2008, 06:05 PM
|
|
Member
|
|
Join Date: Aug 2008
Posts: 9
|
|
|
Hi Nicholas,,
Can you plz elaborate on the same or
Where I am actually doing mistake....
Thanx...
Sachin
|
|

09-22-2008, 08:12 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
|
|
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:
// 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
|
|

09-23-2008, 08:38 AM
|
|
Member
|
|
Join Date: Aug 2008
Posts: 9
|
|
|
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...
|
|

09-23-2008, 03:52 PM
|
 |
Senior Member
|
|
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,223
|
|
|
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.
|
|

09-23-2008, 04:20 PM
|
|
Member
|
|
Join Date: Aug 2008
Posts: 9
|
|
|
Thanx Norm,,,
Its workin now....
Thank u very much.....
Sachin......
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|