1. java裡面byte數組和String字元串怎麼轉換
java裡面byte數組和String字元串轉換有兩種方法:
1、不設定編碼方式
<prename="code"class="java">Stringstr="Hello";
byte[]srtbyte=str.getBytes();//string轉byte[]
//s
Stringres=newString(srtbyte);//byte[]轉string
2、設定編碼方式
Stringstr="hello";
byte[]srtbyte=null;
try{
srtbyte=str.getBytes("UTF-8");//string轉byte[]
Stringres=newString(srtbyte,"UTF-8");//byte[]轉string
}catch(UnsupportedEncodingExceptione){
e.printStackTrace();
}
2. java怎麼把string轉換成數組
轉換為char數組的話,利用getChars方法,
public void getChars(int srcBegin,
int srcEnd,
char[] dst,
int dstBegin)
將字元從此字元串復制到目標字元數組。
要復制的第一個字元位於索引 srcBegin 處;要復制的最後一個字元位於索引 srcEnd-1
處(因此要復制的字元總數是 srcEnd-srcBegin)。要復制到 dst 子數組的字元從索引
dstBegin 處開始,並結束於索引:
dstbegin + (srcEnd-srcBegin) - 1
參數:
srcBegin - 字元串中要復制的第一個字元的索引。
srcEnd - 字元串中要復制的最後一個字元之後的索引。
dst - 目標數組。
dstBegin - 目標數組中的起始偏移量。
拋出:
IndexOutOfBoundsException
- 如果下列任何一項為 true:
srcBegin 為負。
srcBegin 大於 srcEnd
srcEnd 大於此字元串的長度
dstBegin 為負
dstBegin+(srcEnd-srcBegin) 大於
dst.length
轉化為byte數組的話,
getBytes
public byte[] getBytes(String charsetName)
throws UnsupportedEncodingException
使用指定的字元集將此 String 編碼為 byte 序列,並將結果存儲到一個新的 byte 數組中。
當此字元串不能使用給定的字元集編碼時,此方法的行為沒有指定。如果需要對編碼過程進行更多控制,則應該使用 CharsetEncoder
類。
參數:
charsetName - 受支持的 charset 名稱
返回:
所得 byte 數組
3. 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);
}
·
4. java怎麼將string轉換成byte數組
思路:先定義字元串,再通過getBytes()方法進行轉換數組就可以了。
參考代碼:
Strings="ZhiDao";//定義字元串
byte[]sb=s.getBytes();//把字元串轉換成數組
String的getBytes()方法是得到一個系統默認的編碼格式的位元組數組。將一個String類型的字元串中包含的字元轉換成byte類型並且存入一個byte[]數組中。
5. java裡面byte數組和String字元串怎麼轉換
byte數組轉換成String可以調用String的參數為byte數組的構造方法,代碼如下:String res = new String(byte);
String轉換成byte數組可以調用String的getByte方法,代碼如下:byte[] srtbyte = str.getBytes();
6. Java中,String類型怎麼轉換成byte類型輸出
程序如下:
String s = "fs123fdsa";//「fs123fdsa」是輸入的string變數
byte b[] = s.getBytes();//String轉換為byte[]
String t = new String(b);//bytep[]轉換為String
7. 在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;
}
8. string 怎麼轉換成byte 數組 java
publicstaticvoidmain(String[]args){
Stringstr="您的回答被採納後將獲得系統獎勵";
byte[]b=getBytes(str);
if(b!=null){
//......
}
}
publicbyte[]getBytes(Stringstr){
if(StringUtils.isNotBlank(str)){
try{
returnstr.trim().getBytes("UTF-8");
}catch(){
Logger.getLogger(Test.class.getName()).log(Level.SEVERE,null,ex);
}
}
returnnull;
}
9. java 裡面的string 和byte 怎麼互轉
1.string 轉 byte[]
byte[] midbytes=isoString.getBytes("UTF8");
//為UTF8編碼
byte[] isoret = srt2.getBytes("ISO-8859-1");
//為ISO-8859-1編碼
其中ISO-8859-1為單位元組的編碼
2.byte[]轉string
String isoString = new String(bytes,"ISO-8859-1");
String srt2=new String(midbytes,"UTF-8");
說明:
在網路傳輸或其它應用中常常有同一的中間件,假設為String類型。因此需要把其它類型的數據轉換為中間件的類型。
將字元串進行網路傳輸時,如socket,需要將其在轉換為byte[]類型。這中間如果採用用不同的編碼可能會出現未成預料的問題,如亂碼。
下面舉個例子:
我們用socket傳輸String類型的數據時,常常用UTF-8進行編碼,這樣比較可以避免一個「中文亂碼」的問題。
發送端:
String sendString="發送數據";
byte[] sendBytes= sendString .getBytes("UTF8");
.......socket發送
接受端:
String recString=new String( sendBytes ,"UTF-8");
但是,這里往往又會出現這樣一個問題。就是想要發送的數據本身就是byte[]類型的。
如果將其通過UTF-8編碼轉換為中間件String類型就會出現問題
如:
byte[] bytes = new byte[] { 50, 0, -1, 28, -24 };
String sendString=new String( bytes ,"UTF-8");
byte[] sendBytes= sendString .getBytes("UTF8");
然後再發送
接受時進行逆向轉換
String recString=new String( sendBytes ,"UTF-8");
byte[] Mybytes=isoString.getBytes("UTF8");
這時Mybytes中的數據將是[50, 0, -17, -65, -67, 28, -17, -65, -67]
因此,需要採用單位元組的編碼方式進行轉換
String sendString=new String( bytes ,"UTF-8"); 改為 String sendString=new String( bytes , "ISO-8859-1" );
byte[] Mybytes=isoString.getBytes("UTF8"); 改為 byte[] Mybytes=isoString.getBytes( "ISO-8859-1" );
這樣所需要的位元組就有恢復了。