‘壹’ java里面byte数组和String字符串怎么转换
看 String 的构造函数
~
~
~
~
‘贰’ java里面byte数组和String字符串怎么转换
1、string 转 byte[]
String str = "Hello";
byte[] srtbyte = str.getBytes();
2、byte[] 转 string
byte[] srtbyte;
String res = new String(srtbyte);
System.out.println(res);
3、设定编码方式相互转换
String str = "hello";
byte[] srtbyte = null;
try {
srtbyte = str.getBytes("UTF-8");
String res = new String(srtbyte,"UTF-8");
System.out.println(res);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
‘叁’ java怎么将string转换成byte数组
思路:先定义字符串,再通过getBytes()方法进行转换数组就可以了。
参考代码:
Strings="ZhiDao";//定义字符串
byte[]sb=s.getBytes();//把字符串转换成数组
String的getBytes()方法是得到一个系统默认的编码格式的字节数组。将一个String类型的字符串中包含的字符转换成byte类型并且存入一个byte[]数组中。
‘肆’ java里面byte数组和String字符串怎么转换
java中byte数组转换成string字符串可以直接使用string类的构造函数。而string转byte数组,则可以使用string类型的getBytes()方法进行转换,如下形式:
1、string 转 byte[]
String str = "Hello";//声明一个字符串
byte[] srtbyte = str.getBytes();//使用string类的getBytes方法进行转换
2、byte[] 转 string
byte[] srtbyte;//声明一个byte字节数组
String res = new String(srtbyte);//使用构造函数转换成字符串
System.out.println(res);
也可以将byte转换的时候,设定编码方式相互转换,如下代码:
String str = "hello";
byte[] srtbyte = null;
try {
srtbyte = str.getBytes("UTF-8");//设定转换的编码格式
String res = new String(srtbyte,"UTF-8");
System.out.println(res);
} catch (UnsupportedEncodingException e) {//有可能会出现不能支持的编码格式,捕捉异常。
e.printStackTrace();
}
‘伍’ java里面byte数组和String字符串怎么转换
//string 转 byte[]
String str = "Hello";
byte[] srtbyte = str.getBytes();
// byte[] 转 string
String res = new String(srtbyte);
System.out.println(res);
//当然还有可以设定编码方式
的
String str = "hello";
byte[] srtbyte = null;
try {
srtbyte = str.getBytes("UTF-8");
String res = new String(srtbyte,"UTF-8");
System.out.println(res);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
‘陆’ java里面byte数组和String字符串怎么转换
java中的String不属于基本数据类型,字符串是有一个个的字符组成的,字符数组就是字符串,因此,JDK提供了如下转换方法:
1.byte[]转换成String:
Stringstr=newString(byte[]bytes);
2.String转换成byte[]:
byte[]dataArray="HelloWorld!".getBytes();
以上便是jdk的String工具类提供的转换方法。
‘柒’ java里面byte数组和String字符串怎么转换
byte数组转换成String可以调用String的参数为byte数组的构造方法,代码如下:String res = new String(byte);
String转换成byte数组可以调用String的getByte方法,代码如下:byte[] srtbyte = str.getBytes();
‘捌’ Java中,String类型怎么转换成byte类型输出
程序如下:
String s = "fs123fdsa";//“fs123fdsa”是输入的string变量
byte b[] = s.getBytes();//String转换为byte[]
String t = new String(b);//bytep[]转换为String
‘玖’ 在java中,将一个String类型的值转换为byte类型,只能占2个字节存储。
Byte.parseByte(s, 16)将s转换成有符号数 ,这一个字节能表示的范围是:-128~127,下面的代码是在搜来的代码基础上稍做了修改,调用的时候你只要把temperature传进去即可得到转换后的byte数组,byte数组的长度由temperature的值决定,你要求只能占2个字节存储的话temperature的长度就要确保不超过4,这由你自己的代码来约束。
public static byte[] hex2byte(String hex) {
String digital = "0123456789ABCDEF";
char[] hex2char = hex.toCharArray();
byte[] bytes = new byte[hex.length() / 2];
int temp;
for (int i = 0; i < bytes.length; i++) {
temp = digital.indexOf(hex2char[2 * i]) * 16;
temp += digital.indexOf(hex2char[2 * i + 1]);
bytes[i] = (byte) (temp & 0xff);
}
return bytes;
}
‘拾’ java中String类型的如何转为byte[]
一、String转byte数组简单版:
1、String str = "abcd";
2、byte[] bs = str.getBytes();
二、复杂版
// pros - no need to handle UnsupportedEncodingException // pros - bytes in specified
encoding scheme byte[] utf8 = "abcdefgh".getBytes(StandardCharsets.UTF_8);
System.out.println("length of byte array in UTF-8 : " + utf8.length);
System.out.println("contents of byte array in UTF-8: " + Arrays.toString(utf8));
Output : length of byte array in UTF-8 : 8 contents of byte array in UTF-8: [97, 98, 99, 100, 101, 102, 103, 104]1
反过来,将Byte数组转化为String的方法
using System;
using System.Text;
public static string FromASCIIByteArray(byte[] characters)
{
ASCIIEncoding encoding = new ASCIIEncoding( );
string constructedString = encoding.GetString(characters);
return (constructedString);
}
·