C# 将一维数组拼接成按7bit为单元,高位填0的数组

从二维数组中取一行数据

public byte[] GetLineByte(byte[,] bbArray, int iLine)
{
	int iCols = bbArray.GetLength(0);  //列
	byte[] bLineData = new byte[iCols];

	for (int i = 0; i < iCols; i++)
	{
		bLineData[i] = bbArray[i, iLine];
	}

	return bLineData;
}

将一维数组拼接成按7bit为单元,高位填0的数组

public byte[] ConvertTo7bitByteArray(byte[] bArray)
{
	int iArrayLen = bArray.Length;  //数组长度

	int bit7ColsLen = bArray.Length / 7;
	if (bArray.Length % 7 != 0)
	{
		bit7ColsLen = bit7ColsLen + 1;
	}

	byte[] bit7Array = new byte[bit7ColsLen];

	int j = 0;
	for (int i = 0; i < iArrayLen; i += 7)
	{
		byte b1, b2, b3, b4, b5, b6, b7, b8;

		b1 = 0;   //高位置0

		b2 = (byte)(bArray[i + 0] << 6);
		if (i + 1 < iArrayLen)
		{
			b3 = (byte)(bArray[i + 1] << 5);
		}
		else
		{
			b3 = 0;
		}
		if (i + 2 < iArrayLen)
		{
			b4 = (byte)(bArray[i + 2] << 4);
		}
		else
		{
			b4 = 0;
		}
		if (i + 3 < iArrayLen)
		{
			b5 = (byte)(bArray[i + 3] << 3);
		}
		else
		{
			b5 = 0;
		}
		if (i + 4 < iArrayLen)
		{
			b6 = (byte)(bArray[i + 4] << 2);
		}
		else
		{
			b6 = 0;
		}
		if (i + 5 < iArrayLen)
		{
			b7 = (byte)(bArray[i + 5] << 1);
		}
		else
		{
			b7 = 0;
		}
		if (i + 6 < iArrayLen)
		{
			b8 = (byte)(bArray[i + 6] << 0);
		}
		else
		{
			b8 = 0;
		}

		if (b1 != 0 || b2 != 0 || b3 != 0 || b4 != 0 || b5 != 0 || b6 != 0 || b7 != 0 || b8 != 0)
		{

		}

		byte bMyData = (byte)(b1 | b2 | b3 | b4 | b5 | b6 | b7 | b8);
		bit7Array[j] = bMyData;  //产生一个字节
		j++;
	}

	string strHexString = byteToHexStr(bit7Array);
	return bit7Array;
}

字节数组转16进制字符串 

public string byteToHexStr(byte[] bytes)
{
	string returnStr = "";
	if (bytes != null)
	{
		for (int i = 0; i < bytes.Length; i++)
		{
			returnStr += bytes[i].ToString("X2");
		}
	}
	return returnStr;
}

你可能感兴趣的:(C#,c#,算法)