如何将文件读入Ints数组 [英] How to read file into array of Ints
问题描述
在我的应用程序文档文件夹中,我有一个文件,试图将每个字节读取到UInt8
数组中,其中每个元素代表一个字节.我将如何去做呢?该文件恰好叫做Q1.dat.
In my app documents folder I have a file which I am trying to read byte-per-byte into an array of UInt8
where each element represents a byte. How would I go about doing this? The file happens to be called Q1.dat.
这是我失败的尝试:
func readArray() -> [Int]? {
if let arrayPath: String = createArrayPath() {
if let arrayFromFile: [Int] = NSArray(contentsOfFile: arrayPath) as? [Int] {
return arrayFromFile
}
}
return nil
}
func createArrayPath () -> String? {
if let docsPath: String = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last {
return ((docsPath as NSString).stringByAppendingPathComponent("Q1") as NSString).stringByAppendingPathExtension("dat")
}
return nil
}
推荐答案
尝试如下操作:
let filePath = "/Path/To/Directory/Q1.dat"
var bytes = [UInt8]()
if let data = NSData(contentsOfFile: filePath) {
var buffer = [UInt8](count: data.length, repeatedValue: 0)
data.getBytes(&buffer, length: data.length)
bytes = buffer
}
对不起,我不在可以对其进行测试的地方,但是稍加修改就可以为您提供一个UInt8
数组,每个数组代表文件中的一个字节.
I'm sorry, I'm not in a place where I can test it, but with a little tinkering it should give you an array of UInt8
s, each of which represents a byte from the file.
而且,如果您真的希望这是Int
s而不是UInt8
s的数组,只需map
bytes
到Int
s的数组:
And if you really want this to be an array of Int
s instead of UInt8
s, just map
bytes
to an array of Int
s:
let intBytes = bytes.map { Int($0) }
...尽管通常来说,目前尚不清楚为什么您要使用除字节集合以外的任何东西,这些字节集合最适合表示为一系列UInt8
.
...although generally speaking it is not clear why you want to work with anything other than a collection of bytes, which are most appropriately represented as a series of UInt8
s.
如果您确实真的想要1和0,那么将其附加到末尾应该可以做到:
And if you really, really, want the ones and zeros, then tacking this on to the end should do it:
var onesAndZeros = [UInt8]()
bytes.forEach { byte in
var array = [UInt8](count: 8, repeatedValue: 0)
let bits = String(byte, radix: 2).characters.flatMap { UInt8(String($0)) }
array.replaceRange((8-bits.count)..<8, with: bits)
onesAndZeros.appendContentsOf(array)
}
再次,我选择了UInt8
而不是Int
,只是因为这样做更有意义,但是可以根据需要进行更改.
Again, I went with UInt8
instead of Int
, just because it makes more sense, but change it up as you need.
只需整理一下,您就可以将这两个操作包装在几个函数中,如下所示:
Just to neaten things up, you can wrap the two operations in a couple of functions like this:
func bytesFromFile(filePath: String) -> [UInt8]? {
guard let data = NSData(contentsOfFile: filePath) else { return nil }
var buffer = [UInt8](count: data.length, repeatedValue: 0)
data.getBytes(&buffer, length: data.length)
return buffer
}
func bitsFromBytes(bytes: [UInt8]) -> [UInt8] {
var onesAndZeros = [UInt8]()
bytes.forEach { byte in
var array = [UInt8](count: 8, repeatedValue: 0)
let bits = String(byte, radix: 2).characters.flatMap { UInt8(String($0)) }
array.replaceRange((8-bits.count)..<8, with: bits)
onesAndZeros.appendContentsOf(array)
}
return onesAndZeros
}
这篇关于如何将文件读入Ints数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!