0

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!! :)

1

3 Answers 3

3

new byte[] {91,79,44,32,79,44,32,79,44,32,79,93} is actually [O, O, O, O] array of Ohs not zeroes!

Use new byte[] {91,48,44,32,48,44,32,48,44,32,48,93} instead.

Also want to note that you can use:

myString = myString.substring(1, myString.length() - 1);

instead of:

myString = myString.substring(0, myString.length()- 1).substring(1);.

It is more efficient.

Sign up to request clarification or add additional context in comments.

Comments

0
public class AAA {

    public static final byte[] TEST_DATA = {91, 79, 44, 32, 79, 44, 32, 79, 44, 32, 79, 93};

    public String convertToString(byte[] array) {
        return Arrays.toString(array);
    }

    private static byte[] toByteArray(String myString) {
        ByteArrayOutputStream bOut = new ByteArrayOutputStream();
        Pattern pattern = Pattern.compile("\\D*(\\d+)");
        Matcher matcher = pattern.matcher(myString);
        while (matcher.find()) {
            bOut.write((byte)Integer.parseInt(matcher.group(1)));
        }
        return bOut.toByteArray();
    }

    public static void main(String[] args) {
        String myString = new AAA().convertToString(TEST_DATA);
        byte[] bytes = toByteArray(myString);
        System.out.println("Test " + (Arrays.equals(bytes, TEST_DATA) ? "passed" : "failed"));
    }
}

1 Comment

elegant solution, thanx! But for some reason im required to use the code from the toByteArray method i wrote....
0

Simple solution for ByteArray to String is below

 public class ByteArrayToString {
            public static void main(String args[]) {
                byte[] random = new byte[] { 34, 65, 54 , 76, 66, 65, 66, 70, -10 };
                String utf = new String(random, "UTF-8");
                String cp1252 = new String(random, "Cp1252");
                String windows1252 = new String(random, "Windows-1252");
                System.out.println("String created from byte `enter code here`array in UTF-8 encoding : "+ utf);
                System.out.println("byte array to String in Cp1252 encoding : " + cp1252);
                System.out.println("byte array to String in Windows-1252 encoding : "+ windows1252);
            }
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.