『壹』 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);
}
·