0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ESP32のバイトオーダー調査メモ

Last updated at Posted at 2020-06-17

直ぐに忘れがちなので。

調べた時のコード

main.c

typedef struct __attribute__((packed)) {
	union {
		uint8_t row[8];
		char text[8];
		int64_t longValue;
		int32_t intValue;
		int16_t wordValue;
		int8_t byteValue;
		void * pointer;
		float floatValue;
		double doubleValue;
	};
	uint8_t size;
} test_u;

void app_main() {
	_i("size: %u", sizeof(test_u));

	char text[] = "hello, world!!";
	test_u _64, _32, _16, _8, _8a, _f, _d, _p;
	test_u *u[] = {&_64, &_32, &_16, &_8, &_8a, &_f, &_d, &_p};
	for(int i=0; i<8; i++) memset(u[i], 0, sizeof(test_u));

	_64.longValue = -89750967853ll;
	_32.intValue = 987539l;
	_16.wordValue = -4125;
	_8.byteValue = -112;
	_i("size: %u", sizeof(test_u));
	strncpy(_8a.text, text, 8);
	_f.floatValue = 145.412362980f;
	_d.doubleValue = 819047.10892790;
	_p.pointer = text;

	for(int i=0; i<8; i++) {
		char b[128];
		for(int j=0; j<8; j++) {
			sprintf(b+(j*3), "%2x ", u[i]->row[j]);
		}
		_i("%d: %s", i, b);
	}
	_i("pointer address: %p", text);
}
size: 9
0: d3 e9 6c 1a eb ff ff ff // -89,750,967,853(dec) -> ff ff ff eb 1a 6c e9 d3(hex) を逆順詰め
1: 93 11  f  0  0  0  0  0 //         987,539(dec) ->  f 11 93(hex)
2: e3 ef  0  0  0  0  0  0 //          -4,125(dec) ->    ef e3(hex)
3: 90  0  0  0  0  0  0  0 //            -112(dec) ->       90(hex)
4: 68 65 6c 6c 6f 2c 20 77
// h(0x68) e(0x65) l(0x6c) l(0x6c) o(0x6f) ,(0x2c) _(0x20) w(0x77) [o(0x6f) r(0x72) l(0x6c) d(0x64) !(0x21) !(0x21)]

// 浮動小数点の内部表現は下記で調査
// https://tools.m-bsys.com/calculators/ieee754.php
5: 91 69 11 43  0  0  0  0 // 145.412362980f -> 0b 0100 0011 0001 0001 0110 1001 1001 0000 (0x43 11 69 90) を逆順詰め
6: d0 65 c5 37 ce fe 28 41 // 819047.10892790 -> 0b 0100 0001 0010 1000 1111 1110 1100 1110 0011 0111 1100 0101 0110 0101 1101 0000
// (0x 41 28 fe ce 37 c5 65 d0)
7: 30 c8 fb 3f  0  0  0  0 
pointer addres: 0x3ffbc830

バイトオーダーで単純な逆順になることが確認できました。

32bit以上のデータ型はワード毎のリトルエンディアン、
8975093であれば、0x0088f2f5の88 00 f5 f2になると勘違いしていたので、
改めて、仕様の理解が不十分なことは確認することが大事だと思いました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?