欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

二进制转16进制字符串和16进制字符串转二进制的C和JAVA实现

程序员文章站 2024-03-18 14:32:28
...

二进制转16进制字符串和16进制字符串转二进制的C语言实现

//二进制转16进制字符串,长度会翻倍
void ByteToHexStr(const unsigned char* source, char* dest, int sourceLen);

//16进制字符串转二进制,长度减半
void HexStrToByte(const char* source, unsigned char* dest, int sourceLen);







//---------------------------------------
//字节流转换为十六进制字符串
void ByteToHexStr(const unsigned char* source, char* dest, int sourceLen)
{
	short i;
	unsigned char highByte, lowByte;

	for (i = 0; i < sourceLen; i++)
	{
		highByte = source[i] >> 4;
		lowByte = source[i] & 0x0f;

		highByte += 0x30;

		if (highByte > 0x39)
			dest[i * 2] = highByte + 0x07;
		else
			dest[i * 2] = highByte;

		lowByte += 0x30;
		if (lowByte > 0x39)
			dest[i * 2 + 1] = lowByte + 0x07;
		else
			dest[i * 2 + 1] = lowByte;
	}
	return;
}

//十六进制字符串转换为字节流
void HexStrToByte(const char* source, unsigned char* dest, int sourceLen)
{
	short i;
	unsigned char highByte, lowByte;

	for (i = 0; i < sourceLen; i += 2)
	{
		highByte = toupper(source[i]);
		lowByte = toupper(source[i + 1]);

		if (highByte > 0x39)
			highByte -= 0x37;
		else
			highByte -= 0x30;

		if (lowByte > 0x39)
			lowByte -= 0x37;
		else
			lowByte -= 0x30;

		dest[i / 2] = (highByte << 4) | lowByte;
	}
	return;
}

下面是JAVA语言的实现

    /**
     * 二进制字节码转换为16进制字符串
     *
     * @param pSrc   字节码输入参数
     * @param nSrcLen 转换的长度,byte数组的长度
     * @return  16进制字符串
     */

    public static String Bytes2String(byte[] pSrc, int nSrcLen)
    {
        String[] szTable= {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};

        String  str = new String();

        for (int i = 0; i < nSrcLen; i++)
        {

            int idx = (pSrc[i] >> 4) & 0x0f;
            str += szTable[idx];

            idx = pSrc[i] & 0x0f;
            str += szTable[idx];
        }

        return  str;
    }


    private static byte toB (byte X) {
        byte BB = 0;
        switch(X) {
            case '0':BB = 0x0;break;
            case '1':BB = 0x1;break;
            case '2':BB = 0x2;break;
            case '3':BB = 0x3;break;
            case '4':BB = 0x4;break;
            case '5':BB = 0x5;break;
            case '6':BB = 0x6;break;
            case '7':BB = 0x7;break;
            case '8':BB = 0x8;break;
            case '9':BB = 0x9;break;
            case 'A':BB = 0xA;break;
            case 'B':BB = 0xB;break;
            case 'C':BB = 0xC;break;
            case 'D':BB = 0xD;break;
            case 'E':BB = 0xE;break;
            case 'F':BB = 0xF;break;
            case 'a':BB = 0xA;break;
            case 'b':BB = 0xB;break;
            case 'c':BB = 0xC;break;
            case 'd':BB = 0xD;break;
            case 'e':BB = 0xE;break;
            case 'f':BB = 0xF;break;
            default:break;

        }
        return BB;
    }

    /**
     * 16进制字符串转换为二进制字节码
     *
     * @param szSrc  16进制字符串输入参数
     * @return  字节码
     */
    public static byte[] String2Bytes(String szSrc)
    {
        int iLen = szSrc.length();
        if (iLen <= 0)
        {
            return null;
        }

        iLen /= 2;
        szSrc = szSrc.toUpperCase();

        //byte[] szTable = {0,1,2,3,4,5,6,7,8,9,0xA,0xB,0xC,0xD,0xE,0xF,}
        byte[] arrays = szSrc.getBytes();
        byte[] bins = new byte[iLen];

        for (int i = 0; i < iLen; i++)
        {
            byte HB = arrays[i*2];
            byte LB = arrays[i*2+1];

            HB = toB(HB);
            LB = toB(LB);

            bins[i] = (byte)((((HB<<4)&0xF0) | (LB&0x0F))&0x0FF);

        }

        return bins;
    }