ByteArray에서 비트를 조작하는 방법을 알고 싶습니다. 내가 필요한 것은 '테이블'에 따라 비트를 이동하는 것입니다.ByteArray에서 비트 조작
표 :
Bit 0 -> Bit 26 Bit 1 -> Bit 31 Bit 2 -> Bit 17 ... Bit 31 -> Bit 5
나는이 방법
public static BitArray ByteArraytoBitArray(byte[] bytes)
{
BitArray bits = new BitArray(bytes);
return bits;
}
를 사용 BitArray하기 위해이 ByteArray로 변환하지만 붙어 있 느니라, 내가 따라 비트를 이동하는 방법을 모른다 테이블로 이동 한 다음 ByteArray로 돌아갑니다.
는편집 :
코드 Snipet : 내 입력 바이트 배열은 "00804804A02A1CA20100000000000000D2F8B6FABBB700000000000000000000가"와 zimdanen의 코드 내 출력은 "5012000000000000000000000000000000000000000000000000000000000000"입니다
public static byte[] StringToByteArray(String hex)
{
int NumberChars = hex.Length;
byte[] bytes = new byte[NumberChars/2];
for (int i = 0; i < NumberChars; i += 2)
bytes[i/2] = Convert.ToByte(hex.Substring(i, 2), 16);
return bytes;
}
private void button3_Click(object sender, EventArgs e)
{
string featherearring = "00804804A02A1CA20100000000000000D2F8B6FABBB700000000000000000000";
var strarray = StringToByteArray(featherearring);
byte[] strarray_comp = Enc.Encrypt(strarray);
string conv = BitConverter.ToString(strarray_comp);
MessageBox.Show(conv.Replace("-", ""));
}
public static byte[] BitArrayToByteArray(BitArray bits)
{
byte[] bytes = new byte[bits.Length/8];
bits.CopyTo(bytes, 0);
return bytes;
}
public static byte[] Encrypt(byte[] input)
{
BitArray source = new BitArray(input);
BitArray target = new BitArray(source.Length);
target[26] = source[0];
target[31] = source[1];
target[17] = source[2];
target[10] = source[3];
target[30] = source[4];
target[16] = source[5];
target[24] = source[6];
target[2] = source[7];
target[29] = source[8];
target[8] = source[9];
target[20] = source[10];
target[15] = source[11];
target[28] = source[12];
target[11] = source[13];
target[13] = source[14];
target[4] = source[15];
target[19] = source[16];
target[23] = source[17];
target[0] = source[18];
target[12] = source[19];
target[14] = source[20];
target[27] = source[21];
target[6] = source[22];
target[18] = source[23];
target[21] = source[24];
target[3] = source[25];
target[9] = source[26];
target[7] = source[27];
target[22] = source[28];
target[1] = source[29];
target[25] = source[30];
target[5] = source[31];
return BitArrayToByteArray(target);
}
입니다
그리고 당신으로 "501200002FD901000000000400000000BFE8C4DB140D11F40000000000000000" 해야한다 알 수 있듯이, 첫 번째 2 바이트를 가져 오지만 나머지는 모두 null입니다.
빠르거나 정확해야합니까? – harold
[C# - 전체 바이트 배열을 왼쪽으로 이동] [1]에 대한 내 대답을보십시오. [1] :이 방법은 '년후 경우 http://stackoverflow.com/questions/8440938/c-sharp-left-shift-an-entire-byte-array/8450271#8450271 – JamieSee
I는 wouldnt 마음 가장 빠르며, 조작법을 올바르게 처리하는 데 정말로 신경을 쓴다. –