- 浏览: 219209 次
- 性别:
- 来自: 上海
文章分类
最新评论
-
wly20110420:
很详细,谢谢分享!
java中String\十六进制String\byte[]之间相互转换函数 -
星星283320052:
...
随心畅言 -
贾懂凯:
...
随心畅言
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]; }
发表评论
-
@RequestMapping 用法详解之地址映射
2015-08-31 11:32 0前段时间项目中用到了RESTful模式来开发程序,但是当用P ... -
focus获取不到光标
2014-03-07 18:10 513focus第一能获取,重新调用不能获取光标,解决方案: i ... -
根据XML Schema生成java类
2014-02-26 13:41 0根据XML Schema生成java类 JAXB提供了 ... -
CLOB/BLOB与String互转。
2013-12-21 17:28 7513String s1="1231dsdgasd ... -
json转换
2013-11-27 17:03 660j1、DTO:Data Transfer Objec ... -
org.hibernate.LazyInitializationException: failed to lazily initialize a collect
2013-04-26 15:37 824这个异常:org.hibernate.LazyInitial ... -
String Stringbuffer和StringBuilder的区别?
2013-04-10 10:27 605java.lang.StringBuffer线程安全的可变字 ... -
获取当前时间的前一天和后一天
2013-01-17 19:36 1299package jp.co.hitachi_solution ... -
js 上传和下载本地文件
2012-11-29 15:24 1245function writeFile(path,data){ ... -
java 文件上传
2012-11-29 15:11 673//同步销单信息 function synchrony ... -
java 文件下载
2012-11-29 15:04 642//下载任务 public String synchronyT ... -
Specified VM install not found: type Standard VM, name jre
2012-11-29 14:10 1003编译Ant脚本时出错: "Specified VM ... -
JAVA实现HTTP服务器端
2012-10-30 15:37 1057用java socket实现了一个简单的http服务器, 可以 ... -
警告:不能读取 AppletViewer 的属性文件:
2012-10-30 15:04 628警告:不能读取 AppletViewer 的属性文件: C:\ ... -
SAXBuilder基本用法
2012-08-29 13:38 969xml为我们在网络交换 ... -
jdom学习:读取xml文件
2012-08-27 16:50 588用JDOM读取XML文件需先用org.jdom.input.S ... -
Java获取当前日期的前一个月,前一天的时间
2012-08-23 16:08 19710Calendar calendar = Calendar.ge ... -
Hql语句根据时间查询,如何查询昨天、今天、上月、本月的数据
2012-08-10 17:26 13322这个只要你能根据今天的时间算出你需要的时间就完全可以了 如下 ... -
cron 表达式的格式
2012-07-12 10:35 795cron 表达式的格式 Quartz cron 表达式的格式 ... -
BCD 转 String, String 转 BCD
2012-03-16 16:08 6908/// <summary> / ...
相关推荐
1、char[]与string之间的转换 //string 转换成 Char[] string str=hello; char[] arr=str.ToCharArray(); //Char[] 转换成 string string str1 = new string(arr); 2、byte[]与string之间的转化 string str = 你好...
今天在做项目中,因为要调用别人网站的接口,结果需要对请求和返回的时间进行十六进制加密处理,于是在网上查了下资料谢了一个转换Demo做个记录。 如果在TP下使用可以将下面函数放到common.php中 一,加密函数 <...
1、将一个对象转换为byte对象 public static byte GetByte(object o)...2、将一个十六进制字符串转换为byte对象,字符串以0x开头 public static byte GetByteFormHex(string hexValue) { try { return Convert.ToBy
//基本的处理函数,不引用其它任何头文件 typedef unsigned char BYTE; extern "C" { BYTE __stdcall hextobcd(BYTE *buf); void __stdcall bcdtohex(BYTE b,BYTE *buf); bool __stdcall hextostring(unsigned char ...
我们看到的加密后的摘要是十六进制的,而类返回给我们的是byte数组,我们需要byte[]转换成十六进制字符串。具体方法是:用HexBinaryAdapter类,他有一个marshal(byte[] bytes)方法,他可以将byte[]转换为String。 ...
转换函数 /** * [字符串转换为(2,8,16进制)ASCII码] * @param string $str [待处理字符串] * @param boolean $encode [字符串转换为ASCII|ASCII转换为字符串] * @param string $intType [2,8,16进制标示] * @...
function ByteToBin(Value: Byte): string; {测试通过} {* 字节转二进制串} function StrRight(Str: string; Len: Integer): string; {测试通过} {* 返回字符串右边的字符 Examples: StrRight('ABCEDFG',3); 返回:'...
整理了网上的一些java面试题目,很全很强大 面向对象的特征有哪些方面 1. 抽象:抽象就是忽略一个主题中与当前目标2. 无关的那些方面,3. 以便更充分地注意与当前目标4. 有关的方面。抽象并不5. 打算了解全部问题...
在VS2008中,默认的字符集形式是Unicode,但在VC6.0等工程中,默认的字符集形式是多...这里总结了在VS2008环境中 Unicode字符集下CString和char *之间相互转换的几种方法,其实也就是Unicode字符集与MBCS字符集转换。
将字符串转化为数组,资源为pb9导出来的文本文件,可以直接导入pb9,也可以打开查看
python没有二进制类型,但可以存储二进制类型的数据,就是用string字符串类型来存储二进制数据,这也没关系,因为string是以1个字节为单位的。 import struct a=12.34 #将a变为二进制 bytes=struct.pack(‘i’,a) ...
主要实现了以下的函数 代码中出现的sidle是我的网名。 /**//* * @author wuerping * @version 1.0 * @date 2004/11/30 * @description: */ using system; using system.text; namespace ...
减少库的使用,解决那些需要小代码量,但苦恼于没有简易的字符串处理函数的郁闷 char *itoa_private(int val, char *buf, unsigned radix);//整数转字符串 int my_isdigit(int ch);//判断字符是否为数字 long long ...
Hex 函数 返回表示数的十六进制值的字符串。 HelpContext 属性 设置或返回帮助文件中某主题的上下文 ID。 HelpFile 属性 设置或返回帮助文件的全路径。 Hour 函数 返回小时数,取值范围为 0 至 23。 If...Then.....
请参照String类,对一个封装了字节数组的类提供如下函数 public class MyByteArray { private byte[] data; public MyByteArray(byte[] data) { this.data = data; } //返回字节数据b在字节数组中的位置 ...
1.字符串转二进制数组 string content=”这是做个测试!”; System.Text.UnicodeEncoding converter = new System.Text.UnicodeEncoding(); byte[] byteArr = converter.GetBytes(content); 2.二进制数组转为字符串 ...
Python 3最重要的新特性大概要算是对文本和二进制数据作了更为清晰的区分。文本总是Unicode,由str类型表示,二进制数据则由bytes类型表示。Python 3不会以任意隐式的方式混用str和bytes,正是这使得两者的区分特别...
Hex 函数 返回表示数的十六进制值的字符串。 HelpContext 属性 设置或返回帮助文件中某主题的上下文 ID。 HelpFile 属性 设置或返回帮助文件的全路径。 Hour 函数 返回小时数,取值范围为 0 至 23。 If...Then.....
Hex 函数 返回表示数的十六进制值的字符串。 HelpContext 属性 设置或返回帮助文件中某主题的上下文 ID。 HelpFile 属性 设置或返回帮助文件的全路径。 Hour 函数 返回小时数,取值范围为 0 至 23。 If...Then.....