F# 언어 소스:

(* Filename: testHexView_02.fs
 *
 * Compile: fsc testHexView_02.fs
 * Execute: testHexView_02 [filename]
 *
 * Date: 2013. 8. 18.
 *)

open System
open System.IO

let Value (c:char) =
    (int c) - (int 'A') + 1


let toHex n =
    let mutable s = ""
    let x1 = (n &&& 0xF0) >>> 4
    let x2 = n &&& 0xF
    if x1 < 10 then
        s <- s + (sprintf "%c" (char (x1 + (int '0'))))
    else
        s <- s + (sprintf "%c" (char (x1 - 10 + (int 'A'))))
    if x2 < 10 then
        s <- s + (sprintf "%c" (char (x2 + (int '0'))))
    else
        s <- s + (sprintf "%c" (char (x2 - 10 + (int 'A'))))
    s
   
let toHex8 n =
    let mutable s = ""
    let x1 = (n &&& 0xF0000000) >>> 28
    let x2 = (n &&& 0xF000000) >>> 24
    let x3 = (n &&& 0xF00000) >>> 20
    let x4 = (n &&& 0xF0000) >>> 16
    let x5 = (n &&& 0xF000) >>> 12
    let x6 = (n &&& 0xF00) >>> 18
    let x7 = (n &&& 0xF0) >>> 4
    let x8 = n &&& 0xF
    if x1 < 10 then
        s <- s + (sprintf "%c" (char (x1 + (int '0'))))
    else
        s <- s + (sprintf "%c" (char (x1 - 10 + (int 'A'))))
    if x2 < 10 then
        s <- s + (sprintf "%c" (char (x2 + (int '0'))))
    else
        s <- s + (sprintf "%c" (char (x2 - 10 + (int 'A'))))
    if x3 < 10 then
        s <- s + (sprintf "%c" (char (x3 + (int '0'))))
    else
        s <- s + (sprintf "%c" (char (x3 - 10 + (int 'A'))))
    if x4 < 10 then
        s <- s + (sprintf "%c" (char (x4 + (int '0'))))
    else
        s <- s + (sprintf "%c" (char (x4 - 10 + (int 'A'))))
    s <- s + " "
    if x5 < 10 then
        s <- s + (sprintf "%c" (char (x5 + (int '0'))))
    else
        s <- s + (sprintf "%c" (char (x5 - 10 + (int 'A'))))
    if x6 < 10 then
        s <- s + (sprintf "%c" (char (x6 + (int '0'))))
    else
        s <- s + (sprintf "%c" (char (x6 - 10 + (int 'A'))))
    if x7 < 10 then
        s <- s + (sprintf "%c" (char (x7 + (int '0'))))
    else
        s <- s + (sprintf "%c" (char (x7 - 10 + (int 'A'))))
    if x8 < 10 then
        s <- s + (sprintf "%c" (char (x8 + (int '0'))))
    else
        s <- s + (sprintf "%c" (char (x8 - 10 + (int 'A'))))
    s
    
   
[<EntryPoint>]
let main args =

    if args.Length < 1 then
        eprintfn "Usage: testHexView_02 [filename]"
        exit 1
    let fname = args.[0]

    if Directory.Exists(fname) then
          printfn "\"%s\" is a directory." fname
          exit 1

    let file = new FileInfo(fname)
    if not file.Exists then
          printfn "The file \"%s\" does not exist." fname
          exit 1
    let fsize = file.Length

    printfn "The size of the file \"%s\" is %d." fname fsize
    printfn ""

    let fs = IO.File.OpenRead fname
    let currentByte = ref 0
    let mutable n = 0
    let mutable dum = ""
    while (int64 n) < fsize do
        if (n % 16) = 0 then
            printf "%s: " (toHex8 n)
        currentByte := fs.ReadByte()
        if (int !currentByte) < (int ' ') || (int !currentByte) > 0x7F then
            dum <- dum + "."
        else
            dum <- dum + (sprintf "%c" (char !currentByte))
        if (n % 16) = 8 then
            printf "-%s" (toHex (int !currentByte))
        else
            printf " %s" (toHex (int !currentByte))
        if (n % 16) = 15 then
            printfn "  |%s|" dum
            dum <- ""
        n <- n + 1
    if (n % 16) > 0 then
        for i = 1 to (16 - (n % 16)) do
            printf "   "
        printf "  |%s" dum
        for i = 1 to (16 - (n % 16)) do
            printf " "
        printfn "|"
        dum <- ""

    fs.Close()
    printfn "\nRead %d bytes." n

    0

 

실행 예 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.

 

 

 

 

Posted by Scripter
,