package com.silencefun.comtest;

import java.io.UnsupportedEncodingException;

/**
 * @author benjaminwan
 *???????????
 */
public class MyFunc {
	//-------------------------------------------------------
	// ?ж????????????λ????????λ??1??????????0?????
    static public int isOdd(int num)
	{
		return num & 0x1;
	}
    //-------------------------------------------------------
    static public int HexToInt(String inHex)//Hex??????int
    {
    	return Integer.parseInt(inHex, 16);
    }
    //-------------------------------------------------------
    static public byte HexToByte(String inHex)//Hex??????byte
    {
    	return (byte)Integer.parseInt(inHex,16);
    }
    //-------------------------------------------------------
    static public String Byte2Hex(Byte inByte)//1????2??Hex???
    {
    	return String.format("%02x", inByte).toUpperCase();
    }
    //-------------------------------------------------------
	static public String ByteArrToHex(byte[] inBytArr)//?????????hex?????
	{
		StringBuilder strBuilder=new StringBuilder();
		int j=inBytArr.length;
		for (int i = 0; i < j; i++)
		{
			strBuilder.append(Byte2Hex(inBytArr[i]));
			strBuilder.append(" ");
		}
		return strBuilder.toString(); 
	}
  //-------------------------------------------------------
    static public String ByteArrToHex(byte[] inBytArr,int offset,int byteCount)//?????????hex??????????????
	{
    	StringBuilder strBuilder=new StringBuilder();
		int j=byteCount;
		for (int i = offset; i < j; i++)
		{
			strBuilder.append(Byte2Hex(inBytArr[i]));
		}
		return strBuilder.toString();
	}
	//-------------------------------------------------------
	//?hex?????????????
    static public byte[] HexToByteArr(String inHex)//hex?????????????
	{
		int hexlen = inHex.length();
		byte[] result;
		if (isOdd(hexlen)==1)
		{//????
			hexlen++;
			result = new byte[(hexlen/2)];
			inHex="0"+inHex;
		}else {//???
			result = new byte[(hexlen/2)];
		}
	    int j=0;
		for (int i = 0; i < hexlen; i+=2)
		{
			result[j]=HexToByte(inHex.substring(i,i+2));
			j++;
		}
	    return result; 
	}



	public static byte[] stringToHexByteArray(String s) {
		String cleanString = s.replace(" ", ""); // 去除字符串中的空格
		byte[] byteArray = new byte[cleanString.length() / 2]; // 每两个字符代表一个字节
		for (int i = 0; i < cleanString.length(); i += 2) {
			byteArray[i / 2] = (byte) ((Character.digit(cleanString.charAt(i), 16) << 4)
					+ Character.digit(cleanString.charAt(i+1), 16));
		}
		return byteArray;
	}



	public static String strToHex(String str) {
		String hexString = "0123456789ABCDEF";
		byte[] bytes = str.getBytes();
		StringBuilder hex = new StringBuilder(bytes.length * 2);
		for (int i = 0; i < bytes.length; i++) {
			hex.append(hexString.charAt((bytes[i] & 0xf0) >> 4));  // 作用同 n / 16
			hex.append(hexString.charAt((bytes[i] & 0x0f) >> 0));  // 作用同 n
			hex.append(' ');  //中间用空格隔开
		}
		return hex.toString();
	}


	public static String ByteArrToStr(byte[] bytes) {
		String str = "";
		try {
			str = new String(bytes, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return str.toString();
	}





}