java 如何在java中将字符串值转换为二进制字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17059281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Convert a String value to Binary String in java
提问by user2451783
I need to convert a String value into Binary format. For Example: String str = "Java"; now i want to get it in binary format.how could i do this? any one could please help me!
我需要将字符串值转换为二进制格式。例如: String str = "Java"; 现在我想以二进制格式获取它。我该怎么做?任何人都可以请帮助我!
Thanks in advane
提前致谢
回答by sais
Try this,
试试这个,
byte[] infoBin = null;
infoBin = "Java".getBytes("UTF-8");
for (byte b : infoBin) {
System.out.println("c:" + (char) b + "-> "
+ Integer.toBinaryString(b));
}
See this link : String to binary output in Java
请参阅此链接:Java 中的字符串到二进制输出
回答by Evgeniy Dorofeev
Try this
试试这个
byte[] bytes = str.getBytes(charSetName);
this encodes str into a sequence of bytes using the named charset, storing the result into a byte array.
这使用命名的字符集将 str 编码为字节序列,并将结果存储到字节数组中。
回答by Suresh Atta
回答by Ruchira Gayan Ranaweera
following will give you byte code;
以下将为您提供字节码;
String str = "Java";
byte[] bytecode=str.getBytes();