Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String to Bytes

 
Pedro Neves
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, i'm experiencing a problem that you might think its a litle stupid but i'm kind a new in Java. :-/

in this





I have one major problem. the code is doing fine but the problem is when one of Str[j] value is higher than 127. as far as i know, bytes in java goes from -126 to 127 right? how can come around this problem?

Many thanks

 
Manoj Kumar Jain
Ranch Hand
Posts: 198
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First thing I would like to mention is that bytes ranges from -128 to 127.
I am not sure but I think you can use the method writeInt()
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pedro Neves wrote:I have one major problem. the code is doing fine but the problem is when one of Str[j] value is higher than 127. as far as i know, bytes in java goes from -126 to 127 right? how can come around this problem?

My advice: Don't try. Have a look at the following:
1. ByteArrayOutputStream.
2. String.getBytes().

Between the two of them, you should be able to do what you want without all that array processing.

Winston
 
Pedro Neves
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Pedro Neves wrote:I have one major problem. the code is doing fine but the problem is when one of Str[j] value is higher than 127. as far as i know, bytes in java goes from -126 to 127 right? how can come around this problem?

My advice: Don't try. Have a look at the following:
1. ByteArrayOutputStream.
2. String.getBytes().

Between the two of them, you should be able to do what you want without all that array processing.

Winston



well as far as i could i made this:




but it also does not work.

I have to separate this: "102 1 000 019 000 003 000 033 000 002 000"

to several bytes, each number is a byte. the app whitch i'm comunicating to, is expecting bytes, the output from the code i made now is.

49 48 50 32 49 32 48 48 48 32 48 49 57 32 48 48 48 32 48 48 51 32 48 48 48 32 48 51 51 32 48 48 48 32 48 48 50 32 48 48 48

I think that it isnt correct :-/
 
Pedro Neves
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Manoj Kumar Jain wrote:First thing I would like to mention is that bytes ranges from -128 to 127.
I am not sure but I think you can use the method writeInt()


Well, thanks for trying to help me. Unfortunatly that option doesn't work to, i've watched with wireshark what my app is sending and as it is sending Integers, the C app (whitch is the app im comunicating with does not understand because it expects byte by byte)

Thanks ;)
 
Pedro Neves
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pedro Neves wrote:
Manoj Kumar Jain wrote:First thing I would like to mention is that bytes ranges from -128 to 127.
I am not sure but I think you can use the method writeInt()


Well, thanks for trying to help me. Unfortunatly that option doesn't work to, i've watched with wireshark what my app is sending and as it is sending Integers, the C app (whitch is the app im comunicating with does not understand because it expects byte by byte)

Thanks ;)




First i had this:




and it works, except that, when any value is above 128.. then teh output becomes something like this:

--Z> 102 1 000 019 000 003 000 048 000 140 0 Frequency = 1
102 1 0 19 0 3 0 48 0 -116 0


:-/
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pedro Neves wrote:and it works, except that, when any value is above 128.. then teh output becomes something like this:
--Z> 102 1 000 019 000 003 000 048 000 140 0 Frequency = 1
102 1 0 19 0 3 0 48 0 -116 0

OK, first: TellTheDetails (←click). Nobody here had any idea that you needed to convert information in the String (at least I didn't).

Second: A java byte is an 8-bit signed number. If the application that is receiving the data expects unsigned bytes, it should still be fine since the format is the same. If you want to check the unsigned value of a byte, you can use:
System.out.println(byteValue & 0xFF);

You should also
(a) Decide what to do if you get a value > 255.
(b) Work out how you're going to convert (or eliminate) "Frequency = 1".

Winston
 
Pedro Neves
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Pedro Neves wrote:and it works, except that, when any value is above 128.. then teh output becomes something like this:
--Z> 102 1 000 019 000 003 000 048 000 140 0 Frequency = 1
102 1 0 19 0 3 0 48 0 -116 0

OK, first: TellTheDetails (←click). Nobody here had any idea that you needed to convert information in the String (at least I didn't).

Second: A java byte is an 8-bit signed number. If the application that is receiving the data expects unsigned bytes, it should still be fine since the format is the same. If you want to check the unsigned value of a byte, you can use:
System.out.println(byteValue & 0xFF);

You should also
(a) Decide what to do if you get a value > 255.
(b) Work out how you're going to convert (or eliminate) "Frequency = 1".

Winston



Well, looking like this:




The code works fine. Except when i try to send values above the 128 limit. I dont want to eliminate the frequency value, the frequency value it isnt to send either, its just do define from when to when the array of bytes will be sent (like a task to do each 2 seconds), the problem is that as far as i see, the app i'm comunicating with does not get that values (when i have a >128) thats why it ignores the bytes i sent it. I would be much aprecciate for any help. I'm always learning

Thanks
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pedro Neves wrote:The code works fine. Except when i try to send values above the 128 limit.

What happens?

the problem is that as far as i see, the app i'm comunicating with does not get that values (when i have a >128) thats why it ignores the bytes i sent it.

Are you sure, or are you just guessing? There is no possible way for the receiving app to know that it's getting a value from a Java byte, so either there's something else going on or the app requires a signed byte value. If that's the case then you probably cannot send values > 127.

Winston
 
Pedro Neves
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Pedro Neves wrote:The code works fine. Except when i try to send values above the 128 limit.

What happens?

the problem is that as far as i see, the app i'm comunicating with does not get that values (when i have a >128) thats why it ignores the bytes i sent it.

Are you sure, or are you just guessing? There is no possible way for the receiving app to know that it's getting a value from a Java byte, so either there's something else going on or the app requires a signed byte value. If that's the case then you probably cannot send values > 127.

Winston



Hello again.

Well, i'm sorry when i dont provide all the details, as i'm in the project i almost forgot that the other people don't know what i'm doing. :S

My app i'ts a kind of a domotic client app. I have a network with some nodes that send me information about the conditions of the place (like temperature, humidity etc) and gives me the possibility to send it some commands to make them do something like open a door or closing it (stuff like that). I'm doing a client to comunicate with an app that comunicates with the sensor network

The problem was that (as i finally realise), as i send a bunch of commands (to open something for example), my processor sometimes goes crazy, and sometimes, the app that comunicate with the nodes sudenly does not respond to any of the commands i send it. So, i wrongly thought that it was the commands that were send wrongly because when i send a byte value > than 127, the value turns into "- number". Unfortunatly i realise now that the problem is far away from there and that it is much more complicated than that, and i still have to solve it..

Many thanks to all of you and sorry for your time..


 
Walter Gabrielsen Iii
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you considered what character encoding you need for the String and the Network Connection? If you have so much trouble with bytes perhaps you're sending the wrong bytes? Maybe you need just straight US-ASCII instead of Unicode?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic