こんにちは
305419896(16進数0x12345678)をバイト、ワード、ビットで表示するプログラムを作成しています。
ビットで表示する際に2進数変換後にリトルエンディアンからビッグエンディアンに変換したときbit[0]~bit[15]に不明な文字が入力されてしまいます。bit[16]以降は正常に表示されているのですが何が原因なのかわかりません。
下記コードの何が原因なのかご教授いただきたいです。
可能ならば修正コードも載せていただけると非常にありがたいです。
C
1#define _CRT_SECURE_NO_WARNINGS//scanf警告解除 2#include <stdio.h> 3#include <stdlib.h> 4#include <string.h> 5 6#define WORD_SIZE (2) 7#define BYTE_SIZE (4) 8#define BIT_SIZE (32) 9 10typedef union 11{ 12 unsigned int data; /* 4byte */ 13 unsigned short word[WORD_SIZE]; /* 2byte×2 */ 14 unsigned char byte[BYTE_SIZE]; /* 1byte×4 */ 15 unsigned char bit[BIT_SIZE]; /* 1byte×32 */ 16}DATA; 17 18//変数宣言 19int byte_cnt;//バイトサイズカウンタ 20int bit_cnt;//ビットサイズカウンタ 21 22//関数プロトタイプ宣言 23void output_data(DATA *data); 24 25//メイン関数 26int main(int argv, char* arg[]) { 27 28 DATA init_data;//入力用データ初期化用 29 DATA *union_data;//入力用データ 30 31 union_data = &init_data;//ポインタ初期化 32 33 union_data->data = 305419896; 34 35 printf("入力データ:"); 36 printf("%d\n", union_data->data); 37 printf("=======================================\n"); 38 39 output_data(union_data);//データ出力関数呼び出し 40 41 42//デバッグ用 43#if 1 44 int debag; 45 scanf("%d", &debag); 46#endif 47 48 return 0; 49} 50 51//データ出力関数 52void output_data(DATA *data) { 53 54 DATA endian_init;//出力用データ初期化用 55 DATA *endian_data;//出力用データ 56 57 endian_data = &endian_init;//ポインタ初期化 58 59 /***************************/ 60 /*データ名表示 */ 61 /***************************/ 62 printf("=======================================\n"); 63 printf("DATA : 0x%08X\n", data->data); 64 65 /***************************/ 66 /*ワード表示 */ 67 /***************************/ 68 printf("=======================================\n"); 69 endian_data->word[0] = data->word[1];//エンディアン変換 70 endian_data->word[1] = data->word[0];//エンディアン変換 71 printf("WORD(Hi) : 0x%04X\n", endian_data->word[0]); 72 printf("WORD(Lo) : 0x%04X\n", endian_data->word[1]); 73 74 /***************************/ 75 /*バイト表示 */ 76 /***************************/ 77 printf("=======================================\n"); 78 79 for (byte_cnt = 0; byte_cnt < BYTE_SIZE; byte_cnt++) { 80 81 endian_data->byte[byte_cnt] = data->byte[(BYTE_SIZE-1)- byte_cnt];//エンディアン変換 82 83 printf("BYTE(%d) : 0x%02X\n", byte_cnt, endian_data->byte[byte_cnt]); 84 85 } 86 87 /***************************/ 88 /*ビット表示 */ 89 /***************************/ 90 printf("=======================================\n"); 91#if 0 92 for (bit_cnt = 0; bit_cnt<BIT_SIZE; bit_cnt++) { 93 /* ビットの表示 */ 94 printf("BITS(%d) : %u\n", bit_cnt, (data->data>> bit_cnt) & 1); 95 } 96#endif 97 for (bit_cnt = 0; bit_cnt<BIT_SIZE; bit_cnt++) { 98 if (((data->data >> bit_cnt) & 1 )== 1) {//2進数変換した値が1の場合 99 endian_data->bit[(BIT_SIZE-1)- bit_cnt] = '1';//エンディアン変換 100 } 101 else if (((data->data >> bit_cnt) & 1) == 0) {//2進数変換した値が0の場合 102 endian_data->bit[(BIT_SIZE-1)- bit_cnt] = '0';//エンディアン変換 103 } 104 printf("BITS(%d) : %c\n", bit_cnt, endian_data->bit[bit_cnt]); 105 } 106 107}

回答4件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/01/21 13:18