java中String\十六进制String\byte[]之间相互转换函数
- java二进制,字节数组,字符,十六进制,BCD编码转换2007-06-07 00:17/** *//**
- * 把16进制字符串转换成字节数组
- * @param hex
- * @return
- */
- public static byte[] hexStringToByte(String hex) {
- int len = (hex.length() / 2);
- byte[] result = new byte[len];
- char[] achar = hex.toCharArray();
- for (int i = 0; i < len; i++) {
- int pos = i * 2;
- result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
- }
- return result;
- }
- private static byte toByte(char c) {
- byte b = (byte) "0123456789ABCDEF".indexOf(c);
- return b;
- }
- /** *//**
- * 把字节数组转换成16进制字符串
- * @param bArray
- * @return
- */
- public static final String bytesToHexString(byte[] bArray) {
- StringBuffer sb = new StringBuffer(bArray.length);
- String sTemp;
- for (int i = 0; i < bArray.length; i++) {
- sTemp = Integer.toHexString(0xFF & bArray[i]);
- if (sTemp.length() < 2)
- sb.append(0);
- sb.append(sTemp.toUpperCase());
- }
- return sb.toString();
- }
- /** *//**
- * 把字节数组转换为对象
- * @param bytes
- * @return
- * @throws IOException
- * @throws ClassNotFoundException
- */
- public static final Object bytesToObject(byte[] bytes) throws IOException, ClassNotFoundException {
- ByteArrayInputStream in = new ByteArrayInputStream(bytes);
- ObjectInputStream oi = new ObjectInputStream(in);
- Object o = oi.readObject();
- oi.close();
- return o;
- }
- /** *//**
- * 把可序列化对象转换成字节数组
- * @param s
- * @return
- * @throws IOException
- */
- public static final byte[] objectToBytes(Serializable s) throws IOException {
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- ObjectOutputStream ot = new ObjectOutputStream(out);
- ot.writeObject(s);
- ot.flush();
- ot.close();
- return out.toByteArray();
- }
- public static final String objectToHexString(Serializable s) throws IOException{
- return bytesToHexString(objectToBytes(s));
- }
- public static final Object hexStringToObject(String hex) throws IOException, ClassNotFoundException{
- return bytesToObject(hexStringToByte(hex));
- }
- /** *//**
- * @函数功能: BCD码转为10进制串(阿拉伯数据)
- * @输入参数: BCD码
- * @输出结果: 10进制串
- */
- public static String bcd2Str(byte[] bytes){
- StringBuffer temp=new StringBuffer(bytes.length*2);
- for(int i=0;i<bytes.length;i++){
- temp.append((byte)((bytes[i]& 0xf0)>>>4));
- temp.append((byte)(bytes[i]& 0x0f));
- }
- return temp.toString().substring(0,1).equalsIgnoreCase("0")?temp.toString().substring(1):temp.toString();
- }
- /** *//**
- * @函数功能: 10进制串转为BCD码
- * @输入参数: 10进制串
- * @输出结果: BCD码
- */
- public static byte[] str2Bcd(String asc) {
- int len = asc.length();
- int mod = len % 2;
- if (mod != 0) {
- asc = "0" + asc;
- len = asc.length();
- }
- byte abt[] = new byte[len];
- if (len >= 2) {
- len = len / 2;
- }
- byte bbt[] = new byte[len];
- abt = asc.getBytes();
- int j, k;
- for (int p = 0; p < asc.length()/2; p++) {
- if ( (abt[2 * p] >= '0') && (abt[2 * p] <= '9')) {
- j = abt[2 * p] - '0';
- } else if ( (abt[2 * p] >= 'a') && (abt[2 * p] <= 'z')) {
- j = abt[2 * p] - 'a' + 0x0a;
- } else {
- j = abt[2 * p] - 'A' + 0x0a;
- }
- if ( (abt[2 * p + 1] >= '0') && (abt[2 * p + 1] <= '9')) {
- k = abt[2 * p + 1] - '0';
- } else if ( (abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z')) {
- k = abt[2 * p + 1] - 'a' + 0x0a;
- }else {
- k = abt[2 * p + 1] - 'A' + 0x0a;
- }
- int a = (j << 4) + k;
- byte b = (byte) a;
- bbt[p] = b;
- }
- return bbt;
- }
- /** *//**
- * @函数功能: BCD码转ASC码
- * @输入参数: BCD串
- * @输出结果: ASC码
- */
- public static String BCD2ASC(byte[] bytes) {
- StringBuffer temp = new StringBuffer(bytes.length * 2);
- for (int i = 0; i < bytes.length; i++) {
- int h = ((bytes[i] & 0xf0) >>> 4);
- int l = (bytes[i] & 0x0f);
- temp.append(BToA[h]).append( BToA[l]);
- }
- return temp.toString() ;
- }
- /** *//**
- * MD5加密字符串,返回加密后的16进制字符串
- * @param origin
- * @return
- */
- public static String MD5EncodeToHex(String origin) {
- return bytesToHexString(MD5Encode(origin));
- }
- /** *//**
- * MD5加密字符串,返回加密后的字节数组
- * @param origin
- * @return
- */
- public static byte[] MD5Encode(String origin){
- return MD5Encode(origin.getBytes());
- }
- /** *//**
- * MD5加密字节数组,返回加密后的字节数组
- * @param bytes
- * @return
- */
- public static byte[] MD5Encode(byte[] bytes){
- MessageDigest md=null;
- try {
- md = MessageDigest.getInstance("MD5");
- return md.digest(bytes);
- } catch (NoSuchAlgorithmException e) {
- e.printStackTrace();
- return new byte[0];
- }
java中String\十六进制String\byte[]之间相互转换函数的更多相关文章
- Java中InputStream和String之间的转化
https://blog.csdn.net/lmy86263/article/details/60479350 在Java中InputStream和String之间的转化十分普遍,本文主要是总结一下转 ...
- java中Integer 和String 之间的转换
java中Integer 和String 之间的转换 将数组转换成字符串:char[] array = {'a','b','c','d','e'};String str = new String(ar ...
- 【Java】Java中常用的String方法
本文转载于:java中常用的String方法 1 length()字符串的长度 String a = "Hello Word!"; System.out.println(a.len ...
- Java中如何将String转成Date
Java中如何将String转成Date 最近在开发Json数据反序列化为Java对象的时候发现spring mvc 和 Jackson 对Date类型对支持不是特别好,虽然在Java对象序列化为Js ...
- java中如何将string 转化成long
1.Java中如何将string 转化成long long l = Long.parseLong([String]); 或 long l = Long.parseLong([String],[int ...
- Java 中要将 String 类型转化为 int 类型
在 Java 中要将 String 类型转化为 int 类型时,需要使用 Integer 类中的 parseInt() 方法或者 valueOf() 方法进行转换. 例1: 1 2 3 4 5 6 S ...
- Java中几种常用数据类型之间转换的方法
Java中几种常用的数据类型之间转换方法: 1. short-->int 转换 exp: short shortvar=0; int intvar=0; shortvar= (short) in ...
- IRandomAccessStream, IBuffer, Stream, byte[] 之间相互转换
/* * 用于实现 IRandomAccessStream, IBuffer, Stream, byte[] 之间相互转换的帮助类 */ using System;using System.IO;us ...
- <Java中的继承和组合之间的联系和区别>
//Java中的继承和组合之间的联系和区别 //本例是继承 class Animal { private void beat() { System.out.println("心胀跳动...& ...
随机推荐
- Inception V3 的 tensorflow 实现
tensorflow 官方给出的实现:models/inception_v3.py at master · tensorflow/models · GitHub 1. 模型结构 首先来看 Incept ...
- ElasticSearch 在Hadoop生态圈的位置
它的位置非常清晰,直接贴图. 更详细点,见
- Vectorized implementation
Vectorization Vectorization refers to a powerful way to speed up your algorithms. Numerical computin ...
- Whitening
The goal of whitening is to make the input less redundant; more formally, our desiderata are that ou ...
- easyui 前端实现分页 复制就能用
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 用Vue+axios写一个实时搜索
刚刚在学vue,试着写了一个实时搜索文件. 思路:1.input 通过v-model绑定.2.通过watch检测输入结果变化.3根据结果变化从api调用不同的数据. 代码如下: <!DOCTYP ...
- 经典的横线中间文字css布局---flex布局
html: <div class="title"> <div class="line"></div> <div cla ...
- 洛谷 P2105 K皇后
P2105 K皇后 题目描述 小Z最近捡到了一个棋盘,他想在棋盘上摆放K个皇后.他想知道在他摆完这K个皇后之后,棋盘上还有多少了格子是不会被攻击到的. (Ps:一个皇后会攻击到这个皇后所在的那一行,那 ...
- js11--js与C++、java异同
function F(){ this.name = "111"; this.say = function(){alert(222);} alert(333); } var f1 = ...
- 在Qt Creator的项目中添加头文件和库
在Qt Creator中的工程中,工程通过.pro文件管理. 额外需要连接的连接库unix:LIBS += -L your_lib_path -lyour_libwin32:LIBS += your_ ...