LoginSignup
3
1

More than 1 year has passed since last update.

Endian確認プログラム、64bit版追加(C言語)。, docker(84)プログラムちょい替え(3)

Last updated at Posted at 2019-03-27

https://ja.wikipedia.org/wiki/エンディアン
に、下記のようなEndian確認用のプログラムがあった。

endian.c
#include <stdint.h>
#include <stdio.h>

int main (int argc, char **argv)
{
    union
    {
        uint32_t b4 ;     /* 4byte    */
        uint16_t b2 [2] ; /* 2byte×2 */
        uint8_t b1 [4] ;  /* 1byte×4 */
    } bytes ;

    bytes.b4 = 0x12345678 ;
    printf ("bytes.b4: %08X\n", bytes.b4) ;
    printf ("bytes.b2: %04X, %04X\n", bytes.b2[0], bytes.b2[1]) ;
    printf ("bytes.b1: %02X, %02X, %02X, %02X\n", bytes.b1[0], bytes.b1[1], bytes.b1[2], bytes.b1[3]) ;
    return 0 ;
}

せっかくなので64ビットに拡張した。

endian2.c
// https://ja.wikipedia.org/wiki/エンディアン
// arranged by Dr. Kiyoshi  Ogawa
#include <stdint.h>
#include <stdio.h>

int main (int argc, char **argv)
{
    union
    {
        uint64_t b8	; // 8byte
        uint32_t b4 [2] ; // 4byte x 2 
        uint16_t b2 [4] ; // 2byte × 4 
        uint8_t b1 [8] ;  // 1byte × 8 
    } bytes ;

    bytes.b8 = 0x123456789ABCDEF0 ;
    printf ("bytes.b8: %16llX\n", bytes.b8);
    printf ("bytes.b4: %08X, %08X \n", bytes.b4[0], bytes.b4[1]) ;
    printf ("bytes.b2: %04X, %04X, %04X, %04X \n", bytes.b2[0], bytes.b2[1], bytes.b2[2], bytes.b2[3]) ;
    printf ("bytes.b1: %02X, %02X, %02X, %02X, %02X, %02X, %02X, %02X \n", bytes.b1[0], bytes.b1[1], bytes.b1[2], bytes.b1[3], bytes.b1[4], bytes.b1[5], bytes.b1[6], bytes.b1[7]) ;
    return 0 ;
}

mac book pro(intel corei5)での実行結果は

$ cc endian2.c
$ ./a.out
bytes.b8: 123456789ABCDEF0
bytes.b4: 9ABCDEF0, 12345678 
bytes.b2: DEF0, 9ABC, 5678, 1234 
bytes.b1: F0, DE, BC, 9A, 78, 56, 34, 12 

64bit出力のため%16llX としたところは、

$ cc -v
Apple LLVM version 10.0.0 (clang-1000.11.45.5)
Target: x86_64-apple-darwin17.7.0
$ cc endian2.c
endian2.c:17:30: warning: format specifies type 'unsigned int' but the argument
      has type 'uint64_t' (aka 'unsigned long long') [-Wformat]
        printf ("bytes.b8: %16X\n", bytes.b8);
                           ~~~~     ^~~~~~~~
                           %16llX
1 warning generated.

という警告で、clangが治し方を教えてくださった。
dockerのgccでは、

$ cc -v
gcc version 6.3.0 20170516 (Debian 6.3.0-18+deb9u1) 
$ cc endian2.c
$ ./a.out
bytes.b8:         9ABCDEF0
bytes.b4: 9ABCDEF0, 12345678 
bytes.b2: DEF0, 9ABC, 5678, 1234 
bytes.b1: F0, DE, BC, 9A, 78, 56, 34, 12 

警告ださずに、知らんぷりして、半分しか出力してくれなかった。
「%16llX」に治したら、ちゃんと全部出力するようになった。

docker hubに登録した。実行するには、

docker run -it kaizenjapan/wireless_withc /bin/bash

dockerでは、

# cd wireless
# cc endian2.c
# ./a.out

@SaitoAtsushi さんからのコメントで直した版

endian3.c
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>

int main (int argc, char **argv)
{
    union
    {
	uint64_t b8	; // 8byte
        uint32_t b4 [2] ; // 4byte x 2 
        uint16_t b2 [4] ; // 2byte × 4 
        uint8_t b1 [8] ;  // 1byte × 8 
    } bytes ;

    bytes.b8 = 0x123456789ABCDEF0 ;
    printf ("bytes.b8: %16" PRIX64 "\n", bytes.b8);
    printf ("bytes.b4: %08" PRIX32 ", %08" PRIX32 " \n", bytes.b4[0], bytes.b4[1]) ;
    printf ("bytes.b2: %04" PRIX16 ", %04" PRIX16 ", %04" PRIX16 ", %04" PRIX16 " \n", bytes.b2[0], bytes.b2[1], bytes.b2[2], bytes.b2[3]) ;
    printf ("bytes.b1: %02" PRIX8 ", %02" PRIX8 ", %02" PRIX8 ", %02" PRIX8 ", %02" PRIX8 ", %02" PRIX8 ", %02" PRIX8 ", %02" PRIX8 " \n", bytes.b1[0], bytes.b1[1], bytes.b1[2], bytes.b1[3], bytes.b1[4], bytes.b1[5], bytes.b1[6], bytes.b1[7]) ;
    return 0 ;
}

p.s.
上記dockerにはpythonの別プログラムが2つ入っている。
cal.py
log.py

# ./cal.py
# ./log.py

で実行するはず。

文書履歴(document history)

ver. 0.01 初稿 20190327 夕食前
ver. 0.02 docker 追記 20190327 夕食後
ver. 0.03 dockerでの実行方法追記 20190327 夜
ver. 0.04 表題変更 20190331
ver. 0.05 表題追記 20190514

最後までおよみいただきありがとうございました。

いいね 💚、フォローをお願いします。

Thank you very much for reading to the last sentence.

Please press the like icon 💚 and follow me for your happy life.

3
1
2

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
3
1