⑴ 怎麼用java把一個字元串進行utf8編碼
String text = 「字元串」;
byte[] b_utf8 = text.getBytes("UTF-8"); //utf-8
byte[] b_iso88591 = text.getBytes("ISO8859-1"); //iso8859-1
byte[] b_gbk = text.getBytes("GBK"); //gbk
string unicode = getUnicode(text);//unicode
public static String getUnicode(String source) {
String result = "";
for (int i = 0; i < source.length(); i++) {
result += "\u"+Integer.toHexString((int) source.charAt(i));
}
return result;
}
⑵ java輸出utf8編碼
給你一點提示:
import java.net.URLEncoder;
yourNewString = URLEncoder.encode(yourString, "UTF-8");
同樣,解碼就把以上都變成Decoder
我這個是內置包,最簡單的方法了,你先試一下吧
⑶ java 怎麼把一個字元串進行utf8編碼
樓上答得不對,請相信我的方法,已經確認過:
String s="abc";
String s1=URLEncoder.encode(s, "utf-8");
⑷ java怎麼把把字元轉成utf-8
java不同編碼之間進行轉換,都需要使用unicode作為中轉。
以utf-8轉gbk為例,示例代碼如下:
String t = "這是一個字元串aaa111";
String utf8 = new String(t.getBytes( "UTF-8"));
System.out.println(utf8);
String unicode = new String(utf8.getBytes(),"UTF-8");
System.out.println(unicode);
String gbk = new String(unicode.getBytes("GBK"));
System.out.println(gbk);
⑸ java里關於String的編碼與解碼
public
byte[]
getBytes(String
charsetName)
使用指定的字元集將此String編碼為byte序列,結果存在一個byte數組中
public
String(byte[]
bytes,
String
charsetName)
通過使用指定的
charset
解碼指定的
byte
數組,構造一個新的
String。
在網路傳輸中,信息都是以位元組序列的方式傳輸的。所以,發送方的String要按照某種編碼方式(如UTF-8,GBK)編碼為位元組序列,在網路中傳輸後,接收方取得這個位元組序列,按照相同的編碼方式將位元組序列解碼為String。
請看下面的代碼片段:
String
name
=
"張三";
byte[]
b1
=
name.getBytes("UTF-8");
String
name1
=
new
String(b1,
"UTF-8");
//編碼解碼相同,正常顯示
System.out.println(name1);
String
name2
=
new
String(b1,
"GBK");
//編碼解碼不同,亂碼
System.out.println(name2);
byte[]
b2
=
name.getBytes("GBK");
String
name3
=
new
String(b2,
"GBK");
//編碼解碼相同,正常顯示
System.out.println(name3);
String
name4
=
new
String(b2,
"UTF-8");
//編碼解碼不同,亂碼
System.out.println(name4);
至於你的那個情況,要先用gbk編碼,然後再用utf-8解碼才能獲得正常的字元串,我估計是因為
1.傳輸過來的位元組碼是用utf-8編碼的,假設位元組碼為b。
2.你獲得的那個字元串,假設為s,是用gbk對b進行解碼獲得的字元串,所以是亂碼。
3.你使用gbk對s進行編碼,用gbk解碼之後再編碼,於是獲得了原來的b。
4.你使用utf-8解碼,所以獲得了正常的字元串。
簡單的說:
b
->
(gbk解碼)
->
亂碼
->
[此處開始是你做的](gbk編碼)
->
b
->
(utf-8解碼)
->
正常字元串
研究完編碼收獲會不小的,對以後理解Java的輸入輸出(尤其是網路通信和文件讀寫)都很有幫助。
⑹ java String字元串轉UTF-8 hexcode怎麼轉
不知道這是不是你想要的。
String str = "哈希";
byte[] b = str.getBytes();
for(int i=0;i<b.length;i++){
System.out.print(b[i]);
}
控制台輸出:-27-109-120-27-72-116
我的eclipse設置編碼集為utf-8。看來utf-8裡面每個漢字佔3個位元組了。
⑺ java中如何將UTF-8編碼的字元串使用GB18030輸出
java
很方便進行這種轉換
如果輸入是UTF8的位元組用
String
s=new
String(utf8_bytes,
"UTF-8");
//utf8_bytes為byte[]類型
如果輸入是字元串,直接內
String
s=原返回。
在輸出是轉換gb18030,不輸出時不用轉換。
os.write(s.getBytes("GB18030"));
//os為輸出流容
⑻ java如何把string轉為utf-8
可通過以下代碼轉:
/**
*GetXMLStringofutf-8
*
*@returnXML-Formedstring
*/
(Stringxml){
//AStringBufferObject
StringBuffersb=newStringBuffer();
sb.append(xml);
StringxmString="";
StringxmlUTF8="";
try{
xmString=newString(sb.toString().getBytes("UTF-8"));
xmlUTF8=URLEncoder.encode(xmString,"UTF-8");
System.out.println("utf-8編碼:"+xmlUTF8);
}catch(UnsupportedEncodingExceptione){
//TODOAuto-generatedcatchblock
e.printStackTrace();
}
//returntoStringFormed
returnxmlUTF8;
}
⑼ java String字元串轉UTF-8 hexcode怎麼轉
String s=new String("欲轉換字元串".getBytes(),"utf-8");
String s=new String("欲轉換字元串".getBytes("utf-8"),"utf-8");
其中
s.getBytes("UTF-8");
的意思是以UTF-8的編碼取得位元組
new String(XXX,"UTF-8");
的意思是以UTF-8的編碼生成字元串