| Author |
convert byte array to string index by index
|
Sana Bokhari
Greenhorn
Joined: Jan 12, 2012
Posts: 7
|
|
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
Joined: Jan 28, 2008
Posts: 5575
|
|
Welcome to JavaRanch
Moving to Java in General.
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4761
|
|
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
|
Isn't it funny how there's always time and money enough to do it WRONG?
|
 |
Sana Bokhari
Greenhorn
Joined: Jan 12, 2012
Posts: 7
|
|
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
Joined: Mar 17, 2011
Posts: 4761
|
|
Sana Bokhari wrote:hey thank you for explaing it ....and i got it right now 
You're welcome.
Winston
|
 |
 |
|
|
subject: convert byte array to string index by index
|
|
|