How to count the number of bits of a text using java

-1

I'm doing a certain application, I literally searched the internet and I did not find how I can count the number of bits of a certain phrase, can someone help me.

    
asked by anonymous 24.08.2017 / 22:04

2 answers

3

You only need to know the number of bytes and multiply by 8. For example:

System.out.println("teste".getBytes().length * 8); //40
    
24.08.2017 / 22:09
0

Knowing that each character of the sentence has 8 bits, just do this:

String str = "Algum texto";
int numDeBits = str.length * 8;

I do not know if this is the java syntax but anything leaves a comment I try to fix.

    
24.08.2017 / 22:08