1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

swift ,バイナリファイルを読み込み,Data型を[UInt8]や[Int16]に変換

1
Posted at

Data型->[UInt8]

    func ReadBinaryFile_Int8(path : String) -> [UInt8]{
        let dataURL = URL(fileURLWithPath: path)
        var binaryData = Data();
        do {
            // ファイル読み込み
            binaryData = try Data(contentsOf: dataURL, options: [])

        } catch {
            print("Failed to read the file.")
        }
        //return binaryData.encodedHexadecimals!;
        return [UInt8](binaryData)
    }

Data型->[Int16]

    func ReadBinaryFile_Int16(path : String) -> [Int16]{
        let dataURL = URL(fileURLWithPath: path)
        var binaryData = Data();
        do {
            binaryData = try Data(contentsOf: dataURL, options: [])
           
        } catch {
            print("Failed to read the file.")
        }
        
        let i16array = binaryData.withUnsafeBytes{UnsafeBufferPointer<Int16>(start: $0, count: binaryData.count/2).map(Int16.init(bigEndian:))}
        
        return i16array;
    }
1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?