-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?

Community
  • 1
  • 1
cd3
  • 117
  • 1
  • 9
  • 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. – TWhite Feb 24 '15 at 00:17
  • Alphanumeric? Do you mean a decimal string? Also, separating your logic from the Scanner/input/output would make it easier to test. – Adrian Leonhard Feb 24 '15 at 00:18
  • 1
    Give an example of what this should print. – user253751 Feb 24 '15 at 00: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. – cd3 Feb 24 '15 at 00:29
  • If it isn't relevant, you should remove it from the question... – Adrian Leonhard Feb 24 '15 at 00:31
  • 1
    Show clear sample of your input and expected output. It's unclear what your're asking and what's the purpose. – zubergu Feb 24 '15 at 00:32
  • binary string to hex string: Integer.toString(Integer.parseInt(input, 2), 16) – Adrian Leonhard Feb 24 '15 at 00:35
  • this in unclear: "Hello There", which would be the integer 87521618088882520788922981. How an ARRAY of characters (or numbers) colud be converted in ONE single number? – Matteo Rubini Feb 24 '15 at 01:21
  • @MatteoRubini the idea is as the number counts up, it is seperated into groups of 8 bits. these bits are then converted to alphanumeric charaters, and put in a string. – cd3 Feb 24 '15 at 01:36
  • an 8bit number is in range of (255-0). if u concatenate them, u can't split again because you don't know how long is any number. f.e. if i concatenate 124;45;22;1;23 i get 1244522123 and is impossible to know how to split again – Matteo Rubini Feb 24 '15 at 01:41
  • @MatteoRubini the number is converted to binary first, and then groups of 8 bits. As in ASCII alphanumeric characters are ID's 33 - 122, each group of 8 bits should (at some point) return a letter. – cd3 Feb 24 '15 at 01:46

1 Answers1

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();
Matteo Rubini
  • 831
  • 5
  • 9