5

How to convert string into bits(not bytes) or array of bits in Java(i will do some operations later) and how to convert into array of ints(every 32 bits turn into the int and then put it into the array? I have never done this kind of conversion in Java.

String->array of bits->(some operations I'll handle them)->array of ints
10
  • 3
    please provide more details about what on earth you're doing. Commented Jul 6, 2012 at 18:20
  • I provide as much info as neeeded. I didn't want to blur it to much. Commented Jul 6, 2012 at 18:23
  • 1
    If you get asked for more details, that's a hint that more info is needed. Commented Jul 6, 2012 at 18:25
  • There is no such thing as an "array of bits" in Java. Commented Jul 6, 2012 at 18:49
  • @Wug Since no help is coming from OP, my guess is that he wants a bit stream that he'll transform into another bit stream. Commented Jul 6, 2012 at 18:52

7 Answers 7

9
ByteBuffer bytes = ByteBuffer.wrap(string.getBytes(charset));
  // you must specify a charset
IntBuffer ints = bytes.asIntBuffer();
int numInts = ints.remaining();
int[] result = new int[numInts];
ints.get(result);
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I'll check it as fast as I can. Looks promising.(if it will provide bits not bytes)
Like I said in other comments, there's really no way to just get bits in Java that doesn't just get bytes or ints or something bigger. (That said, bits can be easily extracted from the bytes.)
2

THIS IS THE ANSWER

String s = "foo";
      byte[] bytes = s.getBytes();
      StringBuilder binary = new StringBuilder();
      for (byte b : bytes)
      {
         int val = b;
         for (int i = 0; i < 8; i++)
         {
            binary.append((val & 128) == 0 ? 0 : 1);
            val <<= 1;
         }
      //   binary.append(' ');
      }
      System.out.println("'" + s + "' to binary: " + binary);

Comments

0

You are looking for this:

string.getBytes();

Not list, it's an array but you can use it later on, even to convert it to integers.

2 Comments

You can't get pure bits in Java. You can only get them "in bulk" as bytes, ints, etc.
Seriously, can't get bits from bytes? :)
0

Well, maybe you can skip the String to bits conversion and convert directly to an array of ints (if what you want is the UNICODE value of each character), using s.toCharArray() where s is a String variable.

1 Comment

No i don't want to convert it to int. I need to do some operations on bits first(add some bits).
0

This will convert "abc" to byte and then the code will print "abc" in respective ASCII code (ie. 97 98 99).

byte a[]=new byte[160];
String s="abc";
a=s.getBytes();
for(int i=0;i<s.length();i++)
{
    System.out.print(a[i]+" ");
}

1 Comment

I know that stuff. I need bits -> 10101010101 this kind ; )
0

May be so (I have no compiler in my current computer and don't test if it work, but it can help you a bit):

String st="this is a string";
byte[] bytes=st.getBytes();
List<Integer> ints=new ArrayList<Integer>();
ints.addAll(bytes);

If compiler fails in

ints.addAll(bytes);

you can replace it with

for (int i=0;i<bytes.length();i++){
   ints.add(bytes[i]);
}

and if you want to get exactly array:

ints.toArray();

1 Comment

Oops, sorry, I've missunderstood you. You want to get bits.
0

Note that string is a sequence of chars, and in Java each char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive). In order to get char integer value do this:

    String str="test";
    String tmp="";

    int result[]=new int[str.length()/2+str.length()%2];
    int count=0;

    for(char c:str.toCharArray()) {
         tmp+=Integer.toBinaryString((int)c);
         if(tmp.length()==14) {
            result[count++]=Integer.valueOf(tmp,2);
            //System.out.println(tmp+":"+result[count-1]);
            tmp="";
         }
    }

    for(int i:result) {
        System.out.print(i+" ");
    }

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.