Java中文轉GBK碼
遇到一個需求,一個介面的呼叫時,需要將中文轉成對應的GBK碼,然後發請求呼叫,大概搜了下,貌似沒有簡單可行的現成方法,不像python能夠直接decode / encode。
找的時候有一個帖子給了啟示: java預設用Unicode儲存String,所以直接轉成某種編碼的byte的同時,就已經轉成了該編碼的encoding。
於是找了個例子,
天安門 對應的gbk碼是:
%CC%EC%B0%B2%C3%C5
於是轉一下
byte[] bytes = source.getBytes("GBK");
再計算下補碼(還是反碼)神馬的。
for(byte b : bytes) { sb.append("%" + Integer.toHexString((b & 0xff)).toUpperCase()); }
就得到了上述的CC EC B0 B2 C3 C5
按照格式塞進去百分號,大功告成。完整的函式如下:
public static String toGBK(String source) throws UnsupportedEncodingException { StringBuilder sb = new StringBuilder(); byte[] bytes = source.getBytes("GBK"); for(byte b : bytes) { sb.append("%" + Integer.toHexString((b & 0xff)).toUpperCase()); } return sb.toString(); }
補:
public class Tranform { public static void main(String args[]) throws Exception { String chineseStr="我們"; Transform t=new Transform(); System.out.println(t.Chinese2UTF_8(chineseStr)); System.out.println(t.Chinese2GBK(chineseStr)); System.out.println(t.GBK2Chinese(t.Chinese2GBK(chineseStr))); } } class Transform { //中文轉換成UTF-8編碼(16進位制字串),每個漢字3個位元組 public String Chinese2UTF_8(String chineseStr)throws Exception { StringBuffer utf8Str = new StringBuffer(); byte[] utf8Decode = chineseStr.getBytes("utf-8"); for (byte b : utf8Decode) utf8Str.append(Integer.toHexString(b&0xFF)); return utf8Str.toString().toUpperCase(); } //中文轉換成GBK碼(16進位制字串),每個漢字2個位元組 public String Chinese2GBK(String chineseStr)throws Exception { StringBuffer GBKStr = new StringBuffer(); byte[] GBKDecode = chineseStr.getBytes("gbk"); for (byte b : GBKDecode) GBKStr.append(Integer.toHexString(b&0xFF)); return GBKStr.toString().toUpperCase(); } //16進位制GBK字串轉換成中文 public String GBK2Chinese(String GBKStr)throws Exception{ byte[] b = HexString2Bytes(GBKStr); String chineseStr = new String(b, "gbk");//輸入引數為位元組陣列 return chineseStr; } //把16進位制字串轉換成位元組陣列 public byte[] HexString2Bytes(String hexStr) { byte[] b = new byte[hexStr.length() / 2]; for (int i = 0; i < b.length; i++) b[i]=(byte) Integer.parseInt(hexStr.substring(2*i,2*i+2),16); return b; } //把位元組陣列轉換成16進位制字串 public static final String bytesToHexString(byte[] byteArray){ StringBuffer hexStr = new StringBuffer(byteArray.length*2); for (int i = 0; i < byteArray.length; i++) { String sTemp= Integer.toHexString(0xFF& byteArray[i]); int j=0; while(j<2-sTemp.length()) {sTemp="0"+sTemp;j++;} hexStr.append(sTemp.toUpperCase()); } return hexStr.toString(); } }
相關推薦
Java中文轉GBK碼
遇到一個需求,一個介面的呼叫時,需要將中文轉成對應的GBK碼,然後發請求呼叫,大概搜了下,貌似沒有簡單可行的現成方法,不像python能夠直接decode / encode。 找的時候有一個帖子給了啟示: java預設用Unicode儲存String,所以直接轉成
js java中文轉碼 適用於用url傳遞中文引數
第一種方法: 解決方法如下:1、在JS裡對中文引數進行兩次轉碼var login_name = document.getElementById("loginname").value; login_name = encodeURI(login_name); login_
java中文轉碼
request.setCharacterEncoding("UTF-8"); 對字串變數str進行轉碼,程式碼如下: str = new String(str.getBytes("ISO88
Java中文轉碼的例子,用來對付亂碼
public class HelloWorld { public static void main(String[] argv){ try{ System.out.println("中文");//1 System.out.
java 中文轉Unicode 以及 Unicode轉中文
package com.sun;public class Snippet { public static void main(String[] args) { String cn = "你";&n
Java中文與ASCII碼的轉換
今天在研究Java中編碼的時候,看到了Java中ascii碼的強大。寫了一個CoderUtils.java,以後會擴充套件它。 package com.xingxd.study.test; import java.io.File; import java.io.File
java 中文和Unicode碼 互相轉換
LZ很想像其他大牛一樣,書寫自己的部落格,但是怎奈何才疏學淺,始終沒有建立自己的篇幅文章,LZ也是個有夢想的人,那麼就讓原創的夢想在此起航吧。 中文和Unicode碼 互相轉換 package test.com.gjob.services; import java.io.
java 檔案轉碼(gb2315,gbk,utf-8)csv,excel
最近做資料處理,需要將爬取的資料入庫,但是演算法提供的資料編碼格式和資料庫總是有出入,導致匯入的資料亂碼,所以寫一個轉碼程式,將檔案編碼轉為和資料庫一致。 package com.bjk.transcode; import java.io.FileInputStream; import j
Java 中文字串編碼之GBK轉UTF-8
寫過兩篇關於編碼的文章了,以為自己比較瞭解編碼了呢?! 結果今天又結結實實的上了一課。 以前轉來轉去解決的問題終歸還是簡單的情形。即iso-8859-1轉utf-8,或者iso-8859-1轉gbk,gb2312之類。這種無損轉換,一行程式碼就搞定。 今天遇到了gbk轉utf-8。無論怎麼轉,都是亂碼。 一
java提供的把中文轉化為ascII碼(*.properties 檔案配置使用)的方法
在 *.properties 的配置檔案中, 中文是不能在裡面寫的, 要把中文轉成acsII碼 1, 在cmd命令頁面進入到java安裝目錄的bin目錄下: C:\Users\Administrator>cd C:\Program Files\Java\
一些JAVA中文的轉碼問題
對JAVA中文過濾的問題: 原來很長一段時間被JAVA的中文亂碼所困擾,不過在多次實踐後總算找到了較好解決方法,首先在頁面裡的 “charset” 設定上統一用 “GB2312”或“GBK” ,不過我推薦最好使用GBK,對中文支援比較好,然後是增加過濾器。 原始碼示
java中url中文轉碼問題
public static void main(String[] args){ try { System.out.println("中文");//1 System.out.println("中文".getBytes(
PHP json_encode函數使用後多出來首尾的引號、中文轉碼問題
中文 一起 引號 style 字符 之前 tro 去掉 還要 part 1 一般使用時,json_encode會和json_decode一起使用,decode之後和encode之前會一模一樣,不用擔心; 如果確實需要在decode之前去掉這對引號也很簡單,使用trim()函
Java中的代碼點和代碼單元(轉)
swing enter 錯誤 字體 消息 關系 小文本 開發人員 界面 文章來源:http://blog.csdn.net/weizhaozhe/article/details/3909079 這篇文章講的很細,但是對於初學者也很難理解,在後面的筆記中,我會陳述自己的簡單
java 中文與unicode互轉
true format param cte _id col rms AI deb public class FontUtil { public static void main(String[] args) { System.out.pr
javascript中中文轉碼的方法
doc strong 常見 前後端交互 passport cape 傳遞參數 b數 lang js對文字進行編碼涉及3個函數: escape,encodeURI,encodeURIComponent,相應3個解碼函數:unescape,decodeURI,decodeURI
java 視訊轉碼
前臺通過ajaxfileupload外掛上傳提價資料: $.ajaxFileUpload({ url : "${ctx}/h5/h5preventiveEdu/updateEdu",//增加與修改通用 secureuri : false
【Java】積分手機端-中文轉拼音
目錄 前因 啟示 啟示 後果 前因 這次小鹹兒因為專案的業務邏輯的需求,在一個新增頁面時,需要根據新增的中文名稱,生成一個拼音欄位值,存入到資料庫中。 如圖,手機端新增頁面: 啟示
轉:Java實現簡訊驗證碼(阿里雲服務商)
1.先去阿里雲開通簡訊服務: 2.新增模板及簽名:需要稽核,個人賬戶稽核就幾分鐘就OK 先解釋一下模板及簽名: 標準參照:https://help.aliyun.com/document_detail/55324.html?spm=5176.sms-sign.0
jAVA/JS 轉碼解碼(URLEncoder.encode decodeURIComponent)
引用包:import java.net.URLDecoder; Java解碼: String s = URLDecoder.decode(URLDecoder.decode(templateObj.getString("templateContent"), "UTF-8