Scala 언어 소스:

/*
 * Filename: testHexView_03.scala
 *
 *    Purpose: Show the hexadecimal values of the given file.
 *
 * Compile: scalac -deprecation -d . testHexView_03.scala
 * Execute: scala testHexView_03 [filename]
 *
 * Date: 2013. 8. 18.
 */

import java.io._

object testHexView_03 {

    def printUsage() {
        println("Usage: scala testHexView_03 [filename]")
    }

    def toHex(n: Int) : String = {
        var s = ""
        var x1 = (n & 0xF0) >> 4
        var x2 = n & 0xF
        if (x1 < 10) {
            s = s + "%c".format(('0' + x1) toChar)
        }
        else {
            s = s + "%c".format('A' + (x1 - 10))
        }
        if (x2 < 10) {
            s = "%s%c".format(s, ('0' + x2) toChar)
        }
        else {
            s = s + "%c".format('A' + (x2 - 10))
        }
        return s
    }

    def toHex8(n: Int) : String = {
        var s = ""
        var x1 = (n & 0xF0000000) >> 28
        var x2 = (n & 0xF000000) >> 24
        var x3 = (n & 0xF00000) >> 20
        var x4 = (n & 0xF0000) >> 16
        var x5 = (n & 0xF000) >> 12
        var x6 = (n & 0xF00) >> 8
        var x7 = (n & 0xF0) >> 4
        var x8 = n & 0xF
        if (x1 < 10) {
            s = s + "%c".format(('0' + x1) toChar)
        }
        else {
            s = s + "%c".format('A' + (x1 - 10))
        }
        if (x2 < 10) {
            s = "%s%c".format(s, ('0' + x2) toChar)
        }
        else {
            s = s + "%c".format('A' + (x2 - 10))
        }
        if (x3 < 10) {
            s = "%s%c".format(s, ('0' + x3) toChar)
        }
        else {
            s = s + "%c".format('A' + (x3 - 10))
        }
        if (x4 < 10) {
            s = "%s%c".format(s, ('0' + x4) toChar)
        }
        else {
            s = s + "%c".format('A' + (x4 - 10))
        }
        s = s + " "
        if (x5 < 10) {
            s = "%s%c".format(s, ('0' + x5) toChar)
        }
        else {
            s = s + "%c".format('A' + (x5 - 10))
        }
        if (x6 < 10) {
            s = "%s%c".format(s, ('0' + x6) toChar)
        }
        else {
            s = s + "%c".format('A' + (x6 - 10))
        }
        if (x7 < 10) {
            s = "%s%c".format(s, ('0' + x7) toChar)
        }
        else {
            s = s + "%c".format('A' + (x7 - 10))
        }
        if (x8 < 10) {
            s = "%s%c".format(s, ('0' + x8) toChar)
        }
        else {
            s = s + "%c".format('A' + (x8 - 10))
        }
        return s
    }


    def main(args: Array[String]) {
            if (args.length < 1) {
                printUsage()
                System.exit(1)
            }

            var fname : String = args(0)

            var infile : File = new File(fname);
            if  (!infile.exists()) {
                 System.out.println("The file \"" + fname + "\" does not exist.");
                 System.exit(1);
            }

            if (!infile.canRead()) {
                 System.out.println("The file \"" + fname + "\" is not allowed to be read.");
                 System.exit(1);
            }   

            if  (infile.isDirectory()) {
                 System.out.println("\"" + fname + "\" is a directory.");
                 System.exit(1);
            }


            val is = new FileInputStream(fname)
            val cnt = is.available

            var lines = new Array[String](19)
            var b = new Array[Byte](1)

            printf("The size of the file \"%s\" is %d.\n", fname, cnt)
            printf("\n")

            var n = 0
            var dum = ""
            while (n < cnt) {
                if (n % 16 == 0) {
                    printf("%s: ", toHex8(n))
                }
                is.read(b)
                if (n % 16 == 8) {
                    printf("-%s", toHex(b(0) toInt))
                }
                else {
                    printf(" %s", toHex(b(0) toInt))
                }
                if ((b(0) toInt) < (' ' toInt) || (b(0) toInt) > 0x7F) {
                    dum = dum + "."
                }
                else {
                    dum = dum + "%c".format(b(0) toChar)
                }
                if (n % 16 == 15) {
                    printf("  |%s|\n", dum)
                    dum = ""
                }
                n += 1
            }
            if (n % 16 > 0) {
                for (j <- 1 to 16 - (n % 16)) {
                    printf("   ")
                }
                printf("  |%s", dum)
                for (j <- 1 to 16 - (n % 16)) {
                    printf(" ")
                }
                printf("|\n", dum)
                dum = ""
            }
            printf("\nRead %d bytes.\n", n)

            is.close()
    }
}

 

 

실행 예 1> scala testHexView_03 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> scala testHexView_03 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
,