Skip to content

Instantly share code, notes, and snippets.

@RaidAndFade
Last active July 18, 2017 04:53
Show Gist options
  • Save RaidAndFade/35e6269894a4c570fa7e5217501a8780 to your computer and use it in GitHub Desktop.
Save RaidAndFade/35e6269894a4c570fa7e5217501a8780 to your computer and use it in GitHub Desktop.
Swift memdump. I was bored and wanted to learn swift syntax
import Foundation
let bytes = 16
let rows = 8
let sep = 4
let bytesPointer = UnsafeMutableRawPointer.allocate(bytes: 1, alignedTo: 1)
bytesPointer.storeBytes(of: 0xff, as: UInt8.self)
var c = 0
var a = 0
print(" ", terminator: "")
for x in 0 ..< bytes {
var str = String(format:"%X", x)
str = str.padding(toLength:2,withPad:" ",startingAt:0)
if (x % sep)==0 {print(" ", terminator: "")}
print(str, terminator: "")
}
print(" ", terminator: "")
for x in 0 ..< bytes {
var str = String(format:"%X", x)
if (x % sep)==0 {print(" ", terminator: "")}
print(str, terminator: "")
}
print()
for x in stride(from:0,to:bytes*(rows),by:bytes){
let offsetptr = bytesPointer+(x)
print("\(offsetptr) ", terminator: "")
for y in 0 ..< bytes {
let yptr = offsetptr+y
let v = yptr.load(as:UInt8.self)
let str = String(format:"%02X", v)
if (y % sep)==0 {print(" ", terminator: "")}
print(str, terminator: "")
}
print(" ", terminator: "")
for y in 0 ..< bytes {
let yptr = offsetptr+y
let v = yptr.load(as:UInt8.self)
var str = String(UnicodeScalar(v))
if let range = str.range(of:"\\p{L}",options: [.regularExpression]){
str = str.substring(with:range)
}
if str.range(of:"\\p{L}",options: [.regularExpression]) == nil || v<32 || v==127 {
str = "."
}
if (y % sep)==0 {print(" ", terminator: "")}
print(str, terminator: "")
}
print()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment