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
,

역삼각함수란 삼각함수의 역함수를 의미하고,

역쌍곡선함수란 쌍곡선함수의 역함수를 의미한다.

수학에서 sin 함수의 역함수는 arcsin 으로 표기되는데, ErLang 언어에서는 asin 함수로 구현되어 있다.

ErLang 언어는 Haskell 언어 처럼 순수 함수형 언어이다.

ErLang 언어로 프로그래밍할 때는 각 명령 줄 끝이 콤마(,)로 끝나는지, 마침표(.)로 끝나는지 신경을 단단히 써야 한다. 콤마로 끝나는 줄은 그 다음에 명령이 이어진다는 의미이고, 마침표로 끝나는 것은 그 블록이 (Ruby 언어나 Lua 언어의 end 처럼) 블럭이 끝남을 의미한다.

영문 위키피디아의 ErLang 프로그래밍 언어 설명: http://en.wikipedia.org/wiki/Erlang_(programming_language)

(참고. ErLang 언어는 대소 문자를 구분하며 타입에 동적인 언어이다. 그리고 변수는 대문자로 써야 한다.) 

아래의 소스는 컴파일 없이 erl 로 실행하면 되지만,. erlc 로 컴파일 할 경우 생성되는 실행 파일이 *.beam 파일이며 이것을 실행할 때도 조금은 복잡하게 실행해야 한다. (아래의 주석문 참조)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Filename: testArcSine.erl
%
% Compile: erlc testArcSine.erl
% Execute: erl -run testArcSine main -run init stop -noshell
%   Or
% Execute: erl
%        > c(testArcSine).
%        > testArcSine:main().
%
% Author: P. Kim pkim _AT_ scripts.pe.kr
%
% See: http://erlware.blogspot.kr/2010/10/console-applications-in-erlang.html
%
% Created by PKim pkim _AT_ scripts.pe.kr  2013. 1. 3.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

-module(testArcSine).
-author(pkim _AT_ scripts.pe.kr).
-export([main/0]).

asinh(X) -> math:log(X + math:sqrt(X*X + 1)).
acosh(X) -> math:log(X + math:sqrt(X*X - 1)).

main() ->
    X = -0.9,
    Y = math:asin(X),
    io:format("y = asin(~w) = ~.9f~n",  [X, Y]),
    io:format("sin(~.9f) = ~w~n",  [Y, math:sin(Y)]),
    io:format("~n"),

    X1 = 1.1,
    U = acosh(X1),
    io:format("u = acosh(~w) = ~.10f~n",  [X1, U]),
    V = asinh(X1),
    io:format("v = asinh(~w) = ~.10f~n",  [X1, V]),
    io:format("cosh(~.10f) = ~w~n",  [U, math:cosh(U)]),
    io:format("sinh(~.10f) = ~w~n",  [Y, math:cosh(Y)]).


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Output:
% y = asin(-0.9) = -1.119769515
% sin(-1.119769515) = -0.9
%
% u = acosh(1.1) = 0.4435682544
% v = asinh(1.1) = 0.9503469298
% cosh(0.4435682544) = 1.1
% sinh(-1.1197695150) = 1.6952514438291528
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

 

 

 

Posted by Scripter
,