OCaml 언어 소스:
Filename: testHexView_02.ml
Execute: ocaml testHexView_02.ml [filename]
Or
Compile: ocamlc -o testHexView_02 testHexView_02.ml
Execute: ./testHexView_02 [filename]
Or
Compile: ocamlc -o testHexView_02.exe testHexView_02.ml
Execute: testHexView_02 [filename]
Date: 2013. 8. 20.
*)
open Printf;;
let printUsage() =
printf("Usgae: testHexView_02 [filename]\n");;
let toHex n =
let s = ref "" in
let x1 = (n land 0xF0) lsr 4 in
let x2 = n land 0xF in
if x1 < 10 then
s := !s ^ Char.escaped (Char.chr( Char.code('0') + x1 ))
else
s := !s ^ Char.escaped (Char.chr( Char.code('A') + (x1 - 10) ));
if x2 < 10 then
s := !s ^ Char.escaped (Char.chr( Char.code('0') + x2 ))
else
s := !s ^ Char.escaped (Char.chr( Char.code('A') + (x2 - 10) ));
!s
let toHex8 n =
let s = ref "" in
(* let x1 = (n land 0xF0000000) lsr 28 in *)
let x1 = (n land 0x70000000) lsr 28 in
let x2 = (n land 0xF000000) lsr 24 in
let x3 = (n land 0xF00000) lsr 20 in
let x4 = (n land 0xF0000) lsr 18 in
let x5 = (n land 0xF000) lsr 12 in
let x6 = (n land 0xF00) lsr 8 in
let x7 = (n land 0xF0) lsr 4 in
let x8 = n land 0xF in
if x1 < 10 then
s := !s ^ Char.escaped (Char.chr( Char.code('0') + x1 ))
else
s := !s ^ Char.escaped (Char.chr( Char.code('A') + (x1 - 10) ));
if x2 < 10 then
s := !s ^ Char.escaped (Char.chr( Char.code('0') + x2 ))
else
s := !s ^ Char.escaped (Char.chr( Char.code('A') + (x2 - 10) ));
if x3 < 10 then
s := !s ^ Char.escaped (Char.chr( Char.code('0') + x3 ))
else
s := !s ^ Char.escaped (Char.chr( Char.code('A') + (x3 - 10) ));
if x4 < 10 then
s := !s ^ Char.escaped (Char.chr( Char.code('0') + x4 ))
else
s := !s ^ Char.escaped (Char.chr( Char.code('A') + (x4 - 10) ));
s := !s ^ " ";
if x5 < 10 then
s := !s ^ Char.escaped (Char.chr( Char.code('0') + x5 ))
else
s := !s ^ Char.escaped (Char.chr( Char.code('A') + (x5 - 10) ));
if x6 < 10 then
s := !s ^ Char.escaped (Char.chr( Char.code('0') + x6 ))
else
s := !s ^ Char.escaped (Char.chr( Char.code('A') + (x6 - 10) ));
if x7 < 10 then
s := !s ^ Char.escaped (Char.chr( Char.code('0') + x7 ))
else
s := !s ^ Char.escaped (Char.chr( Char.code('A') + (x7 - 10) ));
if x8 < 10 then
s := !s ^ Char.escaped (Char.chr( Char.code('0') + x8 ))
else
s := !s ^ Char.escaped (Char.chr( Char.code('A') + (x8 - 10) ));
!s
let load_file f =
let ic = open_in f in
let n = in_channel_length ic in
let s = String.create n in
really_input ic s 0 n;
close_in ic;
(s);;
if Array.length Sys.argv < 2 then begin
printUsage() ;
exit 1
end;
let fname = Sys.argv.(1) in
if not (Sys.file_exists fname) then begin
printf "The file \"%s\" does not exist.\n" fname;
exit 1
end;
if (Sys.is_directory fname) then begin
printf "\"%s\" is a directory.\n" fname;
exit 1
end;
let dum = ref "" in
let bstr = load_file Sys.argv.(1) in
let fsize = String.length bstr in
printf "The size of the file \"%s\" is %d.\n" fname fsize;
print_newline();
let n = ref 0 in
while (!n < fsize) do
if (!n mod 16) == 0 then begin
printf "%s: " (toHex8 !n)
end;
let c = bstr.[!n] in
(* print_char c *)
if (!n mod 16) == 8 then
printf "-%s" (toHex (Char.code c))
else
printf " %s" (toHex (Char.code c));
if (Char.code c) < (Char.code ' ') or (Char.code c) > 0x7F then
dum := !dum ^ "."
else
dum := !dum ^ (Char.escaped c);
if (!n mod 16) == 15 then begin
printf " |%s|\n" !dum;
dum := ""
end;
n := !n + 1;
done;
if (!n mod 16) > 0 then begin
for i = 0 to (pred (16 - (!n mod 16))) do
printf " "
done;
printf " |%s" !dum;
for i = 0 to (pred (16 - (!n mod 16))) do
printf " "
done;
printf "|\n";
dum := ""
end;
print_newline();
printf "Read %d bytes.\n" !n;
실행 예 1> testHexView_02 temp_1.bin
The size of the file "temp_1.bin" is 12.
0000 0000: 48 65 6C 6C 6F 20 74 68-65 72 65 0A |Hello there. |
Read 12 bytes.
실행 예 2> testHexView_02 myFile.ser
The size of the file "myFile.ser" is 130.
0000 0000: AC ED 00 05 73 72 00 06-50 65 72 73 6F 6E 07 31 |....sr..Person.1|
0000 0010: 46 DB A5 1D 44 AB 02 00-03 49 00 03 61 67 65 4C |F...D....I..ageL|
0000 0020: 00 09 66 69 72 73 74 4E-61 6D 65 74 00 12 4C 6A |..firstNamet..Lj|
0000 0030: 61 76 61 2F 6C 61 6E 67-2F 53 74 72 69 6E 67 3B |ava/lang/String;|
0000 0040: 4C 00 08 6C 61 73 74 4E-61 6D 65 71 00 7E 00 01 |L..lastNameq.~..|
0000 0050: 78 70 00 00 00 13 74 00-05 4A 61 6D 65 73 74 00 |xp....t..Jamest.|
0000 0060: 04 52 79 61 6E 73 71 00-7E 00 00 00 00 00 1E 74 |.Ryansq.~......t|
0000 0070: 00 07 4F 62 69 2D 77 61-6E 74 00 06 4B 65 6E 6F |..Obi-want..Keno|
0000 0080: 62 69 |bi |
Read 130 bytes.
'프로그래밍 > OCaml' 카테고리의 다른 글
OCaml 언어, F# 언어. Haskell 언어로 각각 리스트 쉽게 생성하기 그리고 zipWith 을 이용한 행렬 곱셈 (0) | 2013.02.08 |
---|---|
OCaml 언어로 평방근, 입방근, n제곱근 구하는 함수를 구현하고 테스트하기 (0) | 2013.02.06 |
30000! 빨리 계산하기 with OCaml (0) | 2013.02.06 |
int 타입과 bigint 타입 간 상호 타입 변환 with OCaml (0) | 2013.02.03 |
Pollard's rho method 소개: 정수의 인수분해(factorizing integers) with OCaml (0) | 2013.02.02 |