在Oracle中表之间关联需要有包含有中文的字符串转换为16进制,在网上找了下,比较纤细点的在原文链接
参考链接:https://www.cnblogs.com/carryLess/p/6889378.html
代码样例:
package com.eastcom.two.oracle.hex;
public class StrWithHexTransform {
public static void main(String[] args) {
String str = "hello 我是中國人";
String str2HexStr = str2HexStr(str);
System.out.println(str2HexStr);
String hexStr2Str = hexStr2Str(str2HexStr);
System.out.println(hexStr2Str);
}
/**
* 字符串转换成为16进制(无需Unicode编码)
*
* @param str
* @return
*/
public static String str2HexStr(String str) {
char[] chars = "0123456789ABCDEF".toCharArray();
StringBuilder sb = new StringBuilder("");
byte[] bs = str.getBytes();
int bit;
for (int i = 0; i < bs.length; i++) {
bit = (bs[i] & 0x0f0) >> 4;
sb.append(chars[bit]);
bit = bs[i] & 0x0f;
sb.append(chars[bit]);
// sb.append(' ');
}
return sb.toString().trim();
}
/**
* 16进制直接转换成为字符串(无需Unicode解码)
*
* @param hexStr
* @return
*/
public static String hexStr2Str(String hexStr) {
String str = "0123456789ABCDEF";
char[] hexs = hexStr.toCharArray();
byte[] bytes = new byte[hexStr.length() / 2];
int n;
for (int i = 0; i < bytes.length; i++) {
n = str.indexOf(hexs[2 * i]) * 16;
n += str.indexOf(hexs[2 * i + 1]);
bytes[i] = (byte) (n & 0xff);
}
return new String(bytes);
}
}
68656C6C6F20E68891E698AFE4B8ADE59C8BE4BABA
hello 我是中國人
OK!
打成jar表是其成为Oracle中的函数可以参考链接:
http://blog.csdn.net/liuxiangke0210/article/details/78326901
-----------------------------------------------------------------------
编码格式有多种方式
以下实例是gbk编码方式
package com.eastcom.two.oracle.hex;
import java.io.UnsupportedEncodingException;
public class StrWithHexTransform {
public static void main(String[] args) {
String str = "【IRMS-040-170308-00779】JS/中博学院宿舍楼二期-ONT1061";
String bytes = "a1be49524d532d3034302d3137303330382d3030373739a1bf4a532fd6d0b2a9d1a7d4bacbdec9e1c2a5b6fec6da2d4f4e5431303631";
String bytes2 = "a1be49524d532d3034302d3137303630342d3031313035a1bf4a532fd6d0b2a9d1a7d4bacbdec9e1c2a5b6fec6da2d4f4e5431313132";
// System.out.println(str2HexStr(str, "#"));
// System.out.println(str2HexStr(str));
System.out.println(hexStr2Str(bytes));
System.out.println(hexStr2Str(bytes2));
}
public static String str2HexStr(String str, String separator) {
StringBuffer sb = new StringBuffer();
try {
byte[] bytes = str.getBytes("gb2312");
sb.append(byteToString(bytes[0]));
for (int i = 1; i < bytes.length; i++) {
sb.append(separator);
sb.append(byteToString(bytes[i]));
}
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString().trim();
}
/**
* 中文编码 转化为gb2312编码16进制
*
* @param str
* @return
*/
public static String str2HexStr(String str) {
StringBuffer sb = new StringBuffer();
try {
byte[] bytes = str.getBytes("gb2312");
sb.append(byteToString(bytes[0]));
for (int i = 1; i < bytes.length; i++) {
sb.append(byteToString(bytes[i]));
}
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString().trim();
}
/**
* gb2312编码16进制转换为中文
*
* @param str
* @return
*/
public static String hexStr2Str(String str) {
String string = str;
byte[] bytes = new byte[string.length() / 2];
for (int i = 0; i < bytes.length; i++) {
byte high = Byte.parseByte(string.substring(i * 2, i * 2 + 1), 16);
byte low = Byte.parseByte(string.substring(i * 2 + 1, i * 2 + 2), 16);
bytes[i] = (byte) (high << 4 | low);
}
try {
return new String(bytes, "gb2312");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
private static String byteToString(byte b) {
String str = Integer.toHexString(b);
if (str.length() > 2)
str = str.substring(str.length() - 2);
return str;
}
}
编码网站 : http://www.qqxiuzi.cn/bianma/zifuji.php