`
anonymousstone
  • 浏览: 12737 次
  • 性别: Icon_minigender_1
  • 来自: 南阳
最近访客 更多访客>>
社区版块
存档分类
最新评论

ConverterBetweenHexStringAndByteArray

阅读更多
package pte.stone;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;

/**
 * Converter between byte[] to hexadecimal String.
 * 
 * @author stone
 * 
 */

public class ConverterBetweenHexStringAndByteArray {
	/**
	 * Convert byte[] to hexadecimal String.
	 * 
	 * @param bytea
	 * @return
	 * @throws Exception
	 */
	public static String byteArr2HexStr1(byte[] bytea) throws Exception {
		String sHex = "";
		DataInputStream dis = new DataInputStream(new ByteArrayInputStream(
				bytea));
		int iUnsigned = 0;
		StringBuffer sbHex = new StringBuffer();
		while (true) {
			iUnsigned = dis.read();
			if (iUnsigned == -1) {
				break;
			}
			if (iUnsigned < 16) {
				sbHex.append("0");
			}
			sbHex.append(Integer.toString(iUnsigned, 16));
		}
		sHex = sbHex.toString();
		return sHex;
	}

	/**
	 * Convert byte[] to hexadecimal String.
	 * 
	 * @param bytea
	 * @return
	 * @throws Exception
	 */
	public static String byteArr2HexStr2(byte[] bytea) throws Exception {
		String sHex = "";
		int iUnsigned = 0;
		StringBuffer sbHex = new StringBuffer();
		for (int i = 0; i < bytea.length; i++) {
			iUnsigned = bytea[i];
			if (iUnsigned < 0) {
				iUnsigned += 256;
			}
			if (iUnsigned < 16) {
				sbHex.append("0");
			}
			sbHex.append(Integer.toString(iUnsigned, 16));
		}
		sHex = sbHex.toString();
		return sHex;
	}

	/**
	 * Convert hexadecimal String to byte[].
	 * 
	 * @param sHex
	 * @return
	 * @throws Exception
	 */
	public static byte[] hexStr2ByteArr(String sHex) throws Exception {
		byte[] bytea = new byte[sHex.length() / 2];
		String sHexSingle = "";
		for (int i = 0; i < bytea.length; i++) {
			sHexSingle = sHex.substring(i * 2, i * 2 + 2);
			bytea[i] = (byte) Integer.parseInt(sHexSingle, 16);
		}
		return bytea;
	}
}



test case
package pte.stone.test;

import org.junit.Assert;
import org.junit.Test;
import pte.stone.ConverterBetweenHexStringAndByteArray;

public class ConverterBetweenHexStringAndByteArrayTest {
	@Test
	public void testByteArr2HexStr1() throws Exception{
		byte[] bytea1={0,1,126,127};
		byte[] bytea2={-128,-127,-2,-1};
		String sHex1=ConverterBetweenHexStringAndByteArray.byteArr2HexStr1(bytea1);
		String sHex2=ConverterBetweenHexStringAndByteArray.byteArr2HexStr1(bytea2);
		String sHex1Exp="00017e7f";
		String sHex2Exp="8081feff";
		Assert.assertEquals(sHex1Exp, sHex1);
		Assert.assertEquals(sHex2Exp, sHex2);
	}
	
	@Test
	public void testByteArr2HexStr2() throws Exception{
		byte[] bytea1={0,1,126,127};
		byte[] bytea2={-128,-127,-2,-1};
		String sHex1=ConverterBetweenHexStringAndByteArray.byteArr2HexStr2(bytea1);
		String sHex2=ConverterBetweenHexStringAndByteArray.byteArr2HexStr2(bytea2);
		String sHex1Exp="00017e7f";
		String sHex2Exp="8081feff";
		Assert.assertEquals(sHex1Exp, sHex1);
		Assert.assertEquals(sHex2Exp, sHex2);
	}
	
	@Test
	public void testHexStr2ByteArr()throws Exception{	
		String sHex1="00017e7f";
		String sHex2="8081feff";
		byte[] bytea1=ConverterBetweenHexStringAndByteArray.hexStr2ByteArr(sHex1);
		byte[] bytea2=ConverterBetweenHexStringAndByteArray.hexStr2ByteArr(sHex2);
		byte[] bytea1Exp={0,1,126,127};
		byte[] bytea2Exp={-128,-127,-2,-1};
		Assert.assertArrayEquals(bytea1Exp, bytea1);
	}
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics