① java裡面byte數組和String字元串怎麼轉換
byte數組轉換成String可以調用String的參數為byte數組的構造方法,代碼如下:String res = new String(byte);
String轉換成byte數組可以調用String的getByte方法,代碼如下:byte[] srtbyte = str.getBytes();
② java中的字元串轉為byte類型怎麼轉
String類有提供好的方法啊,getBytes()
getBytes
public byte[] getBytes(Charset charset)
使用給定的 charset 將此 String
編碼到 byte 序列,並將結果存儲到新的 byte 數組。
此方法總是使用此字元集的默認替代 byte 數組替代錯誤輸入和不可映射字元序列。如果需要對編碼過程進行更多控制,則應該使用 CharsetEncoder
類。
參數:
charset - 用於編碼 String 的 Charset
返回:
所得 byte 數組
從以下版本開始:
1.6
③ 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);
}
·
④ java怎麼將這個字元串轉成byte
importjava.util.Arrays;
publicclassTest{
publicstaticvoidmain(String[]args){
Stringtext="1222334423";
String[]split=text.split("");
byte[]bytes=newbyte[split.length];
for(inti=0;i<bytes.length;i++){
bytes[i]=(byte)Integer.parseInt(split[i]);
}
System.out.println(Arrays.toString(bytes));
}
}
⑤ 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" );
這樣所需要的位元組就有恢復了。
⑥ 字元串轉byte數組 java
8b =139怎麼超出范圍了.只不過代表一個負值而已
看代碼,粘貼到類直接運行
public static void main(String[] args) {
String aa = "4c 00 3a 02 00 03 0a 8b";
if(aa == null || aa.length() <= 0) {
System.out.println("aa沒有值");
return;
}
Pattern p = Pattern.compile("[^\\da-fA-F\\s]");
Matcher m = p.matcher(aa);
if(m.find()) {
System.out.println("aa非法字元");
return;
}
String[] bb = aa.split("\\s");
byte[] cc = new byte[bb.length];
for(int i = cc.length; i-- > 0;) {
cc[i] = Integer.valueOf(bb[i], 16).byteValue();
System.out.println("cc[" + i + "]=" + cc[i] + " \tbb[" + i + "]=" + bb[i]);
}
}
⑦ 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();
}
⑧ java裡面byte數組和String字元串怎麼轉換
java中的String不屬於基本數據類型,字元串是有一個個的字元組成的,字元數組就是字元串,因此,JDK提供了如下轉換方法:
1.byte[]轉換成String:
Stringstr=newString(byte[]bytes);
2.String轉換成byte[]:
byte[]dataArray="HelloWorld!".getBytes();
以上便是jdk的String工具類提供的轉換方法。
⑨ 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
(9)java字元串byte數組擴展閱讀:
反過來,將Byte數組轉化為String的方法
using
System;
using
System.Text;
public
static
string
FromASCIIByteArray(byte[]
characters)
{
ASCIIEncoding
encoding
=
new
ASCIIEncoding(
);
string
constructedString
=
encoding.GetString(characters);
return
(constructedString);
}
·
⑩ java中如何將字元串轉換成位元組數組
package com.sunjing.util;
import java.io.UnsupportedEncodingException;
/**
* 將Byte轉換為String 或者將String轉換為Byte
*
* @author Administrator
*
*/
public class ByteOrStringHelper {
/**
* 默認的字元集編碼
* UTF-8 一個漢字佔三個位元組
*/
private static String CHAR_ENCODE = "UTF-8";
/**
* 設置全局的字元編碼
* @param charEncode
*/
public static void configCharEncode(String charEncode){
CHAR_ENCODE = charEncode;
}
/**
* @param str 源字元串轉換成位元組數組的字元串
* @return
*/
public static byte[] StringToByte(String str) {
return StringToByte(str,CHAR_ENCODE);
}
/**
*
* @param srcObj 源位元組數組轉換成String的位元組數組
* @return
*/
public static String ByteToString(byte[] srcObj) {
return ByteToString(srcObj,CHAR_ENCODE);
}
/**
* UTF-8 一個漢字佔三個位元組
* @param str 源字元串
* 轉換成位元組數組的字元串
* @return
*/
public static byte[] StringToByte(String str,String charEncode) {
byte[] destObj = null;
try {
if(null == str || str.trim().equals("")){
destObj = new byte[0];
return destObj;
}else{
destObj = str.getBytes(charEncode);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return destObj;
}
/**
* @param srcObj 源位元組數組轉換成String的位元組數組
* @return
*/
public static String ByteToString(byte[] srcObj,String charEncode) {
String destObj = null;
try {
destObj = new String(srcObj,charEncode);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return destObj.replaceAll("\0"," ");
}
}