File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

convert byte array to string index by index

 
Sana Bokhari
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey i am reading a file byte by byte..., then storing it in a byte array and lastly converting it to a string... but i want to convert values on every index of the byte array individually..... i.e. what ever bytes i have on index one should be converted to string.....the problem here is, i convert the whole byte array into a string at one time. plus i am concatenating the bytes in "data" in string form which means even if i convert "data" back to string i will let get the same result i.e. the bytes .. rather then the data in readable form...
Please help




 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch

Moving to Java in General.

 
Winston Gutkowski
Bartender
Pie
Posts: 9442
49
Eclipse IDE Hibernate Ubuntu
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sana Bokhari wrote:hey i am reading a file byte by byte..., then storing it in a byte array and lastly converting it to a string...

First mistake. Bytes and characters are not the same thing. They can be converted; though this is usually done by using either
(a) a BufferedReader, or
(b) one of the String constructors that takes a byte array.
but this really only works if what you are reading in is text. If it's binary, you really DON'T want a String.

but i want to convert values on every index of the byte array individually..... i.e. what ever bytes i have on index one should be converted to string.....

And here's where my confusion starts. Are you saying that if you read in a byte that contains an ascii 'a', you want it to print out as '97'?
Because that's what your code will do.

Furthermore, if you simply concatenate all those numbers together, you will never be able to get your original values back, because the strings produced may be of different lengths. You could zero-fill them to some uniform length, but it seems like an awful lot of "noise code" to me.

My suggestion is this: back up and explain exactly what it is you're trying to do (NOT what you've done) and why. That might help us suggest a solution.

Also: provide some examples of what you want, because at the moment your requirements mystify me.

Winston
 
Sana Bokhari
Greenhorn
Posts: 7
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Sana Bokhari wrote:hey i am reading a file byte by byte..., then storing it in a byte array and lastly converting it to a string...

First mistake. Bytes and characters are not the same thing. They can be converted; though this is usually done by using either
(a) a BufferedReader, or
(b) one of the String constructors that takes a byte array.
but this really only works if what you are reading in is text. If it's binary, you really DON'T want a String.

but i want to convert values on every index of the byte array individually..... i.e. what ever bytes i have on index one should be converted to string.....

And here's where my confusion starts. Are you saying that if you read in a byte that contains an ascii 'a', you want it to print out as '97'?
Because that's what your code will do.

Furthermore, if you simply concatenate all those numbers together, you will never be able to get your original values back, because the strings produced may be of different lengths. You could zero-fill them to some uniform length, but it seems like an awful lot of "noise code" to me.

My suggestion is this: back up and explain exactly what it is you're trying to do (NOT what you've done) and why. That might help us suggest a solution.

Also: provide some examples of what you want, because at the moment your requirements mystify me.

Winston






hey thank you for explaing it ....and i got it right now
 
Winston Gutkowski
Bartender
Pie
Posts: 9442
49
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sana Bokhari wrote:hey thank you for explaing it ....and i got it right now

You're welcome.

Winston
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic