import java.io.UnsupportedEncodingException;
import org.apache.commons.codec.binary.Hex; public class Example {
/**
* 将普通字符串转换成Hex编码字符串
*
* @param dataCoding 编码格式,15表示GBK编码,8表示UnicodeBigUnmarked编码,0表示ISO8859-1编码
* @param realStr 普通字符串
* @return Hex编码字符串
* @throws UnsupportedEncodingException
*/
public static String encodeHexStr(int dataCoding, String realStr) {
String hexStr = null;
if (realStr != null) {
try {
if (dataCoding == 15) {
hexStr = new String(Hex.encodeHex(realStr.getBytes("GBK")));
} else if ((dataCoding & 0x0C) == 0x08) {
hexStr = new String(Hex.encodeHex(realStr.getBytes("UnicodeBigUnmarked")));
} else {
hexStr = new String(Hex.encodeHex(realStr.getBytes("ISO8859-1")));
}
} catch (UnsupportedEncodingException e) {
System.out.println(e.toString());
}
}
return hexStr;
} /**
* 将Hex编码字符串转换成普通字符串
*
* @param dataCoding 反编码格式,15表示GBK编码,8表示UnicodeBigUnmarked编码,0表示ISO8859-1编码
* @param hexStr Hex编码字符串
* @return 普通字符串
*/
public static String decodeHexStr(int dataCoding, String hexStr) {
String realStr = null;
try {
if (hexStr != null) {
if (dataCoding == 15) {
realStr = new String(Hex.decodeHex(hexStr.toCharArray()), "GBK");
} else if ((dataCoding & 0x0C) == 0x08) {
realStr = new String(Hex.decodeHex(hexStr.toCharArray()), "UnicodeBigUnmarked");
} else {
realStr = new String(Hex.decodeHex(hexStr.toCharArray()), "ISO8859-1");
}
}
} catch (Exception e) {
System.out.println(e.toString());
} return realStr;
} }

普通字符串与Hex编码字符串之间转换的更多相关文章

  1. 中文字符串和UTF-8编码字符串相互转换

    中文字符串和UTF-8编码字符串相互转换 //UTF字符转换 var UTFTranslate = { Change: function(pValue) { ) { ).replace(/(%u)(\ ...

  2. Python: 在Unicode和普通字符串之间转换

    Unicode字符串可以用多种方式编码为普通字符串, 依照你所选择的编码(encoding): <!-- Inject Script Filtered --> Toggle line nu ...

  3. Python3 字符串与hex之间的相互转换

    在字符串转换上,python2和python3是不同的,在查看一些python2的脚本时候,总是遇到字符串与hex之间之间的转换出现问题,记录一下解决方法. 1. 在Python2.7.x上,hex字 ...

  4. Python——在Unicode和普通字符串之间转换

    1.1. 问题 Problem You need to deal with data that doesn't fit in the ASCII character set. 你需要处理不适合用ASC ...

  5. python ----列表、字符串、元组之间转换小结

    字符串转换列表 li =list("adfadfsf") #内部使用for循环 print(li) #输出结果:['a', 'd', 'f', 'a', 'd', 'f', 's' ...

  6. SSIS无法在unicode和非unicode 字符串数据类型之间转换

    场景:SSIS从oracle抽到sqlserver,一个表对表到数据仓库ODS层的抽取,没有任何逻辑结果遇到问题: SSIS无法在unicode和非unicode 字符串数据类型之间转换 如下图2个字 ...

  7. 图片和base64编码字符串 互相转换,图片和byte数组互相转换

    图片和base64编码字符串 互相转换 import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import java.io.*; ...

  8. char*、string、CString各种字符串之间转换

    参考博客: http://blog.csdn.net/luoweifu/article/details/20242307 http://blog.csdn.net/luoweifu/article/d ...

  9. SSIS 无法在 unicode 和非 unicode 字符串数据类型之间转换

    最近在学SSIS,遇到一个问题,把平面文件源的数据导入到EXCEL中. 平面文件源的对象是CSV,读进来的PhoneNumber是 DT_STR 然后倒入Excel 对应列建立的是longtext 一 ...

随机推荐

  1. Tkinter教程之Frame篇

    本文转载自:http://blog.csdn.net/jcodeer/article/details/1811339 '''Tkinter教程之Frame篇'''#Frame就是屏幕上的一块矩形区域, ...

  2. Apache Rewrite常用设置说明

    例子: RewriteEngine on 打开引擎 RewriteRule test.html /test.php [L] RewriteRule test.html?$ /tianqi.php?s1 ...

  3. Apache Maven 入门

    Apache Maven 入门篇 ( 上 ) Apache Maven 入门篇 ( 下 ) ~$mvn archetype:generate -DgroupId=com.mycompany.hello ...

  4. C# 释放非托管资源

    C#中资源分为托管资源和非托管资源. 托管资源由垃圾回收器控制如何释放,不需要程序员过多的考虑(当然也程序员也可以自己释放). 非托管资源需要自己编写代码来释放.那么编写好的释放非托管资源的代码(释非 ...

  5. [转]Ubuntu下GitHub的使用

    转自Pythoner 本文将对Ubuntu下Git的安装,以及如何连接GitHub进行讲解. 1.环境 OS: Ubuntu13.04 64bitsGit: 1.8.1.2 2.Git安装 执行如下命 ...

  6. tomcat log

    $TOMCAT_HOME$/logs/ .out tomcat 启动后的输出日志 ,主要用于输出一些常规的东西,打印的info日志也会在这里输出. 修改tomcat生成的日志位置 在开发/测试环境,日 ...

  7. 第二百四十三天 how can I 坚持

    制定的计划完成不了了,好多问题啊.又想当然了,晚上加了会班. 今天雾霾好严重,一出地铁大裤衩怎么没了.雾霾爆表啊. 还好现在刮大风了. 准备看<芈mi月传>了. 睡觉.

  8. Gym 100507J Scarily interesting! (贪心)

    Scarily interesting! 题目链接: http://acm.hust.edu.cn/vjudge/contest/126546#problem/D Description This y ...

  9. Educational Codeforces Round 7 - E. Ants in Leaves

    题目链接:http://www.codeforces.com/contest/622/problem/E 题意是给你一棵树,1为根,每个叶子节点有一个蚂蚁,移动到一个邻接节点时间耗费为1,一个节点上不 ...

  10. Unity3D之游戏暂停制作方法记录

    在游戏开发中我们一般都需要涉及到一个功能:游戏暂停,但是这里指的暂停仅仅是核心模块的暂停,并不是整个游戏都暂停,比如一些UI和UI上的动画与特效是不能被暂停的,整个游戏都暂停了玩家该如何继续游戏呢. ...