私はunsigned long longのバイナリ形式を取得し、その各ビットを配列に格納しようとしています。符号なしlong longのバイナリ表現
0000000000000000 0000000000000000
FFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFF
3000000000000000 1000000000000001
各エントリはヘクスで表される64ビットの整数である:
私はこのような入力ファイルを有しています。私はunsigned long longを使用してこの値を保持し、ビットを反復して配列に格納しようとしていますが、配列のいくつかは間違った位置にビットがあります。ここで
は私が持っているものである:ここ
char key_in[17];
char plaintext_in[17];
//64-bit long variables to hold the 64-bit hex values in the input file
unsigned long long key, plaintext;
//I read an entry from the file with fscanf
fscanf(infile,"%s %s",&key_in, &plaintext_in)
//convert the numbers from hex to unsigned long long with strtoull
key = strtoull(key_in, NULL, 16);
plaintext = strtoull(plaintext_in, NULL, 16);
//initialize arrays with 64 positions that will hold the
//binary representation of the key and plaintext
int key_arr[64];
int pt_arr[64];
//fill the arrays with the binary representations
//of the plaintext and the key
int64_to_bin_array(key, key_arr, 64);
int64_to_bin_array(plaintext, pt_arr, 64);
//print both arrays
printArray(key_arr, 64);
printArray(pt_arr, 64);
は、私がint64_to_bin_array
とprintArray
を作成した機能は以下のとおりです。
/* Converts from an unsigned long long into an array of
integers that form the binary representation of a */
void int64_to_bin_array(unsigned long long a, int *b, int length)
{
int i;
for(i = 0; i < length; i++)
{
*(b+i) = (a >> i) & 1; //store the ith bit in b[i]
}
}
/* prints a one-dimensional array given
a pointer to it, and its length */
void printArray(int *arr, int length)
{
int i;
for(i = 0; i < length; i++)
{
printf("%d ", *(arr + i));
}
printf("\n\n");
}
私はしかし、第三の入力のためのアレイを印刷するとき、私は間違った結果を受け取ります:
入力(16進数):
(バイナリで)出力:私はミスを犯した場所
1 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00001100
2 10000000 00000000 00000000 00000000 00000000 00000000 00000000 00001000
は誰でも見ることができますか?
EDITは、私は逆に、両方の読み取りと印刷後の正しい出力を得るが、私の問題は、私はそれを操作することができますので、私が最初にその最上位バイトを持っている配列が必要です。どのようにそれを行うことができます任意のアイデア?私はそれを新しい配列に再割り当てし、逆に要素をコピーする必要がありますか?
'Bを使用して、[i]の代わりに' '*(B + I)の'、それは明確です。 – Borealid
2つの質問: - 最下位ビットのビット数とは何ですか? - ビットを印刷するときは、最下位ビットを最初に(左に)、最後に(左に)印刷する必要がありますか? –
@DaleHagglund人によって作成されたファイルから16進値が読み込まれているので、すべてビッグエンディアンでなければなりません。 –