`

java中String\十六进制String\byte[]之间相互转换函数

    博客分类:
  • Java
 
阅读更多
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]; 
    } 

 

 

分享到:
评论
1 楼 wly20110420 2016-05-10  
很详细,谢谢分享!

相关推荐

    C# char[]与string byte[]与string之间的转换详解

    1、char[]与string之间的转换 //string 转换成 Char[] string str=hello; char[] arr=str.ToCharArray(); //Char[] 转换成 string string str1 = new string(arr); 2、byte[]与string之间的转化 string str = 你好...

    PHP字符串和十六进制如何实现互相转换

    今天在做项目中,因为要调用别人网站的接口,结果需要对请求和返回的时间进行十六进制加密处理,于是在网上查了下资料谢了一个转换Demo做个记录。 如果在TP下使用可以将下面函数放到common.php中 一,加密函数 &lt;...

    C#中Byte转换相关的函数

    1、将一个对象转换为byte对象 public static byte GetByte(object o)...2、将一个十六进制字符串转换为byte对象,字符串以0x开头 public static byte GetByteFormHex(string hexValue) { try { return Convert.ToBy

    c++一些基本的处理函数

    //基本的处理函数,不引用其它任何头文件 typedef unsigned char BYTE; extern "C" { BYTE __stdcall hextobcd(BYTE *buf); void __stdcall bcdtohex(BYTE b,BYTE *buf); bool __stdcall hextostring(unsigned char ...

    jdk-8u241-windows-i586 (1).exe

    我们看到的加密后的摘要是十六进制的,而类返回给我们的是byte数组,我们需要byte[]转换成十六进制字符串。具体方法是:用HexBinaryAdapter类,他有一个marshal(byte[] bytes)方法,他可以将byte[]转换为String。 ...

    关于PHP中字符串与多进制转换函数的实例代码

    转换函数 /** * [字符串转换为(2,8,16进制)ASCII码] * @param string $str [待处理字符串] * @param boolean $encode [字符串转换为ASCII|ASCII转换为字符串] * @param string $intType [2,8,16进制标示] * @...

    delphi通用函数单元一

    function ByteToBin(Value: Byte): string; {测试通过} {* 字节转二进制串} function StrRight(Str: string; Len: Integer): string; {测试通过} {* 返回字符串右边的字符 Examples: StrRight('ABCEDFG',3); 返回:'...

    史上最全Java面试题目大集合

    整理了网上的一些java面试题目,很全很强大 面向对象的特征有哪些方面 1. 抽象:抽象就是忽略一个主题中与当前目标2. 无关的那些方面,3. 以便更充分地注意与当前目标4. 有关的方面。抽象并不5. 打算了解全部问题...

    Unicode下CString与char*之间的转换(vs2008绝对实用)

    在VS2008中,默认的字符集形式是Unicode,但在VC6.0等工程中,默认的字符集形式是多...这里总结了在VS2008环境中 Unicode字符集下CString和char *之间相互转换的几种方法,其实也就是Unicode字符集与MBCS字符集转换。

    pb9 写的字符串转化为数组函数

    将字符串转化为数组,资源为pb9导出来的文本文件,可以直接导入pb9,也可以打开查看

    使用Python进行二进制文件读写的简单方法(推荐)

    python没有二进制类型,但可以存储二进制类型的数据,就是用string字符串类型来存储二进制数据,这也没关系,因为string是以1个字节为单位的。 import struct a=12.34 #将a变为二进制 bytes=struct.pack(‘i’,a) ...

    C#中string与byte[]的转换帮助类-.NET教程,C#语言

    主要实现了以下的函数 代码中出现的sidle是我的网名。 /**//* * @author wuerping * @version 1.0 * @date 2004/11/30 * @description: */ using system; using system.text; namespace ...

    不用string.h库函数的,方便易用的字符串处理函数,减少库带来代码量

    减少库的使用,解决那些需要小代码量,但苦恼于没有简易的字符串处理函数的郁闷 char *itoa_private(int val, char *buf, unsigned radix);//整数转字符串 int my_isdigit(int ch);//判断字符是否为数字 long long ...

    VBScript 语言参考中文手册CHM

    Hex 函数 返回表示数的十六进制值的字符串。 HelpContext 属性 设置或返回帮助文件中某主题的上下文 ID。 HelpFile 属性 设置或返回帮助文件的全路径。 Hour 函数 返回小时数,取值范围为 0 至 23。 If...Then.....

    java数组作业

    请参照String类,对一个封装了字节数组的类提供如下函数 public class MyByteArray { private byte[] data; public MyByteArray(byte[] data) { this.data = data; } //返回字节数据b在字节数组中的位置 ...

    asp.net 字符串、二进制、编码数组转换函数

    1.字符串转二进制数组 string content=”这是做个测试!”; System.Text.UnicodeEncoding converter = new System.Text.UnicodeEncoding(); byte[] byteArr = converter.GetBytes(content); 2.二进制数组转为字符串 ...

    Python3中内置类型bytes和str用法及byte和string之间各种编码转换 问题

    Python 3最重要的新特性大概要算是对文本和二进制数据作了更为清晰的区分。文本总是Unicode,由str类型表示,二进制数据则由bytes类型表示。Python 3不会以任意隐式的方式混用str和bytes,正是这使得两者的区分特别...

    VBSCRIPT中文手册

    Hex 函数 返回表示数的十六进制值的字符串。 HelpContext 属性 设置或返回帮助文件中某主题的上下文 ID。 HelpFile 属性 设置或返回帮助文件的全路径。 Hour 函数 返回小时数,取值范围为 0 至 23。 If...Then.....

    VBSCRIP5 -ASP用法详解

    Hex 函数 返回表示数的十六进制值的字符串。 HelpContext 属性 设置或返回帮助文件中某主题的上下文 ID。 HelpFile 属性 设置或返回帮助文件的全路径。 Hour 函数 返回小时数,取值范围为 0 至 23。 If...Then.....

Global site tag (gtag.js) - Google Analytics