% Filename: testHexView_02.m
%
% Execute: octave -qf testHexView_02.m [filename]
%
% Date: 2013. 8. 18.
function printUsage()
printf("Usage: octave -qf testHexView_02.m [filename]\n");
endfunction
function y = getFileSize(fd)
n = ftell(fd);
fseek(fd, 0, SEEK_END);
x = ftell(fd);
fseek(fd, n, SEEK_SET);
y = x;
endfunction
function y = toHex(n)
s = "";
x1 = bitshift(bitand(n, 0xF0), -4);
x2 = bitand(n, 0xF);
if (x1 < 10)
s = strcat(s, char(toascii('0') + x1));
else
s = strcat(s, char(toascii('A') + (x1 - 10)));
endif
% printf("s = %s\n", s);
if (x2 < 10)
s = strcat(s, char(toascii('0') + x2));
else
s = strcat(s, char(toascii('A') + (x2 - 10)));
endif
% printf("s = %s\n", s);
y = s;
endfunction
function y = toHex8(n)
s = "";
% printf("s = %s\n", s);
x1 = bitshift(bitand(n, 0xF0000000), -28);
x2 = bitshift(bitand(n, 0xF000000), -24);
x3 = bitshift(bitand(n, 0xF00000), -20);
x4 = bitshift(bitand(n, 0xF0000), -16);
x5 = bitshift(bitand(n, 0xF000), -12);
x6 = bitshift(bitand(n, 0xF00), -8);
x7 = bitshift(bitand(n, 0xF0), -4);
x8 = bitand(n, 0xF);
if (x1 < 10)
s = strcat(s, char(toascii('0') + x1));
else
s = strcat(s, char(toascii('A') + (x1 - 10)));
endif
if (x2 < 10)
s = strcat(s, char(toascii('0') + x2));
else
s = strcat(s, char(toascii('A') + (x2 - 10)));
endif
if (x3 < 10)
s = strcat(s, char(toascii('0') + x3));
else
s = strcat(s, char(toascii('A') + (x3 - 10)));
endif
if (x4 < 10)
s = strcat(s, char(toascii('0') + x4));
else
s = strcat(s, char(toascii('A') + (x4 - 10)));
endif
%% s = sprintf("%s -", s);
if (x5 < 10)
% s = strcat(s, char(toascii('0') + x5));
s = sprintf("%s %c", s, char(toascii('0') + x5));
else
% s = strcat(s, char(toascii('A') + (x5 - 10)));
s = sprintf("%s %c", s, char(toascii('A') + (x5 - 10)));
endif
if (x6 < 10)
s = strcat(s, char(toascii('0') + x6));
else
s = strcat(s, char(toascii('A') + (x6 - 10)));
endif
if (x7 < 10)
s = strcat(s, char(toascii('0') + x7));
else
s = strcat(s, char(toascii('A') + (x7 - 10)));
endif
if (x8 < 10)
s = strcat(s, char(toascii('0') + x8));
else
s = strcat(s, char(toascii('A') + (x8 - 10)));
endif
y = s;
endfunction
arg_list = argv ();
if nargin < 1
printUsage();
quit;
endif
fname = arg_list{1};
if exist(fname, "dir")
printf("\"%s\" is a directory.\n", fname);
quit;
endif
if ! exist(fname, "file")
printf("The file \"%s\" does not exist.\n", fname);
quit;
endif
f = fopen(fname, "rb");
fsize = getFileSize(f);
printf("The size of the file \"%s\" is %d.\n", fname, fsize);
printf("\n");
fseek(f, 0, SEEK_SET);
n = 0;
dum = "";
while n < fsize
if mod(n, 16) == 0
printf("%s: ", toHex8(n));
endif
[val, count] = fread (f, 1);
if mod(n, 16) == 8
printf("-%s", toHex(val));
else
printf(" %s", toHex(val));
endif
if toascii(val) < toascii(' ') || toascii(val) > 0x7F
dum = sprintf("%s%c", dum, '.');
else
dum = sprintf("%s%c", dum, toascii(val));
endif
if mod(n, 16) == 15
printf(" |%s|\n", dum);
dum = "";
endif
n = n + 1;
endwhile
if mod(n, 16) > 0
for i = 1 : 16 - mod(n, 16)
printf(" ");
endfor
printf(" |%s", dum);
for i = 1 : 16 - mod(n, 16)
printf(" ");
endfor
printf("|\n");
dum = "";
endif
fclose(f);
printf("\nRead %d bytes.\n", n);
Read 12 bytes.
Read 130 bytes.