質問編集履歴
1
コードの追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -14,4 +14,146 @@
|
|
14
14
|
|
15
15
|
https://teratail.com/questions/204968
|
16
16
|
上記の方の質問を見ての質問です。
|
17
|
-
よろしくお願い致します。
|
17
|
+
よろしくお願い致します。
|
18
|
+
```C
|
19
|
+
#include <stdio.h>
|
20
|
+
#include <stdlib.h>
|
21
|
+
#pragma warning(disable: 4996)
|
22
|
+
|
23
|
+
#define H_PRT 0x02// ヘッダ印字オプション
|
24
|
+
#define C_PRT 0x01// 文字印字オプション
|
25
|
+
#define ROW 16 // 1行に表示する文字
|
26
|
+
#define TESTBUF 512 //テストデータ用のバッファ
|
27
|
+
#define TEXTBUF 1024 //テキストファイル用のバッファ
|
28
|
+
|
29
|
+
//プロトタイプ宣言
|
30
|
+
void dump(char* title, unsigned char* staddr, int offset, int dsize, char opt);
|
31
|
+
|
32
|
+
void main(void) {
|
33
|
+
|
34
|
+
//int filcnt;// フィル用のカウンタ
|
35
|
+
|
36
|
+
//char bin_data[TESTBUF];// テストデータ用のバッファ
|
37
|
+
//char asc_data[] = "01234567809 ABCあいうえおかきくけこDEFGHIJKLMNOPQRSTUVWXYZ 漢字表示のテスト abcdefghijklmnopqrstuvwxyz01234567809ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz";
|
38
|
+
char txt_data[TEXTBUF]; // テキストファイル読み込み用
|
39
|
+
//char txt;
|
40
|
+
int readnum;
|
41
|
+
|
42
|
+
FILE* file;
|
43
|
+
file = fopen("s-jis2.txt", "rb");
|
44
|
+
if (file == NULL) {
|
45
|
+
printf("ファイルが開けません");
|
46
|
+
exit(1);
|
47
|
+
}
|
48
|
+
|
49
|
+
readnum = fread(txt_data, sizeof(unsigned char), TEXTBUF, file);
|
50
|
+
|
51
|
+
if (readnum == 0) {
|
52
|
+
|
53
|
+
printf("ファイルの内容がありません");
|
54
|
+
exit(1);
|
55
|
+
}
|
56
|
+
|
57
|
+
fclose(file);
|
58
|
+
|
59
|
+
//for (filcnt = 0; filcnt < 512; filcnt++) bin_data[filcnt] = filcnt & 0xff; // テスト用のテーブルを0からFFで埋める
|
60
|
+
|
61
|
+
// dump("\nASCIIdata 文字印字あり", asc_data, 0, 65, H_PRT + C_PRT);
|
62
|
+
// dump("\nBINARYdata 文字印字あり", bin_data, 0, 512, H_PRT + C_PRT);
|
63
|
+
dump("テキストファイル", txt_data, 0, readnum, H_PRT + C_PRT);
|
64
|
+
}
|
65
|
+
|
66
|
+
|
67
|
+
void dump(char* title, unsigned char* staddr, int offset, int dsize, char opt) {
|
68
|
+
|
69
|
+
int startcnt = 0;// ダンプするバイト数のカウント
|
70
|
+
int bytecnt; // 16回ループさせる
|
71
|
+
int savecnt = 0;
|
72
|
+
int address = 0; //アドレスの表示
|
73
|
+
int onhold = 0; //文字出力が最後のバイトだった時の一時保留
|
74
|
+
staddr += offset;
|
75
|
+
|
76
|
+
char addrhead[] = "Addr";
|
77
|
+
char hexa[] = "0 1 2 3 4 5 6 7 8 9 A B C D E F";
|
78
|
+
char charprint[] = "0 2 4 6 8 A C E";
|
79
|
+
|
80
|
+
|
81
|
+
printf("\n%s\n", title);
|
82
|
+
printf(" %s %s %s\n", addrhead, hexa, charprint);
|
83
|
+
|
84
|
+
printf("-------- ---- ---- ---- ---- ---- ---- ---- ---- ----------------\n");
|
85
|
+
|
86
|
+
while (startcnt < dsize) {
|
87
|
+
|
88
|
+
if (onhold != 0) { //前回最後が漢字だった場合、漢字を出力して改行
|
89
|
+
|
90
|
+
printf("%c%c", onhold, staddr[startcnt]);
|
91
|
+
printf("\n");
|
92
|
+
}
|
93
|
+
|
94
|
+
printf("%08x ", address);
|
95
|
+
|
96
|
+
savecnt = startcnt;
|
97
|
+
for (bytecnt = 0; bytecnt < ROW; bytecnt++) {
|
98
|
+
|
99
|
+
if (startcnt < dsize) {
|
100
|
+
printf("%02x", staddr[startcnt]); //16進数で出力
|
101
|
+
}
|
102
|
+
else {
|
103
|
+
printf(" ");
|
104
|
+
}
|
105
|
+
if (startcnt % 2 != 0) {
|
106
|
+
printf(" ");
|
107
|
+
}
|
108
|
+
startcnt++;
|
109
|
+
}
|
110
|
+
startcnt = savecnt;
|
111
|
+
|
112
|
+
//文字の出力
|
113
|
+
for (bytecnt = 0; bytecnt < ROW; bytecnt++) {
|
114
|
+
|
115
|
+
if (onhold != 0) { //前回最後が漢字だった場合 次の行の最初にスペースと初期化
|
116
|
+
|
117
|
+
printf(" ");
|
118
|
+
onhold = 0;
|
119
|
+
bytecnt++;
|
120
|
+
}
|
121
|
+
|
122
|
+
//漢字だった場合
|
123
|
+
if ((staddr[startcnt] >= 0x81 && staddr[startcnt] < 0xa0) || (staddr[startcnt] >= 0xe0 && staddr[startcnt] < 0xfd)) {
|
124
|
+
|
125
|
+
if (bytecnt > ROW - 1) { //漢字で最後のバイトの場合、保留
|
126
|
+
|
127
|
+
onhold = staddr[startcnt];
|
128
|
+
bytecnt++;
|
129
|
+
|
130
|
+
}
|
131
|
+
else {//最後のバイトでなければ出力
|
132
|
+
|
133
|
+
printf("%c%c", staddr[startcnt], staddr[startcnt + 1]);
|
134
|
+
startcnt++;
|
135
|
+
bytecnt++;
|
136
|
+
}
|
137
|
+
}
|
138
|
+
if (startcnt < dsize) {
|
139
|
+
|
140
|
+
if ((staddr[startcnt] >= 0x20 && staddr[startcnt] < 0x7f) || (staddr[startcnt] >= 0xa0 && staddr[startcnt] < 0xe0)) {
|
141
|
+
|
142
|
+
printf("%c", staddr[startcnt]);
|
143
|
+
}
|
144
|
+
|
145
|
+
}
|
146
|
+
else {
|
147
|
+
|
148
|
+
printf(" ");
|
149
|
+
}
|
150
|
+
startcnt++;
|
151
|
+
}
|
152
|
+
if (onhold == 0) { //最後が漢字でなければ改行
|
153
|
+
printf("\n");
|
154
|
+
}
|
155
|
+
address += ROW; //addressを出力しただけ足す
|
156
|
+
|
157
|
+
}
|
158
|
+
}
|
159
|
+
```
|