-2

After trying every answer on this topic I could find (and searching for almost a full day), I've been unable to successfully convert binary data in string format (variable a) to an alphanumeric string. My question is "What am I doing wrong?"

Ultimately I want this to print out a human readable string, such as "Hello There", which would be the integer 87521618088882520788922981 (input will be changed to long.)

int input = 256;
    int maxInt = 10000;
    String result = "";
    String o;
    String a = "";
    String stringByte;
    char c;
    int pos;
     while (input <= maxInt){
         System.out.println("Input:          " + input);
         o = Integer.toBinaryString(input);
         System.out.println("Binary String:  " + o);
         System.out.println("Remainder:      " + o.length() % 8);
         if (((o.length() % 8) == 0) && (o != "0")){
             //is a multiple
            a = o;
        }else{
            a = o;
            while ((a.length() % 8 != 0)){
                a = "0" + a;
            }
         }
         //divide into bytes
         System.out.println("Zero-putter:    " + a);
         int numBytes = a.length()/8;
         pos = 1;
         System.out.println("Bytes Received: " + numBytes);
             byte[] bytes = a.getBytes();
             String str = new String(bytes);
         System.out.println("Character:      " + str);
         System.out.println("-----------------------------");
         input++;
         try {
                Thread.sleep(1000);                 //1000 milliseconds is one second.
            } catch(InterruptedException ex) {
                Thread.currentThread().interrupt();
            }
        }



     }

Answers I've tried:
- Binary to text in Java
- Convert binary string to ascii text?
- Parsing a string of binary into text/characters
- How to convert efficiently binary string to binary byte array in Java?
- http://www.dreamincode.net/forums/topic/247956-convert-binary-data-to-text/
- How do I convert a large binary String to byte array java?
- How do I convert a large binary String to byte array java?

11
  • 1
    It would be helpful to list "every answer on this topic" you've tried so you don't receive duplicate answer you claim didn't work for you. Commented Feb 24, 2015 at 0:17
  • Alphanumeric? Do you mean a decimal string? Also, separating your logic from the Scanner/input/output would make it easier to test. Commented Feb 24, 2015 at 0:18
  • 1
    Give an example of what this should print. Commented Feb 24, 2015 at 0:18
  • @AdrianLeonhard The scanner is declared but is never used. that is a remnant from an iteration when I tried to put a "stop the loop" command in. Commented Feb 24, 2015 at 0:29
  • 1
    Show clear sample of your input and expected output. It's unclear what your're asking and what's the purpose. Commented Feb 24, 2015 at 0:32

1 Answer 1

0

You can try BigInteger to convert byte array to limitless number.

1- convert your String in array of characters

char[] chars = myString.getBytes();

2- convert your array of characters in array of bytes ( byte b = (byte)c where c is character )

byte[] bytes = new byte[chars.length];
for(int i=0; i<bytes.length;i++){
  bytes[i] = (byte)chars[i];
}

3- use BigInteger

BigInteger myData = new BigInteger(bytes);

4- display as string

myData.toString();
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.