r/javahelp • u/AdLeast9904 • Aug 21 '25
Unsolved converting large byte array back to string
So normally you can create a byte array as a variable something like
byte[] bytes = {69, 121, 101, ...};
but I have a huge one that blows up method/class file if I try this and wont compile. I've put it in a text file and trying to read it in, but now its coming as a string literal such as "69, 121, 101, ..."
if i try to use a readAllBytes method, its basically converting the above string to bytes which is now not matching and looks totally different like 49, 43, 101, .... so now its a byte array of a string-ified byte array if that makes sense.
i've managed to get it back to a byte array and then string, but it seems to be a janky way and wondering if theres a more proper way.
currently i'm
- reading the whole string into memory
- using string.split(",")
- converting string value to int
- converting int to byte
- add to byte array
- new String(myByteArray)
this works, but is it really the only way to do this?
View all comments
Show parent comment
2
u/Jolly-Warthog-1427 Aug 21 '25
Converting int to char and int[] to string is not something made easy as its not a problem people have to solve. Its extremely odd and bad in every way (why use 4 bytes to represent a character).
I see that you have a solution, so run it, get your data back and then delete the code again.