ErLang 언어 소스:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Filename: testHexView_03.erl
%
% Compile: erlc testHexView_03.erl
% Execute: erl -f [filename] -run testHexView_03 main -run init stop -noshell
%
%    Date: 2013. 8. 24.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

-module(testHexView_03).
-export([main/0]).

toHex(N) ->
    X1 = (N band 16#F0) bsr 4,
    X2 = N band 16#F,
    S1 = if X1 < 10  -> string:chars($0 + X1, 1);
            X1 >= 10 -> string:chars($A + X1 - 10, 1)
         end,
    S2 = if X2 <  10 -> string:chars($0 + X2, 1);
            X2 >= 10 -> string:chars($A + X2 - 10, 1)
         end,
    S1 ++ S2.

toHex8(N) ->
    X1 = (N band 16#F0000000) bsr 28,
    X2 = (N band 16#F000000) bsr 24,
    X3 = (N band 16#F00000) bsr 20,
    X4 = (N band 16#F0000) bsr 16,
    X5 = (N band 16#F000) bsr 12,
    X6 = (N band 16#F00) bsr 8,
    X7 = (N band 16#F0) bsr 4,
    X8 = N band 16#F,
    S1 = if X1 < 10  -> string:chars($0 + X1, 1);
            X1 >= 10 -> string:chars($A + X1 - 10, 1)
         end,
    S2 = if X2 <  10 -> string:chars($0 + X2, 1);
            X2 >= 10 -> string:chars($A + X2 - 10, 1)
         end,
    S3 = if X3 <  10 -> string:chars($0 + X3, 1);
            X3 >= 10 -> string:chars($A + X3 - 10, 1)
         end,
    S4 = if X4 <  10 -> string:chars($0 + X4, 1);
            X4 >= 10 -> string:chars($A + X4 - 10, 1)
         end,
    S5 = if X5 <  10 -> string:chars($0 + X5, 1);
            X5 >= 10 -> string:chars($A + X5 - 10, 1)
         end,
    S6 = if X6 <  10 -> string:chars($0 + X6, 1);
            X6 >= 10 -> string:chars($A + X6 - 10, 1)
         end,
    S7 = if X7 <  10 -> string:chars($0 + X7, 1);
           X7 >= 10 -> string:chars($A + X7 - 10, 1)
          end,
    S8 = if X8 <  10 -> string:chars($0 + X8, 1);
            X8 >= 10 -> string:chars($A + X8 - 10, 1)
         end,
    S1 ++ S2 ++ S3 ++ S4 ++ " " ++ S5 ++ S6 ++ S7 ++ S8.


raw_read_file(Path) ->
    {ok, File} = file:open(Path, [read, binary]),
    raw_read_loop(File, []).

raw_read_loop(File, Acc) ->
    case file:read(File, 1024) of
        {ok, Bytes} ->
            raw_read_loop(File, [Acc | Bytes]);
        eof ->
            file:close(File),
            iolist_to_binary(Acc);
        {error, Reason} ->
            file:close(File),
            erlang:error(Reason)
    end.


 

getHexStr(S, Offset, K, Cnt, T) ->
    if
        K =:= Cnt -> T;
        K =/= Cnt -> C0 = if
                              K =:= 8 -> "-";
                              K =/= 8 -> " "
                           end,
                     getHexStr(S, Offset, K + 1, Cnt, T ++ C0 ++ (toHex (binary:at(S, Offset + K))))
    end.


getDumStr(S, Offset, K, Cnt, T) ->
    if
        K =:= Cnt -> T;
        K =/= Cnt -> X = binary:at(S, Offset + K),
                     A = if
                            (X < 16#20) or (X > 16#7F) -> $.;
                            true                       -> X
                         end,
                     getDumStr(S, Offset, K + 1, Cnt, T ++ string:chars(A, 1))
    end.


printHexTable(S, Offset, Size) ->
    case (Offset + 16) =< Size of
      true  -> io:fwrite("~s: ", [toHex8(Offset)]),
               io:fwrite("~s", [getHexStr(S, Offset, 0, 16, "")]),
               io:fwrite("  |~s|~n", [getDumStr(S, Offset, 0 ,16, "")]),
               printHexTable(S, Offset + 16, Size);
      _Else -> io:fwrite("~s: ", [toHex8(Offset)]),
               io:fwrite("~s", [getHexStr(S, Offset, 0, Size - Offset, "")]),
               io:fwrite("~s", [string:chars(16#20,  3*(16 - Size + Offset))]),
               io:fwrite("  |~s", [getDumStr(S, Offset, 0, Size - Offset, "")]),
               io:fwrite("~s|~n", [string:chars(16#20, 16 - Size + Offset)])
    end.

 

file_exist(Filename) ->
    case file:read_file_info(Filename) of
        {ok, _}         -> true;
        {error, enoent} -> false;
        {error, Reason} -> io:format("~s is ~s~n", [Filename, Reason]),
                           false
    end.


 

main() ->
    {ok, TMP} = init:get_argument(f),
    Fname1 = lists:nth(1, TMP),
    Fname = lists:nth(1, Fname1),
    case filelib:is_dir(Fname) of
           true  -> io:format("\"~s\" is a directory.~n", [Fname]),
                    halt();
           _Else  -> io:format("")
    end,
    case file_exist(Fname) of
           false  -> io:format("The file \"~s\" does not exist.~n", [Fname]),
                     halt();
           true  -> io:format("")
    end,

    B = raw_read_file(Fname),

    io:fwrite("The size of the file \"~s\" is ~w.~n~n", [Fname, size(B)]),

    printHexTable(B, 0, size(B)),

    io:fwrite("~nRead ~w bytes.~n", [size(B)]),

    0.

 

 

싱행 예 1> erl -f temp_1.bin -run testHexView_03 main -run init stop -noshell
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.

싱행 예 1> erl -f myFile.ser -run testHexView_03 main -run init stop -noshell
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.

 

 

Posted by Scripter
,