'프로그래밍/R'에 해당되는 글 1건

  1. 2013.08.17 이진 파일을 읽어서 16진수로 보여주는 HexView 소스 with R

 

R 언어 소스:

# Filename: testHexView_02.r
#
#     Purpose: Show hexademial values of the given binary file.
#
#     Require: R 3.0.1 above
#
# Execute: r --silent --slave -q -f testHexView_02.r --args [filename]
#
# Date: 2013. 8. 17.


asc <- function(x) { strtoi(charToRaw(x),16L) }
chr <- function(n) { rawToChar(as.raw(n)) }


 

toHex = function(n) {
    s <- ""
    x1 <- bitwShiftR(as.integer(as.hexmode(n) & as.hexmode(0xF0)), 4)
    x2 <- as.integer(as.hexmode(n) & as.hexmode(0xF))
    if (x1 < 10) {
        s = sprintf("%s%s", s, chr(asc("0") + x1))
    } else {
        s = sprintf("%s%s", s, chr(asc("A") + (x1 - 10)))
    }
    if (x2 < 10) {
        s = sprintf("%s%s", s, chr(asc("0") + x2))
    } else {
        s = sprintf("%s%s", s, chr(asc("A") + (x2 - 10)))
    }
    s
}

toHex8 = function(n) {
    s <- ""
    s = sprintf("%s%s", s, "0")
    x2 <- bitwShiftR(as.integer(as.hexmode(n) & as.hexmode(0xF000000L)), 24)
    x3 <- bitwShiftR(as.integer(as.hexmode(n) & as.hexmode(0xF00000L)), 20)
    x4 <- bitwShiftR(as.integer(as.hexmode(n) & as.hexmode(0xF0000L)), 16)
    x5 <- bitwShiftR(as.integer(as.hexmode(n) & as.hexmode(0xF000L)), 12)
    x6 <- bitwShiftR(as.integer(as.hexmode(n) & as.hexmode(0xF00L)), 8)
    x7 <- bitwShiftR(as.integer(as.hexmode(n) & as.hexmode(0xF0L)), 4)
    x8 <- as.integer(as.hexmode(n) & as.hexmode(0xFL))
    if (x2 < 10) {
        s = sprintf("%s%s", s, chr(asc("0") + x2))
    } else {
        s = sprintf("%s%s", s, chr(asc("A") + (x2 - 10)))
    }
    if (x3 < 10) {
        s = sprintf("%s%s", s, chr(asc("0") + x3))
    } else {
        s = sprintf("%s%s", s, chr(asc("A") + (x3 - 10)))
    }
    if (x4 < 10) {
        s = sprintf("%s%s", s, chr(asc("0") + x4))
    } else {
        s = sprintf("%s%s", s, chr(asc("A") + (x4 - 10)))
    }
    s = sprintf("%s%s", s, " ")
    if (x5 < 10) {
        s = sprintf("%s%s", s, chr(asc("0") + x5))
    } else {
        s = sprintf("%s%s", s, chr(asc("A") + (x5 - 10)))
    }
    if (x6 < 10) {
        s = sprintf("%s%s", s, chr(asc("0") + x6))
    } else {
        s = sprintf("%s%s", s, chr(asc("A") + (x6 - 10)))
    }
    if (x7 < 10) {
        s = sprintf("%s%s", s, chr(asc("0") + x7))
    } else {
        s = sprintf("%s%s", s, chr(asc("A") + (x7 - 10)))
    }
    if (x8 < 10) {
        s = sprintf("%s%s", s, chr(asc("0") + x8))
    } else {
        s = sprintf("%s%s", s, chr(asc("A") + (x8 - 10)))
    }
    s
}


args <- commandArgs(trailingOnly = TRUE)

fname <- "c:\\test\\java7\\temp_1.bin"

if (length(args) > 0) {
    fname = args[1]
} else {
    cat("Usage: r --silent --slave -q -f testHexView_02.r --args [filename]\n")
    q()
}

if (file.exists(fname) == FALSE) {
    cat(sprintf("The file \"%s\" does not exist.\n", fname))
    q()
}

finfo <- file.info(fname)

if (finfo$isdir) {
    cat(sprintf("\"%s\" is a directory.\n", fname))
    q()
}

fsize <- finfo$size
cat(sprintf("The size of the file \"%s\" is %d.\n", fname, fsize))
cat("\n")

zz <- file(fname, "rb")
n = 0
dum = ""
while (n < fsize) {
    if ((n %% 16) == 0) {
        cat(toHex8(n))
        cat(": ")
    }
    c <- readBin(con=zz, what="raw", n=1)
    if ((n %% 16) == 8) {
        cat("-")
    } else {
        cat(" ")
    }
    cat(toHex(as.integer(c)))
    if (as.integer(c) < 0x20 | as.integer(c) > 0x7F) {
        dum = sprintf("%s.", dum)
    } else {
        dum = sprintf("%s%s", dum, chr(as.integer(c)))
    }

    if ((n %% 16) == 15) {
        cat("  |")
        cat(dum)
        cat("|\n")
        dum = ""
    }
    n = n + 1
}

close(zz)

if ((n %% 16) > 0) {
    for (i in 1:(16 - (n %% 16))) {
        cat("   ")
    }
    cat("  |")
    cat(dum)
    for (i in 1:(16 - (n %% 16))) {
        cat(" ")
    }
    cat("|\n")
    dum = ""
}

cat(sprintf("\nRead %d bytes.", n))
cat("\n")

 

 

실행 예 1> r --silent --slave -q -f testHexView_02.r --args 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> r --silent --slave -q -f testHexView_02.r --args 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
,