4

Let's say I have the following string:

String s = "Hello, stackoverflow"

How would I convert that into a binary number (0s and 1s)? Here's the trick: I'm not allowed to use built-in methods for the conversion. It has to be done mathematically. I have no idea where to begin. Can anyone point me in the right direction?

Edit 1: I've seen this question and people suggested using getBytes(string), charset UTF-8, and Integer.toBinaryString. I'm not allowed to use any of these methods for conversion. I haven't heard of 36-base either.

Community
  • 1
  • 1
user3479783
  • 69
  • 1
  • 2
  • 10

3 Answers3

3

This seems a bit unfair because we can't use:

byte[] b = s.getBytes();

This is the shortest solution to your problem, but since you aren't supposed to use that, here's a bad approach:

Using String#charAt(int):

String s = "Hello, stackoverflow";
StringBuilder buf = new StringBuilder();
for (int i = 0; i < s.length(); i++ ) {
    int characterASCII = s.charAt(i);
    // Automatic type casting as 'char' is a type of 'int'
    buf.append(Integer.toBinaryString(characterASCII));
    // or:
    // buf.append(characterASCII, 2);
    // or - custom method:
    // buf.append(toBinary(characterASCII));
}
System.out.println("answer : " + buf.toString());

And if you decide to use toBinary(int) method, here it is:

public static final String toBinary (int num) {
    char[] buf = new char[32];
    int charPos = 32;
    do {
        buf[--charPos] = i & 1;
        i >>>= 1;
    } while (i != 0);
    return new String(buf);
}

Or if even this is not allowed, you'd better make your own solution. :-)

lubilis
  • 3,942
  • 4
  • 31
  • 54
Hungry Blue Dev
  • 1,313
  • 16
  • 30
2

First get bytes form string using getByte() function then use Integer.toBinaryString(byte) function. & thats it.

NOTE:~ Make sure Integer.toBinaryString(byte) accepts int(or in this case byte not all.) so you have to pass byte by byte.(may be for loop.)

Edited :~ what you can do is parse string char by char & for each char you can get ascii value using.

int tempChar = (int)ch;

Then simpley use this.

Integer.toString(tempChar,2);

try this.

string finalString = "";
for(int i = 0;i<myStr.length;i++){
    int tempChar = (int)ch;
    finalString = finalString + Integer.toString(tempChar,2);//& this is not allowed then now only you need to create function for integer to binary conversion.
}
System.out.println("Your byte String is"+finalString);
Pulah Nandha
  • 774
  • 4
  • 23
1

You may try like this:-

String s = "Hello, stackoverflow"
byte[] bytes = str.getBytes(s);

You may check the getBytes() method in java

Encodes this String into a sequence of bytes using the named charset, storing the result into a new byte array.

The behavior of this method when this string cannot be encoded in the given charset is unspecified. The CharsetEncoder class should be used when more control over the encoding process is required.

If you want the bits for each byte:

for(int i=0; i<array.length;i++){
    System.out.println(Integer.toBinaryString(0x100 + array[i]).substring(1));
}
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331