To convert a string to binary, we first append the string's individual ASCII values to a list ( l ) using the ord(_string) function. This function gives the ASCII value of the string (i.e., ord(H) = 72 , ord(e) = 101). Then, from the list of ASCII values we can convert them to binary using bin(_integer) .
In Python an strings can be converted into a integer using the built-in int() function. The int() function takes in any python data type and converts it into a integer.
We can convert String to float in java using Float. parseFloat() method.
The usual way is to use String#getBytes()
to get the underlying bytes and then present those bytes in some other form (hex, binary whatever).
Note that getBytes()
uses the default charset, so if you want the string converted to some specific character encoding, you should use getBytes(String encoding)
instead, but many times (esp when dealing with ASCII) getBytes()
is enough (and has the advantage of not throwing a checked exception).
For specific conversion to binary, here is an example:
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);
Running this example will yield:
'foo' to binary: 01100110 01101111 01101111
A shorter example
private static final Charset UTF_8 = Charset.forName("UTF-8");
String text = "Hello World!";
byte[] bytes = text.getBytes(UTF_8);
System.out.println("bytes= "+Arrays.toString(bytes));
System.out.println("text again= "+new String(bytes, UTF_8));
prints
bytes= [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]
text again= Hello World!
A String
in Java can be converted to "binary" with its getBytes(Charset)
method.
byte[] encoded = "こんにちは、世界!".getBytes(StandardCharsets.UTF_8);
The argument to this method is a "character-encoding"; this is a standardized mapping between a character and a sequence of bytes. Often, each character is encoded to a single byte, but there aren't enough unique byte values to represent every character in every language. Other encodings use multiple bytes, so they can handle a wider range of characters.
Usually, the encoding to use will be specified by some standard or protocol that you are implementing. If you are creating your own interface, and have the freedom to choose, "UTF-8" is an easy, safe, and widely supported encoding.
Here are my solutions. Their advantages are : easy-understanding code, works for all characters. Enjoy.
Solution 1 :
public static void main(String[] args) {
String str = "CC%";
String result = "";
char[] messChar = str.toCharArray();
for (int i = 0; i < messChar.length; i++) {
result += Integer.toBinaryString(messChar[i]) + " ";
}
System.out.println(result);
}
prints :
1000011 1000011 100101
Solution 2 :
Possibility to choose the number of displayed bits per char.
public static String toBinary(String str, int bits) {
String result = "";
String tmpStr;
int tmpInt;
char[] messChar = str.toCharArray();
for (int i = 0; i < messChar.length; i++) {
tmpStr = Integer.toBinaryString(messChar[i]);
tmpInt = tmpStr.length();
if(tmpInt != bits) {
tmpInt = bits - tmpInt;
if (tmpInt == bits) {
result += tmpStr;
} else if (tmpInt > 0) {
for (int j = 0; j < tmpInt; j++) {
result += "0";
}
result += tmpStr;
} else {
System.err.println("argument 'bits' is too small");
}
} else {
result += tmpStr;
}
result += " "; // separator
}
return result;
}
public static void main(String args[]) {
System.out.println(toBinary("CC%", 8));
}
prints :
01000011 01000011 00100101
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With