ASCII printing
This commit is contained in:
46
hex.c
46
hex.c
@@ -1,10 +1,12 @@
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
|
||||
// TODO: ASCII/Position printing
|
||||
// TODO: Stop appending garbage at file end
|
||||
// TODO: Simplify/clean some code (useless condition/loop guard brackets)
|
||||
|
||||
#define BYTES 1024
|
||||
#define BYTES_PER_LINE 10
|
||||
#define BYTES_PER_LINE 20
|
||||
|
||||
void print_hex(unsigned char* buf, int byteno)
|
||||
{
|
||||
@@ -12,10 +14,46 @@ void print_hex(unsigned char* buf, int byteno)
|
||||
{
|
||||
if (i % BYTES_PER_LINE == 0)
|
||||
{
|
||||
if (i != 0)
|
||||
{
|
||||
printf(" ");
|
||||
for (int j=i-BYTES_PER_LINE; j<i; j++)
|
||||
{
|
||||
if (isprint(buf[j]))
|
||||
{
|
||||
printf("%c", buf[j]);
|
||||
}
|
||||
else {
|
||||
printf(".");
|
||||
}
|
||||
}
|
||||
}
|
||||
puts("");
|
||||
}
|
||||
printf("%.2X ", buf[i]);
|
||||
}
|
||||
|
||||
// ASCII: last line
|
||||
int padding = BYTES_PER_LINE - (byteno % BYTES_PER_LINE);
|
||||
if (padding < BYTES_PER_LINE)
|
||||
{
|
||||
for (int i=0; i<padding; i++)
|
||||
{
|
||||
printf(" ");
|
||||
}
|
||||
printf(" ");
|
||||
}
|
||||
|
||||
int start = byteno - (byteno % BYTES_PER_LINE);
|
||||
for (int j = start; j < byteno; j++) {
|
||||
if (isprint(buf[j])) {
|
||||
printf("%c", buf[j]);
|
||||
}
|
||||
else {
|
||||
printf(".");
|
||||
}
|
||||
}
|
||||
|
||||
puts("");
|
||||
}
|
||||
|
||||
@@ -35,7 +73,7 @@ int main(int argc, char** argv)
|
||||
for (;;)
|
||||
{
|
||||
char cmd;
|
||||
int loc;
|
||||
int loc = 0;
|
||||
scanf("%c%d", &cmd, &loc);
|
||||
switch(cmd)
|
||||
{
|
||||
@@ -51,6 +89,10 @@ int main(int argc, char** argv)
|
||||
case 'S':
|
||||
fclose(f);
|
||||
f = fopen(argv[1], "w");
|
||||
if (!f) {
|
||||
perror("Error opening file for writing");
|
||||
return -1;
|
||||
}
|
||||
fwrite(buf, 1, BYTES, f);
|
||||
fclose(f);
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user