2バイト配列に16ビットの数値をコピーすると、なぜ配列の最初のインデックスにのみコピーされるのか不思議です。 私のコードは次のとおりです。16ビット整数を2バイト配列にコピー
#include <iostream>
#include <stdint.h>
#include <stdio.h>
#include <cstring>
using namespace std;
int main(){
uint16_t my_num = 1; // This should be 0000 0000 0000 0001, right?
unsigned char my_arr[2]; // This should hold 16 bits, right?
memcpy(my_arr, &my_num, sizeof(my_num)); // This should make my_arr = {00000000, 00000001}, right?
printf("%x ", my_arr[0]);
printf("%x ", my_arr[1]);
cout << endl;
// "1 0" is printed out
return 0;
}
ありがとうございます。
あなたのマシンはリトルエンディアンのようです。連続するバイトは、逆順に格納されます。最初は '0x01'、その後は' 0x00'です。 –
あなたは '00000001 000000000' – Shark