aspose file tools*
The moose likes Java in General and the fly likes how can i convert char [] to unsigned char [] to connect to an app writen in 'C' Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "how can i convert char [] to unsigned char [] to connect to an app writen in Watch "how can i convert char [] to unsigned char [] to connect to an app writen in New topic
Author

how can i convert char [] to unsigned char [] to connect to an app writen in 'C'

Pedro Neves
Ranch Hand

Joined: Mar 07, 2012
Posts: 64
Hello, i'm having some trouble in getting a string from a user and send it to a socket from the client (java) to the server (C). The thing is that as long as i know, C use unsigned chars and i'm sending chars. How can i send my string (already separated in a [] char) to the server?


public void write(String s)
{

boolean flag2=true;
Scanner sc = new Scanner(System.in);
String ss= new String();

try{
do
{

ss = sc.nextLine(); // get the string from user

char []stringChar = new char[ss.length()]; //creates a [] char --------> but i think i would have to create an unsigned char array.

for(int i=0 ; i < ss.length() ; i++)
stringChar[i] = ss.charAt(i); //passes the chars one by one to the array


Socket skt = new Socket(s, 12345); //open a connection
PrintStream ps = new PrintStream(skt.getOutputStream()); //printstream to wrinte in the socket

ps.print(stringChar);
ps.flush();


}while(flag2);

} catch(Exception e) { System.out.print("erro de connecção\n");}

}



Thanks in advance
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8

I think that the difference is more likely to be that the "chars" in C are unsigned 8-bit units whereas the "chars" in Java are unsigned (yes, unsigned) 16-bit units. It's a design difference: C really doesn't care what you put in those 8-bit units under the name of "char" whereas Java really intends for your "chars" to be something which is suitable for text which can be read by some human. Also, C was written at a time when ASCII was the only charset you needed to know about and those people who used strange letters like ç and ã weren't going to be using your program.

Probably what you really need to do is to encode the string; that's a standard process which Java uses to convert sequences of chars (such as String) to sequences of bytes. However you need to choose an encoding, which means you have to know what the user of the bytes is going to expect. Common encodings are ISO-8859-1 and its relatives, UTF-8, windows-1252 and its relatives.

In other words your question is probably not a question about how (or whether) to set the sign bit on certain bytes, but a question about data requirements.
Pedro Neves
Ranch Hand

Joined: Mar 07, 2012
Posts: 64
Paul Clapham wrote:I think that the difference is more likely to be that the "chars" in C are unsigned 8-bit units whereas the "chars" in Java are unsigned (yes, unsigned) 16-bit units. It's a design difference: C really doesn't care what you put in those 8-bit units under the name of "char" whereas Java really intends for your "chars" to be something which is suitable for text which can be read by some human. Also, C was written at a time when ASCII was the only charset you needed to know about and those people who used strange letters like ç and ã weren't going to be using your program.

Probably what you really need to do is to encode the string; that's a standard process which Java uses to convert sequences of chars (such as String) to sequences of bytes. However you need to choose an encoding, which means you have to know what the user of the bytes is going to expect. Common encodings are ISO-8859-1 and its relatives, UTF-8, windows-1252 and its relatives.

In other words your question is probably not a question about how (or whether) to set the sign bit on certain bytes, but a question about data requirements.



Humm i see, i'll try to change the change the encoding then
Pedro Neves
Ranch Hand

Joined: Mar 07, 2012
Posts: 64
Pedro Neves wrote:
Paul Clapham wrote:I think that the difference is more likely to be that the "chars" in C are unsigned 8-bit units whereas the "chars" in Java are unsigned (yes, unsigned) 16-bit units. It's a design difference: C really doesn't care what you put in those 8-bit units under the name of "char" whereas Java really intends for your "chars" to be something which is suitable for text which can be read by some human. Also, C was written at a time when ASCII was the only charset you needed to know about and those people who used strange letters like ç and ã weren't going to be using your program.

Probably what you really need to do is to encode the string; that's a standard process which Java uses to convert sequences of chars (such as String) to sequences of bytes. However you need to choose an encoding, which means you have to know what the user of the bytes is going to expect. Common encodings are ISO-8859-1 and its relatives, UTF-8, windows-1252 and its relatives.

In other words your question is probably not a question about how (or whether) to set the sign bit on certain bytes, but a question about data requirements.



Humm i see, i'll try to change the change the encoding then



I tried to change the encoding and i still dont get the expected result:


String ss= new String();

do
{

ss = sc.nextLine(); // get the string from user


byte[] utf8Bytes = ss.getBytes("ISO-8859-1"); // i tried even with utf8
String enviar = new String(utf8Bytes, "ISO-8859-1");


Socket skt = new Socket(s, 12345); //open a connection
PrintStream ps = new PrintStream(skt.getOutputStream()); //printstream to wrinte in the socket

System.out.print(enviar);

ps.print(enviar);
ps.flush();


}while(flag2);

What should i do? I think the UTF8 is indeed the choice to comunicate with c app but it seem that that does not work properly :-/
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

You shouldn't be using the PrintStream's print method - which is for writing characters and relies on your system character encoding. If the system encoding is different than what C expects than what you want to do will break. Instead, take the raw bytes you have in utf8Bytes and send them via the PrintStream's write(byte[] buffer, int length, int offset) method. Alternatively, since you can't really make use of the PrintStream's main functionality, perhaps using a different Stream will be easier.


Steve
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8

So you created an array of bytes from the string, as I suggested. Next step should be to send that array of bytes over the network. But you didn't do that. You did this:

which converts the array of bytes back to a string. Why did you do that?

And why are you using a PrintStream to send your data? You already have an OutputStream which you get from the socket connection, why not just use that?
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

Pedro,

You seem to be missing a critical piece of Internet information. What you send over a socket is an octet, specifically an unsigned 8 bit byte. There is no exact match in Java, but usually an array of byte works fine. Most low level protocols don't pay too much attention to the unsigned part.

Java Strings have nothing to do with bytes. Characters in a Java String are at last 16 bits long.

You have to send and receive arrays of byte. Not arrays of Byte, and not Strings.
Pedro Neves
Ranch Hand

Joined: Mar 07, 2012
Posts: 64
Pat Farrell wrote:Pedro,

You seem to be missing a critical piece of Internet information. What you send over a socket is an octet, specifically an unsigned 8 bit byte. There is no exact match in Java, but usually an array of byte works fine. Most low level protocols don't pay too much attention to the unsigned part.

Java Strings have nothing to do with bytes. Characters in a Java String are at last 16 bits long.

You have to send and receive arrays of byte. Not arrays of Byte, and not Strings.



Hello to all and thank you for your participation, i've finally resolved the problem with a byte Array but i still have one major problem


the thing is when the user type this command:

102 001 000 016 000 // as a string

i separate it like this

ss = sc.nextLine();
Str = ss.split(" ");


if i do something like this:

byte [] ArrayByte = new byte[Str.length];

for(int i=0 ; i < Str.length ; i++)
ArrayByte[i] = Byte.valueOf(Str[i]);

then a i can now send the ArrayByte over the socket and it works!

The problem is when the user type the command:

102 255 255 016 000

it throws an exception when it tries to Byte.valueOf(Str[i]) when Str[i] == 255. I'm dont understand this.. 255 are 8 bits (so, logicaly, a byte). why should eclipse throw an exception and how can i fix this, because the other commands are all working! ??

Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

Java Byte objects and the primitive 'byte' type are signed.
You can't put 255 in it.
the first bit is the sign bit.

max value is 127
and min value is -128
Pedro Neves
Ranch Hand

Joined: Mar 07, 2012
Posts: 64
Pat Farrell wrote:Java Byte objects and the primitive 'byte' type are signed.
You can't put 255 in it.
the first bit is the sign bit.

max value is 127
and min value is -128


Hum i see, do you have any idea how to solve the problem? :-/
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8

So what you do is something like this:

Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

What is your problem, exactly? You have to stop thinking that you can use Byte values.

Why are you trying to send a byte with the value 255?
Pedro Neves
Ranch Hand

Joined: Mar 07, 2012
Posts: 64
Paul Clapham wrote:So what you do is something like this:




Thank you man.. Thats it ;). I apologize if i'm a little "noob" but i'm learning java, and i'm in a project witch i decided to program in java to learn a bit (i'm learning by my self), but i didn't notice i would have all this "low level" problems.

Really. Thanks to all
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4659
    
    5

Paul Clapham wrote:


Which will, quite properly, give a warning about loss of data. It happens that @paul's example is no problem, but if the code was really



The result would not be '301'
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how can i convert char [] to unsigned char [] to connect to an app writen in 'C'