Hello guys!
have small problem, and would very much appreciate help :)
I should convert byte array to string and get this output string: “[0, 0, 0, 0]” After that another method should take the string as input and retrieve the byte array from the first one.
Im getting error that i have number.format exception, so i guess i should make convertToString method in some other way. This is what i have so far:
import java.io.ByteArrayOutputStream;
import java.util.StringTokenizer;
public class byteToString {
public String convertToString(){
byte[] byteArray = new byte[] {91,79,44,32,79,44,32,79,44,32,79,93};
String holder = new String(byteArray);
return holder;
}
/*was told to use this code to convert back*/
private static byte[] toByteArray(String myString){
myString = myString.substring(0, myString.length()- 1).substring(1);
ByteArrayOutputStream myStream = new ByteArrayOutputStream();
for (StringTokenizer myTok = new StringTokenizer(myString, ","); myTok.hasMoreTokens();){
myStream.write(Byte.parseByte(myTok.nextToken().trim()));
}
return myStream.toByteArray();
}
public static void main(String[] args){
String myString = new byteToString().convertToString();
toByteArray(myString);
}
}
Thanks ahead!! :)