日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

GBK 汉字编码转换

發(fā)布時(shí)間:2023/12/14 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 GBK 汉字编码转换 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

/**
* 描述:漢字轉(zhuǎn)GBK碼
* @param word
* @return
*/
public String wordToGBk (String word) throws UnsupportedEncodingException {
String[] wordArray;
String GBK="";
wordArray = word.split("");
for (int i=0;i<wordArray.length;i++){
GBK += URLEncoder.encode(wordArray[i], “GBK”).replaceAll("\%","");
if (i != wordArray.length-1){
GBK += “,”;
}
}
return GBK;
}

/*** 描述:GBK轉(zhuǎn)漢字* @param GBK* @return*/ public String GBKToWord (String GBK){String result = new String();try {/*GBK轉(zhuǎn)漢字*/byte[] bytes = new byte[GBK.length() / 2];for(int i = 0; i < bytes.length; i ++){byte high = Byte.parseByte(GBK.substring(i * 2, i * 2 + 1), 16);byte low = Byte.parseByte(GBK.substring(i * 2 + 1, i * 2 + 2), 16);bytes[i] = (byte) (high << 4 | low);}result = new String(bytes, "gbk");} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}return result; }/*** 描述:漢字轉(zhuǎn)字庫信息* @param* @return* @throws IOException*/ public byte[] wordToByte(String word) throws IOException {//16*16點(diǎn)陣的漢字占用32個(gè)字節(jié)byte[] cbuf = new byte[32];try {//這個(gè)是取點(diǎn)陣的“位”//char[] key = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};byte[] bytes = word.getBytes("GB2312");//這中寫法是把byte轉(zhuǎn)成intint segNum = bytes[0] & 0xff;int bitNum = bytes[1] & 0xff;//算出這個(gè)字在字庫文件中的偏移量,注意32是表示16*16像素的字站32個(gè)字節(jié)int offset = (94 * (segNum - 0xa0 - 1) + (bitNum - 0xa0 - 1)) * 32;/* System.out.println("offset = " + offset);*///讀取點(diǎn)陣字庫文件,需要按需修改為你電腦上實(shí)際字庫的絕對(duì)地址ClassPathResource classPathResource = new ClassPathResource("HZK16C");InputStream inputStream = classPathResource.getInputStream();//跳過offset個(gè)字節(jié),讀取漢字占用的32個(gè)字節(jié)inputStream.skip(offset);inputStream.read(cbuf);}catch (Exception e){e.printStackTrace();}return cbuf; }public String BinaryToHexString(byte[] bytes){String hexStr = "0123456789ABCDEF";String result = "";String hex = "";for(int i=0;i<bytes.length;i++){//字節(jié)高4位hex = String.valueOf(hexStr.charAt((bytes[i]&0xF0)>>4));//字節(jié)低4位hex += String.valueOf(hexStr.charAt(bytes[i]&0x0F));result +=hex;}return result; }/**** @descripton 二進(jìn)制字符串轉(zhuǎn)Byte數(shù)組* @author LP* @date 2020/6/28 17:04* @return*/ public byte[] BinaryToByte(String binStr){String[] temp = binStr.split(",");byte[] b = new byte[temp.length];for (int i = 0; i < b.length; i++) {b[i] = Long.valueOf(temp[i], 2).byteValue();}return b; }

總結(jié)

以上是生活随笔為你收集整理的GBK 汉字编码转换的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。