• Post Reply Bookmark Topic Watch Topic
  • New Topic

covert from byte to string and back to byte  RSS feed

 
Olya Smith
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I hope someone can help me.

I am trying to read in some binary data from a file, change a few bits and then write the information back.

I read the data into an array of bytes. I then find the byte I need and convert it into a String, so I can change specific bits. After manipulating the String (swapping some bits with other bits) I want to write the values back to the byte array, and then back into a file.

The bytes store data as signed values. When I read the data into a String and manipulate it, I sometimes get a value like "11111111". If I try to write this value into a byte array, I get an error, because Java thinks the number is 255, which is out of range of a byte.

Am I doing something wrong? How can I write "11111111" into a byte without getting the error (why can't the value just be stored as "-1"??).

I can (I think) avoid the problem by reading in the data into an array of integers, but this means I need to use for (int i = 0; i < arraySize; i++) loop, which seems to be a very slow process.

Maybe there is a way to change a string that represents a value bigger that 127 to a signed value that I could store in a byte?

Someone please help I'm stuck!


some code samples:



 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Converting to a String?

No, read this tutorial for how to mess around with bits in Java: Java Notes: Bitwise Operators.
 
Olya Smith
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Converting to a String?

No, read this tutorial for how to mess around with bits in Java: Java Notes: Bitwise Operators.


thanks for the link, but I don't think logical operators are very useful in my case. I convert bytes to strings because I can then easily crop out the bits I need and concatenate them with another string of bits... I don't do any other operations on the bits.
 
Rob Spoor
Sheriff
Posts: 21048
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Olya Smith wrote:The bytes store data as signed values. When I read the data into a String and manipulate it, I sometimes get a value like "11111111". If I try to write this value into a byte array, I get an error, because Java thinks the number is 255, which is out of range of a byte.

Am I doing something wrong? How can I write "11111111" into a byte without getting the error (why can't the value just be stored as "-1"??).

Just convert the value to a short or int, then cast that short / int to byte. You'll then get -1.
 
Olya Smith
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:
Olya Smith wrote:The bytes store data as signed values. When I read the data into a String and manipulate it, I sometimes get a value like "11111111". If I try to write this value into a byte array, I get an error, because Java thinks the number is 255, which is out of range of a byte.

Am I doing something wrong? How can I write "11111111" into a byte without getting the error (why can't the value just be stored as "-1"??).

Just convert the value to a short or int, then cast that short / int to byte. You'll then get -1.


Thank you, this worked perfectly!
 
Rob Spoor
Sheriff
Posts: 21048
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!