普通字符串与Hex编码字符串之间转换
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编码字符串之间转换的更多相关文章
- 中文字符串和UTF-8编码字符串相互转换
中文字符串和UTF-8编码字符串相互转换 //UTF字符转换 var UTFTranslate = { Change: function(pValue) { ) { ).replace(/(%u)(\ ...
- Python: 在Unicode和普通字符串之间转换
Unicode字符串可以用多种方式编码为普通字符串, 依照你所选择的编码(encoding): <!-- Inject Script Filtered --> Toggle line nu ...
- Python3 字符串与hex之间的相互转换
在字符串转换上,python2和python3是不同的,在查看一些python2的脚本时候,总是遇到字符串与hex之间之间的转换出现问题,记录一下解决方法. 1. 在Python2.7.x上,hex字 ...
- Python——在Unicode和普通字符串之间转换
1.1. 问题 Problem You need to deal with data that doesn't fit in the ASCII character set. 你需要处理不适合用ASC ...
- python ----列表、字符串、元组之间转换小结
字符串转换列表 li =list("adfadfsf") #内部使用for循环 print(li) #输出结果:['a', 'd', 'f', 'a', 'd', 'f', 's' ...
- SSIS无法在unicode和非unicode 字符串数据类型之间转换
场景:SSIS从oracle抽到sqlserver,一个表对表到数据仓库ODS层的抽取,没有任何逻辑结果遇到问题: SSIS无法在unicode和非unicode 字符串数据类型之间转换 如下图2个字 ...
- 图片和base64编码字符串 互相转换,图片和byte数组互相转换
图片和base64编码字符串 互相转换 import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import java.io.*; ...
- char*、string、CString各种字符串之间转换
参考博客: http://blog.csdn.net/luoweifu/article/details/20242307 http://blog.csdn.net/luoweifu/article/d ...
- SSIS 无法在 unicode 和非 unicode 字符串数据类型之间转换
最近在学SSIS,遇到一个问题,把平面文件源的数据导入到EXCEL中. 平面文件源的对象是CSV,读进来的PhoneNumber是 DT_STR 然后倒入Excel 对应列建立的是longtext 一 ...
随机推荐
- web服务器分析与设计(五)--一些总结
随着年龄与经验的增加,对于软件方面的分析与设计也会有一些新的认识.下面做个近期的总结: 1,关于到底用不用作设计的问题: 在最近两个公司,原有人马是不会作设计(我自己的感觉),也察觉不到作设计的任何冲 ...
- 解决问题的步骤(第一篇)-- clwu
现象: 之前打开IE 还是正常的,但前几天开始打开就不正常了,报错如下. 处理(别人的)问题的步骤: 百度一下 0xc0000018,没有什么有用信息. 看一下程序(IE)启动时做了些什么. 怎么看 ...
- Bmob第三方登录详解
Bmob第三方登录详解 Bmob 第三方登录 简介 本文主要介绍新浪微博,QQ,微信的登录接入以及如何配合BmobSDK中的第三方登录功能实现第三方登录. 在使用之前请先按照快速入门创建好可以调用Bm ...
- Spring EL ternary operator (if-then-else) example
Spring EL supports ternary operator , perform "if then else" conditional checking. For exa ...
- Codeforces Beta Round #7 C. Line (扩展欧几里德)
题目链接:http://codeforces.com/problemset/problem/7/C 给你一个直线方程,有整数解输出答案,否则输出-1. 扩欧模版题.这里有讲解:http://www.c ...
- php把时间格式化
如题,把如 2013-6-12 12:00 格式化为 2013-6--12 可以先将时间转换下,然后重新将时间格式化显示: echo date("Y-m-d", strtotime ...
- 集合引入(ArrayList、LinkedList)
1.引入 代替数组固定大小操作不变 2.ArrayList 常用的操作(add,remove) 3.LinkedList 能实现一些特殊的操作(pop)
- [Oralce]Oralce格式化日期
字符串转日期 1.to_date(日期,'yyyyMMdd') 2.to_date(日期,'yyyyMMdd hh24miss')日期转字符串 TO_CHAR(SYSDATE, 'YYYY-MM-DD ...
- SOS 调试扩展 (SOS.dll)
http://blog.csdn.net/cslie/article/details/2158780 SOS 调试扩展 (SOS.dll) 提供公共语言运行时(CLR)内部环境的有关信息,帮助你在Wi ...
- C# random(number)
C#随机函数Random()的用法 出自:http://www.cnblogs.com/wang726zq/archive/2012/04/28/2474711.html http://blog.cs ...