Skip to content

Instantly share code, notes, and snippets.

@nolili
Created August 23, 2014 23:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nolili/2bf1a701df1015ed6488 to your computer and use it in GitHub Desktop.
Save nolili/2bf1a701df1015ed6488 to your computer and use it in GitHub Desktop.
SwiftのArray(バイト列)からNSDataへ変換
import UIKit
var str = "Hello, playground"
// Swiftの配列からNSDataへ変換
var rawArray:[UInt8] = [0x01, 0x02, 0x03];
let data = NSData(bytes: &rawArray, length: rawArray.count)
print(data)
// バイト列からNSDataに変換
// 0x00, 0x00, 0x00 のバッファを作成
var buffer = [UInt8](count: 3, repeatedValue: 0x00)
data.getBytes(&buffer, length: 3)
print(buffer)
@russellwang710017
Copy link

Thanks a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment