Skip to content

Instantly share code, notes, and snippets.

@edadma
Last active July 4, 2018 19:07
Show Gist options
  • Save edadma/699020e32ae9e2d52c455badb8d70c01 to your computer and use it in GitHub Desktop.
Save edadma/699020e32ae9e2d52c455badb8d70c01 to your computer and use it in GitHub Desktop.
binary file dump
def dump {
val cur = file.getFilePointer
val width = 16
file.seek( 0 )
def printByte( b: Int ) = print( "%02x ".format(b&0xFF).toUpperCase )
def printChar( c: Int ) = print( if (' ' <= c && c <= '~') c.asInstanceOf[Char] else '.' )
for (line <- 0L until file.length by width) {
print( "%10x ".format(line).toUpperCase )
val mark = file.getFilePointer
for (i <- line until ((line + width) min file.length)) {
if (i%16 == 8)
print( ' ' )
printByte( file.readByte )
}
val bytes = (file.getFilePointer - mark).asInstanceOf[Int]
print( " "*((width - bytes)*3 + 1 + (if (bytes < 9) 1 else 0)) )
file.seek( mark )
for (i <- line until ((line + width) min file.length))
printChar( file.readByte.asInstanceOf[Int] )
println
}
file.seek( cur )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment