import java.lang.*;
import java.io.*;
class d2b
{
public static void main(String args[]) throws IOException{
BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the decimal value:");
String h = b.readLine();
int k = Integer.parseInt(h);
String out = Integer.toBinaryString(k);
System.out.println("Binary: " + out);
}
}
import java.lang.*;
import java.io.*;
class d2b
{
public static void main(String args[]) throws IOException{
BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the decimal value:");
String h = b.readLine();
int k = Integer.parseInt(h);
String out = Integer.toBinaryString(k);
System.out.println("Binary: " + out);
}
}
public class Test {
public String toBinary(String text) {
StringBuilder sb = new StringBuilder();
for (char character : text.toCharArray()) {
sb.append(Integer.toBinaryString(character) + "\n");
}
return sb.toString();
}
}
This is my implementation.
public class Test {
public String toBinary(String text) {
StringBuilder sb = new StringBuilder();
for (char character : text.toCharArray()) {
sb.append(Integer.toBinaryString(character) + "\n");
}
return sb.toString();
}
}
public class HexadecimalToBinaryAndLong{
public static void main(String[] args) throws IOException{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the hexa value!");
String hex = bf.readLine();
int i = Integer.parseInt(hex); //hex to decimal
String by = Integer.toBinaryString(i); //decimal to binary
System.out.println("This is Binary: " + by);
}
}
public class HexadecimalToBinaryAndLong{
public static void main(String[] args) throws IOException{
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the hexa value!");
String hex = bf.readLine();
int i = Integer.parseInt(hex); //hex to decimal
String by = Integer.toBinaryString(i); //decimal to binary
System.out.println("This is Binary: " + by);
}
}
public static String stringToBinary(String str, boolean pad ) {
byte[] bytes = str.getBytes();
StringBuilder binary = new StringBuilder();
for (byte b : bytes)
{
binary.append(Integer.toBinaryString((int) b));
if(pad) { binary.append(' '); }
}
return binary.toString();
}
While playing around with the answers I found here to become familiar with it I twisted Nuoji's solution a bit so that I could understand it faster when looking at it in the future.
public static String stringToBinary(String str, boolean pad ) {
byte[] bytes = str.getBytes();
StringBuilder binary = new StringBuilder();
for (byte b : bytes)
{
binary.append(Integer.toBinaryString((int) b));
if(pad) { binary.append(' '); }
}
return binary.toString();
}
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);
运行此示例将产生:
'foo' to binary: 01100110 01101111 01101111
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);
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.
It's easy, because rather than including some way to note the encoding of each message, you can default to UTF-8.
It's safe, because UTF-8 can encode any character that can be used in a Java character string.
It's widely supported, because it is one of a small handful of character encodings that is required to be present in any Java implementation, all the way down to J2ME. Most other platforms support it too, and it's used as a default in standards like XML.
发布评论
评论(10)
这是我的实现。
This is my implementation.
您也可以使用 ol' 好的方法来做到这一点:
You can also do this with the ol' good method :
在尝试我在这里找到的答案以熟悉它时,我稍微扭曲了诺吉的解决方案,以便我在将来查看它时可以更快地理解它。
While playing around with the answers I found here to become familiar with it I twisted Nuoji's solution a bit so that I could understand it faster when looking at it in the future.
通常的方法是使用 String#getBytes() 获取底层字节,然后以其他形式(十六进制、二进制等)呈现这些字节。
请注意,
getBytes()
使用默认的字符集,因此如果您希望将字符串转换为某种特定的字符编码,则应该使用getBytes(String encoding)
,但很多时候(特别是在处理 ASCII 时)getBytes()
就足够了(并且具有不引发已检查异常的优点)。对于到二进制的具体转换,这里有一个示例:
运行此示例将产生:
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 usegetBytes(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:
Running this example will yield:
更短的示例
打印
A shorter example
prints
Java 中的
String
可以通过其getBytes(Charset)
方法。该方法的参数是“字符编码”; 这是字符和字节序列之间的标准化映射。 通常,每个字符都被编码为单个字节,但没有足够的唯一字节值来表示每种语言中的每个字符。 其他编码使用多个字节,因此它们可以处理更广泛的字符。
通常,要使用的编码将由您正在实施的某些标准或协议指定。 如果您正在创建自己的界面,并且可以自由选择,那么“UTF-8”是一种简单、安全且得到广泛支持的编码。
A
String
in Java can be converted to "binary" with itsgetBytes(Charset)
method.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.
这是我的解决方案。 它们的优点是:代码易于理解,适用于所有字符。 享受。
解决方案 1:
打印:
解决方案 2:
可以选择每个字符的显示位数。
印刷 :
Here are my solutions. Their advantages are : easy-understanding code, works for all characters. Enjoy.
Solution 1 :
prints :
Solution 2 :
Possibility to choose the number of displayed bits per char.
prints :