java 将单个字节转换为字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11689167/
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
Convert a single byte to a string?
提问by JuiCe
This is simply to error check my code, but I would like to convert a single byte out of a byte array to a string. Does anyone know how to do this? This is what I have so far:
这只是为了错误检查我的代码,但我想将字节数组中的单个字节转换为字符串。有谁知道如何做到这一点?这是我到目前为止:
recBuf = read( 5 );
Log.i( TAG, (String)recBuf[0] );
But of course this doesn't work.
但这当然行不通。
I have googled around a bit but have only found ways to convert an entire byte[] array to a string...
我用谷歌搜索了一下,但只找到了将整个 byte[] 数组转换为字符串的方法......
new String( recBuf );
I know I could just do that, and then sift through the string, but it would make my task easier if I knew how to operate this way.
我知道我可以这样做,然后筛选字符串,但如果我知道如何以这种方式操作,它会使我的任务更容易。
回答by SLaks
You can make a new byte array with a single byte:
您可以使用单个字节创建一个新的字节数组:
new String(new byte[] { recBuf[0] })
回答by amicngh
Use toString
method of Byte
使用toString
方法Byte
String s=Byte.toString(recBuf[0] );
Try above , it works.
尝试上面,它的工作原理。
Example:
例子:
byte b=14;
String s=Byte.toString(b );
System.out.println("String value="+ s);
Output:
输出:
String value=14
回答by Sujay
There's a String
constructor of the form String(byte[] bytes, int offset, int length)
. You can always use that for your conversion.
有一个String
form的构造函数String(byte[] bytes, int offset, int length)
。您可以随时使用它进行转换。
So, for example:
因此,例如:
byte[] bite = new byte[]{65,67,68};
for(int index = 0; index < bite.length; index++)
System.out.println(new String(bite, index,1));
回答by waqaslam
Another alternate could be converting byte
to char
and finally string
另一个替代方案可能是转换byte
为char
并最终string
Log.i(TAG, Character.toString((char) recBuf[0]));
Or
或者
Log.i(TAG, String.valueOf((char) recBuf[0]));
回答by Taras Tsugrii
You're assuming that you're using 8bit character encoding (like ASCII) and this would be wrong for many others.
您假设您使用的是 8 位字符编码(如 ASCII),而这对其他人来说是错误的。
But with your assumption you might just as well using simple cast to character like
但是根据您的假设,您也可以使用简单的演员表来像
char yourChar = (char) yourByte;
or if really need String:
或者如果真的需要字符串:
String string = String.valueOf((char)yourByte);
回答by meiamsome
Edit: Hows about
编辑:怎么样
""+ recBuf[0];//Hacky... not sure if would work
((Byte)recBuf[0]).toString();
Pretty sure that would work.
很确定那会奏效。
回答by Shark
What about converting it to char? or simply
把它转换成char怎么样?或者干脆
new String(buffer[0])
回答by Sourabh Saldi
public static String toString (byte value)
public static String toString(字节值)
Since: API Level 1 Returns a string containing a concise, human-readable description of the specified byte value.
从以下版本开始:API 级别 1 返回一个字符串,其中包含对指定字节值的简明的、人类可读的描述。
Parameters value the byte to convert to a string. Returns a printable representation of value.]1this is how you can convert single byte to string try code as per your requirement
参数值要转换为字符串的字节。返回值的可打印表示。] 1这是您可以根据需要将单字节转换为字符串尝试代码的方法