Java常用转换封装类

1、其他类型转换为字节数组:ConverterBytes.java

package zte.rfid.hup.base.converter;

import java.util.Calendar;

import zte.rfid.hup.base.HUPRRDLog;

public final class ConverterBytes
{
	private ConverterBytes()
	{
	}
	
	/**
	 * 字节数组内容比较
	 * 
	 * @param abyeSrc
	 * @param intSrcOffset
	 * @param abyeDst
	 * @param intDstOffset
	 * @param intLength
	 * @throws HUPRRD9800Exception
	 */
	public static boolean equal(byte[] abyeSrc, int intSrcOffset,
			byte[] abyeDst, int intDstOffset, int intLength) throws Exception
	{
		if (intLength <= 0)
		{
			Exception ex = new Exception("intLength <= 0");
			HUPRRDLog.d(ex);
			throw ex;
		}
		
		if (abyeSrc == null)
		{
			Exception ex = new Exception("abyeSrc == null");
			HUPRRDLog.d(ex);
			throw ex;
		}
		
		if (intSrcOffset < 0 || intSrcOffset + intLength > abyeSrc.length)
		{
			Exception ex = new Exception(
					"intSrcOffset < 0 || intSrcOffset + intLength > abyeSrc.length");
			HUPRRDLog.d(ex);
			throw ex;
		}
		
		if (abyeDst == null)
		{
			Exception ex = new Exception("abyeDst == null");
			HUPRRDLog.d(ex);
			throw ex;
		}
		
		if (intDstOffset < 0 || intDstOffset + intLength > abyeDst.length)
		{
			Exception ex = new Exception(
					"intDstOffset < 0 || intDstOffset + intLength > abyeDst.length");
			HUPRRDLog.d(ex);
			throw ex;
		}
		
		for (int index = 0; index < intLength; index++)
		{
			if (abyeSrc[intSrcOffset + index] != abyeDst[intDstOffset + index])
			{
				return false;
			}
		}
		return true;
	}
	
	/**
	 * 将LONG转换为字节数组
	 * 
	 * @param lngValue
	 *            需要转换的值
	 * @param intLength
	 *            需要转换的字节数目,可以控制返回字节数组大小
	 * @return
	 * @throws HUPRRD9800Exception
	 */
	public static byte[] valueOf(long lngValue, int intLength) throws Exception
	{
		if (intLength <= 0)
		{
			Exception ex = new Exception("intLength <= 0");
			HUPRRDLog.d(ex);
			throw ex;
		}
		
		try
		{
			byte[] abye = new byte[intLength];
			for (int index = 0; index < intLength; index++)
			{
				abye[index] = (byte) ((lngValue >> ((intLength - index - 1) * 8)) & 0x00000000000000FF);
			}
			return abye;
		}
		catch (Exception ex)
		{
			HUPRRDLog.d(ex);
			throw new Exception(ex.getMessage());
		}
	}
	
	/**
	 * 将GB2312编码的字符串转换成对应字节
	 * 
	 * @param strSrc
	 * @return
	 * @throws HUPRRD9800Exception
	 */
	public static byte[] valueOfGB2312(String strSrc) throws Exception
	{
		if (strSrc == null)
		{
			Exception ex = new Exception("strSrc == null");
			HUPRRDLog.d(ex);
			throw ex;
		}
		
		try
		{
			return strSrc.getBytes("GB2312");
		}
		catch (Exception ex)
		{
			HUPRRDLog.d(ex);
			throw new Exception(ex.getMessage());
		}
	}
	
	/**
	 * 将16进制字符串转换为对应字节数组
	 * 
	 * @param strSrc
	 * @return
	 * @throws HUPRRD9800Exception
	 */
	public static byte[] valueOfHex(String strSrc) throws Exception
	{
		if (strSrc == null || strSrc.length() % 2 != 0)
		{
			Exception ex = new Exception(
					"strSrc == null || strSrc.length() % 2 != 0");
			HUPRRDLog.d(ex);
			throw ex;
		}
		
		try
		{
			int intLength = strSrc.length() / 2;
			byte[] abye = new byte[intLength];
			
			for (int index = 0; index < intLength; index++)
			{
				abye[index] = (byte) Integer.parseInt(
						strSrc.substring(2 * index, 2 * index + 2), 16);
			}
			
			return abye;
		}
		catch (Exception ex)
		{
			HUPRRDLog.d(ex);
			throw new Exception(ex.getMessage());
		}
	}
	
	/**
	 * 将日历转换成对应字节数据
	 * 
	 * @param calendar
	 * @return
	 * @throws HUPRRD9800Exception
	 */
	public static byte[] valueOfHex(Calendar calendar) throws Exception
	{
		if (calendar == null)
		{
			Exception ex = new Exception("calendar == null");
			HUPRRDLog.d(ex);
			throw ex;
		}
		
		try
		{
			String strDateString = ConverterString
					.valueOf("yyyyMMdd", calendar);
			return ConverterBytes.valueOfHex(strDateString);
		}
		catch (Exception ex)
		{
			HUPRRDLog.d(ex);
			throw new Exception(ex.getMessage());
		}
	}
	
	public static byte[] valueOfTimeStamp(Calendar calendar) throws Exception
	{
		if (calendar == null)
		{
			Exception ex = new Exception("calendar == null");
			HUPRRDLog.d(ex);
			throw ex;
		}
		
		try
		{
			byte[] abyeTimeStamp = new byte[8];
			
			int year = Calendar.getInstance().get(Calendar.YEAR);
			abyeTimeStamp[0] = (byte) (year % 256);
			abyeTimeStamp[1] = (byte) (year / 256);
			abyeTimeStamp[2] = (byte) (Calendar.getInstance().get(
					Calendar.MONTH) + 1);
			abyeTimeStamp[3] = (byte) Calendar.getInstance().get(
					Calendar.DAY_OF_MONTH);
			abyeTimeStamp[4] = (byte) (Calendar.getInstance().get(
					Calendar.DAY_OF_WEEK) - 1);
			abyeTimeStamp[5] = (byte) Calendar.getInstance().get(
					Calendar.HOUR_OF_DAY);
			abyeTimeStamp[6] = (byte) Calendar.getInstance().get(
					Calendar.MINUTE);
			abyeTimeStamp[7] = (byte) Calendar.getInstance().get(
					Calendar.SECOND);
			
			return abyeTimeStamp;
		}
		catch (Exception ex)
		{
			HUPRRDLog.d(ex);
			throw new Exception(ex.getMessage());
		}
	}
}

2、其他类型转换为日历:ConverterCalendar.java

public final class ConverterCalendar
{
	private ConverterCalendar()
	{
	}
	
	/**
	 * 获取1970-01-01到当前日期的秒数
	 * 
	 * @return
	 */
	public static int getSeconds()
	{
		long lngMSDiff = Calendar.getInstance().getTime().getTime()
				+ Calendar.getInstance().getTimeZone().getRawOffset() / 1000;
		
		if (lngMSDiff <= 0)
		{
			return 0;
		}
		return (int) (lngMSDiff / 1000);
	}
	
	public static Calendar addYears(Calendar calendar, int intYears)
			throws Exception
	{
		if (calendar == null)
		{
			Exception ex = new Exception("calendar == null");
			HUPRRDLog.d(ex);
			throw ex;
		}
		
		try
		{
			calendar.add(Calendar.YEAR, intYears);
			return calendar;
		}
		catch (Exception ex)
		{
			HUPRRDLog.d(ex);
			throw new Exception(ex.getMessage());
		}
	}
	
	/**
	 * 将指定字符串格式的日期转换为日历
	 * 
	 * @param strDateFormat
	 *            指定格式字符串,如yyyyMMdd
	 * @param strDate
	 *            指定字符串日期,如20120101
	 * @return
	 * @throws HUPRRD9800Exception
	 */
	public static Calendar valueOf(String strDateFormat, String strDate)
			throws Exception
	{
		if (strDateFormat == null || strDate == null)
		{
			Exception ex = new Exception(
					"strDateFormat == null || strDate == null");
			HUPRRDLog.d(ex);
			throw ex;
		}
		
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(strDateFormat);
		try
		{
			Date date = simpleDateFormat.parse(strDate);
			
			String strConvertDate = simpleDateFormat.format(date);
			if (!strDate.equalsIgnoreCase(strConvertDate))
			{
				throw new Exception("");
			}
			
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			return calendar;
		}
		catch (Exception ex)
		{
			HUPRRDLog.d(ex);
			throw new Exception(ex.getMessage());
		}
	}
	
	/**
	 * 将字节数组转换成日历,如:0x20,0x12,0x01,0x01-->2012年1月1日
	 * 
	 * @param abyeSrc
	 * @param intOffset
	 * @return
	 * @throws HUPRRD9800Exception
	 */
	public static Calendar valueOf(byte[] abyeSrc, int intOffset)
			throws Exception
	{
		if (abyeSrc == null)
		{
			Exception ex = new Exception("abyeSrc == null");
			HUPRRDLog.d(ex);
			throw ex;
		}
		
		if (intOffset < 0 || intOffset + abyeSrc.length / 2 > abyeSrc.length)
		{
			Exception ex = new Exception(
					"intOffset < 0 || intOffset + abyeSrc.length() / 2 > abyeSrc.length");
			HUPRRDLog.d(ex);
			throw ex;
		}
		
		Calendar calendar = Calendar.getInstance();
		try
		{
			calendar.setLenient(false);
			calendar.set(
					ConverterInteger.valueOfDecimal(abyeSrc, intOffset, 2),
					ConverterInteger.valueOfDecimal(abyeSrc, intOffset + 2, 1) - 1,
					ConverterInteger.valueOfDecimal(abyeSrc, intOffset + 3, 1));
			calendar.get(Calendar.YEAR);
			return calendar;
		}
		catch (Exception ex)
		{
			HUPRRDLog.d(ex);
			throw new Exception(ex.getMessage());
		}
	}
	
}

3、其他类型转换为整型:ConverterInteger.java

public final class ConverterInteger
{
	private ConverterInteger()
	{
	}
	
	/**
	 * 将字节数组的指定字节数转换成对应数字
	 * 
	 * @param abyeHexSrc
	 * @param intOffset
	 * @param intLength
	 * @return
	 * @throws HUPRRD9800Exception
	 */
	public static int valueOfHex(byte[] abyeHexSrc, int intOffset, int intLength)
			throws Exception
	{
		if (abyeHexSrc == null)
		{
			Exception ex = new Exception("abyeHexSrc == null");
			HUPRRDLog.d(ex);
			throw ex;
		}
		
		if (intOffset < 0 || intOffset >= abyeHexSrc.length)
		{
			Exception ex = new Exception(
					"intOffset < 0 || intOffset >= abyeHexSrc.length");
			HUPRRDLog.d(ex);
			throw ex;
		}
		
		if (intLength < 0 || intOffset + intLength > abyeHexSrc.length)
		{
			Exception ex = new Exception(
					"intLength < 0 || intOffset + intLength > abyeHexSrc.length");
			HUPRRDLog.d(ex);
			throw ex;
		}
		
		try
		{
			int lngTmpValue = 0;
			for (int index = 0; index < intLength; index++)
			{
				int lngData = abyeHexSrc[intOffset + index] & 0x000000FF;
				lngTmpValue += lngData << ((intLength - index - 1) * 8);
			}
			return lngTmpValue;
		}
		catch (Exception ex)
		{
			HUPRRDLog.d(ex);
			throw new Exception(ex.getMessage());
		}
	}
	
	/**
	 * 将10进制数组转换成整型。如:0x12,0x34-->1234
	 * 
	 * @param abyeDecimalSrc
	 * @param intOffset
	 * @param intLength
	 * @return
	 * @throws HUPRRD9800Exception
	 */
	public static int valueOfDecimal(byte[] abyeDecimalSrc, int intOffset,
			int intLength) throws Exception
	{
		if (abyeDecimalSrc == null)
		{
			Exception ex = new Exception("abyeDecimalSrc == null");
			HUPRRDLog.d(ex);
			throw ex;
		}
		
		if (intOffset < 0 || intOffset >= abyeDecimalSrc.length)
		{
			Exception ex = new Exception(
					"intOffset < 0 || intOffset >= abyeDecimalSrc.length");
			HUPRRDLog.d(ex);
			throw ex;
		}
		
		if (intLength < 0 || intOffset + intLength > abyeDecimalSrc.length)
		{
			Exception ex = new Exception(
					"intLength < 0 || intOffset + intLength > abyeDecimalSrc.length");
			HUPRRDLog.d(ex);
			throw ex;
		}
		
		try
		{
			String str = ConverterString.valueOfHex(abyeDecimalSrc, intOffset,
					intLength);
			return Integer.parseInt(str);
		}
		catch (Exception ex)
		{
			HUPRRDLog.d(ex);
			throw new Exception(ex.getMessage());
		}
	}
}

4、其他类型转换为长整型:ConverterLong.java

public class ConverterLong
{
	public static long valueOfHex(byte[] abyeHexSrc, int intOffset,
			int intLength) throws Exception
	{
		if (abyeHexSrc == null)
		{
			Exception ex = new Exception("abyeHexSrc == null");
			HUPRRDLog.d(ex);
			throw ex;
		}
		
		if (intOffset < 0 || intOffset >= abyeHexSrc.length)
		{
			Exception ex = new Exception(
					"intOffset < 0 || intOffset >= abyeHexSrc.length");
			HUPRRDLog.d(ex);
			throw ex;
		}
		
		if (intLength < 0 || intOffset + intLength > abyeHexSrc.length)
		{
			Exception ex = new Exception(
					"intLength < 0 || intOffset + intLength > abyeHexSrc.length");
			HUPRRDLog.d(ex);
			throw ex;
		}
		
		try
		{
			long lngTmpValue = 0;
			for (int index = 0; index < intLength; index++)
			{
				long lngData = abyeHexSrc[intOffset + index] & 0x000000FF;
				lngTmpValue += lngData << ((intLength - index - 1) * 8);
			}
			return lngTmpValue;
		}
		catch (Exception ex)
		{
			HUPRRDLog.d(ex);
			throw new Exception(ex.getMessage());
		}
	}
}

5、其他类型转换为String:ConverterString.java

public final class ConverterString
{
	private ConverterString()
	{
	}
	
	/**
	 * 将字节数组转换成GB2312编码格式字符串
	 * 
	 * @param abyeSrc
	 * @param intOffset
	 * @param intLength
	 * @return
	 * @throws HUPRRD9800Exception
	 */
	public static String valueOfGB2312(byte[] abyeSrc, int intOffset,
			int intLength) throws Exception
	{
		if (abyeSrc == null)
		{
			Exception ex = new Exception("abyeSrc == null");
			HUPRRDLog.d(ex);
			throw ex;
		}
		
		if (intOffset < 0 || intOffset >= abyeSrc.length)
		{
			Exception ex = new Exception(
					"intOffset < 0 || intOffset >= abyeSrc.length");
			HUPRRDLog.d(ex);
			throw ex;
		}
		
		if (intLength < 0 || intOffset + intLength > abyeSrc.length)
		{
			Exception ex = new Exception(
					"intLength < 0 || intOffset + intLength > abyeSrc.length");
			HUPRRDLog.d(ex);
			throw ex;
		}
		
		try
		{
			return new String(abyeSrc, intOffset, intLength, "GB2312");
		}
		catch (Exception ex1)
		{
			HUPRRDLog.d(ex1);
			try
			{
				return valueOfHex(abyeSrc, intOffset, intLength);
			}
			catch (Exception ex)
			{
				HUPRRDLog.d(ex);
				throw new Exception(ex.getMessage());
			}
		}
	}
	
	public static String valueOfGB2312Chinese(byte[] abyeSrc, int intOffset,
			int intLength) throws Exception
	{
		if (abyeSrc == null)
		{
			Exception ex = new Exception("abyeSrc == null");
			HUPRRDLog.d(ex);
			throw ex;
		}
		
		if (intOffset < 0 || intOffset >= abyeSrc.length)
		{
			Exception ex = new Exception(
					"intOffset < 0 || intOffset >= abyeSrc.length");
			HUPRRDLog.d(ex);
			throw ex;
		}
		
		if (intLength < 0 || intOffset + intLength > abyeSrc.length)
		{
			Exception ex = new Exception(
					"intLength < 0 || intOffset + intLength > abyeSrc.length");
			HUPRRDLog.d(ex);
			throw ex;
		}
		
		try
		{
			int length = 0;
			for (int index = intOffset; index < intOffset + intLength - 1; index += 2)
			{
				int bye1 = abyeSrc[index] & 0xff;
				int bye2 = abyeSrc[index + 1] & 0xff;
				if (bye1 >= 0x81 && bye1 <= 0xFE && bye2 >= 0x40
						&& bye2 <= 0xFE)
				{
					length += 2;
					continue;
				}
				break;
			}
			return new String(abyeSrc, intOffset, length, "GB2312");
		}
		catch (Exception ex1)
		{
			HUPRRDLog.d(ex1);
			try
			{
				return valueOfHex(abyeSrc, intOffset, intLength);
			}
			catch (Exception ex)
			{
				HUPRRDLog.d(ex);
				throw new Exception(ex.getMessage());
			}
		}
	}
	
	private static final char[]	bcdLookup	= { '0', '1', '2', '3', '4', '5',
			'6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
	
	/**
	 * 将字节数字转换成16进制编码字符串
	 * 
	 * @param abyeSrc
	 * @param intOffset
	 * @param intLength
	 * @return
	 * @throws HUPRRD9800Exception
	 */
	public static String valueOfHex(byte[] abyeSrc, int intOffset, int intLength)
			throws Exception
	{
		if (abyeSrc == null)
		{
			Exception ex = new Exception("abyeSrc == null");
			HUPRRDLog.d(ex);
			throw ex;
		}
		
		if (intOffset < 0 || intOffset >= abyeSrc.length)
		{
			Exception ex = new Exception(
					"intOffset < 0 || intOffset >= abyeSrc.length");
			HUPRRDLog.d(ex);
			throw ex;
		}
		
		if (intLength < 0 || intOffset + intLength > abyeSrc.length)
		{
			Exception ex = new Exception(
					"intLength < 0 || intOffset + intLength > abyeSrc.length");
			HUPRRDLog.d(ex);
			throw ex;
		}
		try
		{
			StringBuffer stringBuilder = new StringBuffer(intLength * 2);
			for (int index = 0; index < intLength; index++)
			{
				stringBuilder
						.append(bcdLookup[(abyeSrc[intOffset + index] >>> 4) & 0x0f]);
				stringBuilder
						.append(bcdLookup[abyeSrc[intOffset + index] & 0x0f]);
			}
			return stringBuilder.toString();
		}
		catch (Exception ex)
		{
			HUPRRDLog.d(ex);
			throw new Exception(ex.getMessage());
		}
	}
	
	/**
	 * 将日历按指定格式返回字符串
	 * 
	 * @param strDateFormat
	 * @param calendar
	 * @return
	 * @throws HUPRRD9800Exception
	 */
	public static String valueOf(String strDateFormat, Calendar calendar)
			throws Exception
	{
		if (strDateFormat == null)
		{
			Exception ex = new Exception("strDateFormat == null");
			HUPRRDLog.d(ex);
			throw ex;
		}
		if (calendar == null)
		{
			Exception ex = new Exception("calendar == null");
			HUPRRDLog.d(ex);
			throw ex;
		}
		
		try
		{
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
					strDateFormat);
			return simpleDateFormat.format(calendar.getTime());
		}
		catch (Exception ex)
		{
			HUPRRDLog.d(ex);
			throw new Exception(ex.getMessage());
		}
	}
}

发表回复