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. 读《HTML5与CSS3权威指南(上册)》笔记

    第二章 1.内容类型:“text/html”.DOCTYPE声明:<!DOCTYPE html>.指定字符编码:<meta charset="utf-8"> ...

  2. android开发关于popupwindow显示关闭的笔记

    一.方法一: popupWindow.setFocusable(false); //这样popupWindow无法获得焦点,无法处理popupWindow中的事件 设置MainActivity的onT ...

  3. mahout 安装

    1. 下载mahout-distribution-0.5.tar.gz 并解压: 2.配置环境变量: /etc/profile export MAHOUT_HOME=/home/mahout/ exp ...

  4. Windwos Server 2008: 当网卡有多个IP地址时,如何指定缺省地址?

    这实际是一个当应用向外发起连接时,协议栈对源IP地址的选择问题.如果你的应用没有显式绑定一个本地地址,协议栈会选择一个"最佳"的本地地址来使用. 从 Vista 之后这个选择策略发 ...

  5. Java多线程编程之同步器

    同步器 为每种特定的同步问题提供了解决方案 Semaphore Semaphore[信号标:旗语],通过计数器控制对共享资源的访问. 测试类: package concurrent; import c ...

  6. JavaScript如何判断参数为浮点型

    在codewars里,确实可以学到很多很酷的方法,例如这一次的题目是判断数字是否为浮点型.我一开始是想有没有原生的js方法,像isNaN(),isFinite(),在前者Infinity是不属于NaN ...

  7. CSS3之背景剪裁Background-clip

    CSS3之背景剪裁Background-clip是CSS3中新添加的内容.这个属性还是比较简单的,主要分五个属性值:border.padding.content.no-clip和text.下面将针对这 ...

  8. MATLAB remove outliers.

    Answer by Richard Willey on 9 Jan 2012 Hi Michael MATLAB doesn't provide a specific function to remo ...

  9. 去除Linq to Sql 查询结果中的空格

    原来的写法: Dim db = From city In DataContext1.AddressCity Where city.ProvinceID = dgvAddress(2, e.RowInd ...

  10. Android学习第一课

    首先看一个android项目中各个包的作用 以下看几个经常使用的控件: 1. TextView 显示文本框控件 2. EditText 输入文本框 TextView控件经常使用属性: id----控件 ...