1

I'm new to Swift and I'm not sure how I can pick up a file and store it in a binary array.

I do know how to pick up a file, but I don't know how I can store it in a binary array which would be modified later.

Suppose the variable "chosenFile" is the file I pick up ( in NSData type) And the variable "bArray" ( [int8] array) is the array used to store the binary representation of the file.

var bArray: [Int8] = [Int8]()
var chosenFile: NSData! = NSData(contentsOfURL: "xxxxxxxx")

Any help ?

AstroCB
  • 12,337
  • 20
  • 57
  • 73
Yoope
  • 1,745
  • 2
  • 16
  • 22
  • `NSMutableData` would probably be a better place to start than using a `[Int8]`. If you do want to use primitives (for some reason), surely you want `[UInt8]`? – sapi Jan 04 '15 at 23:52
  • Well... What i wanna do is to get the binary representation of a file (lets say an image) and i will modify some of its data to create a new file (image). So in this case should i use UInt8 instead of Int8? Thank you~ – Yoope Jan 05 '15 at 00:42
  • By the way, if altering an image, simply getting `NSData` representation of the PNG or JPEG is generally not very useful. You usually want to get a `CGDataProvider` of the `CGImage`, which is a pixel buffer that is more easily manipulated than trying to parse JPEG/PNG files yourself. See [QA1509](https://developer.apple.com/library/mac/qa/qa1509/_index.html) for an example. – Rob Jan 05 '15 at 02:18

1 Answers1

0

If you create an instance of NSMutableData, you can use the mutableBytes property to get a reference to the underlying data. In C, this would be a void *; in Swift, it is an UnsafeMutablePointer<Void>.

You can then work with this data directly (there's no need for another byte array). So long as you stay within the length of the NSMutableData, you can, eg, replace individual bits of data, or indeed the entirety of the data.

Alternatively, rather than working on the primitive directly, you can use replaceBytesInRange:withBytes: to selectively modify part of your data. This is also explained in the documentation.

sapi
  • 9,944
  • 8
  • 41
  • 71
  • Thanks~ can i use .getBytes function instead of .mutableBytes? Im now trying to pass this byte array (or mutable pointer) to a C function and then do the modification... Or can i get the byte array from that pointer? – Yoope Jan 05 '15 at 01:26
  • @Yoope If the C function is going to mutate the underlying data, you'll want to use `mutableBytes`. You should be able to pass in the resulting `void *` directly to a C function which expects an array (although make sure it knows about the correct size, so that it doesn't try to read/write out of bounds). – sapi Jan 05 '15 at 01:47
  • Oh actually the data (raw) would be sent to a server (http) which will process it and send back the result. Can i print the binary data out using this pointer because it now gives me just null if i try to print the raw data out... – Yoope Jan 05 '15 at 01:53
  • If you're looking to send it online you probably want [to encode it into a string](http://stackoverflow.com/questions/6428461/convert-nsdata-to-string), but that's straying into a different question. You can do whatever you like with the underlying data (although attempting to print it doesn't make a great deal of sense: print it as *what*?) – sapi Jan 05 '15 at 02:00
  • I dont need to encode it since the server will deal with the raw data (binary)... Oh it indeed doesnt make sense to print the raw data out but i just wanna check if it is what i need... I guess i would expect it to print out lots of numbers? – Yoope Jan 05 '15 at 02:11