Skip to content

Instantly share code, notes, and snippets.

@kristopherjohnson
Last active July 14, 2018 12:44
Show Gist options
  • Save kristopherjohnson/ed2623e1b486a8262b12 to your computer and use it in GitHub Desktop.
Save kristopherjohnson/ed2623e1b486a8262b12 to your computer and use it in GitHub Desktop.

Revisions

  1. kristopherjohnson revised this gist Oct 24, 2014. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions NSData_hexadecimalString.swift
    Original file line number Diff line number Diff line change
    @@ -9,11 +9,10 @@ extension NSData {
    getBytes(&bytes, length: length)

    let hexString = NSMutableString()
    for i in 0..<length {
    let byte = bytes[i]
    for byte in bytes {
    hexString.appendFormat("%02x", UInt(byte))
    }

    return NSString(string: hexString)
    }
    }
    }
  2. kristopherjohnson revised this gist Oct 23, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion NSData_hexadecimalString.swift
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@ extension NSData {
    var bytes = [UInt8](count: length, repeatedValue: 0)
    getBytes(&bytes, length: length)

    var hexString = NSMutableString()
    let hexString = NSMutableString()
    for i in 0..<length {
    let byte = bytes[i]
    hexString.appendFormat("%02x", UInt(byte))
  3. kristopherjohnson revised this gist Oct 23, 2014. 1 changed file with 4 additions and 8 deletions.
    12 changes: 4 additions & 8 deletions NSData_hexadecimalString.swift
    Original file line number Diff line number Diff line change
    @@ -5,19 +5,15 @@ extension NSData {
    /// Return hexadecimal string representation of NSData bytes
    @objc(kdj_hexadecimalString)
    public var hexadecimalString: NSString {
    let buffer = malloc(UInt(length))
    getBytes(buffer, length: length)
    var bytes = [UInt8](count: length, repeatedValue: 0)
    getBytes(&bytes, length: length)

    let byteArray = UnsafePointer<UInt8>(buffer)

    var hexString: NSMutableString = NSMutableString()
    var hexString = NSMutableString()
    for i in 0..<length {
    let byte: UInt8 = byteArray[i]
    let byte = bytes[i]
    hexString.appendFormat("%02x", UInt(byte))
    }

    free(buffer)

    return NSString(string: hexString)
    }
    }
  4. kristopherjohnson created this gist Oct 23, 2014.
    23 changes: 23 additions & 0 deletions NSData_hexadecimalString.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    import Foundation

    extension NSData {

    /// Return hexadecimal string representation of NSData bytes
    @objc(kdj_hexadecimalString)
    public var hexadecimalString: NSString {
    let buffer = malloc(UInt(length))
    getBytes(buffer, length: length)

    let byteArray = UnsafePointer<UInt8>(buffer)

    var hexString: NSMutableString = NSMutableString()
    for i in 0..<length {
    let byte: UInt8 = byteArray[i]
    hexString.appendFormat("%02x", UInt(byte))
    }

    free(buffer)

    return NSString(string: hexString)
    }
    }