What is different between Data from String and Image in Swift? - ios

In Swift, We can convert String to Data, and convert from UIImage to Data also. So, what is different between two Data? Can Anyone help. Thanks.

To quote Apple's documentation:
The Data value type allows simple byte buffers to take on the behavior of Foundation objects.
Essentially it provides a byte buffer representation of Foundation objects, which allows you to access/manipulate the object's bytes in memory.
You can read more about the Data structure on Apple's documentation: https://developer.apple.com/documentation/foundation/data

Related

Extracting binary data from QR-code with zbar

I'm trying to extract binary data from QR-code with zbar (the QR-code was originally encoded using the iOS SDK passing a NSData object). Unfortunately the ZBarSymbol class only provides the content in a NSString member. Trying to extract a NSData from it using NSISOLatin1StringEncoding seems to work but still fails in some occasions.
I see in the zbar implementation that it is possible to access an object of type zbar_symbol_t that contains a pointer to char. By looking into it, it seems to contain the original content but with additional data of some kind, this is an example:
Original data: 9e7328c16bca3aaff532440917e4df6e155b96bd
Data in zbar_symbol_t: c29e7328c3816bc38a3ac2afc3b532440917c3a4c39f6e155bc296c2bd
Anyone who knows what is exactly that data in zbar_symbol_t, why it is different from data I originally placed in the QR-code and how it is possible, if possible at all, to extract my original data from that?
I am not sure what those bytes represent, probably zbar is trying to interprete the bytes as a UTF-8 string even though the QR is in byte mode.
Switching to zxing fixed everything, there is no interleaved unexpected byte and the raw data contains the entire QR code including the mode, terminator, padding etc... Also it seems to never fails, while zbar seemed to fail sometime.

iOS:convert hex data to integer format

i am doing corebluetooth application and able to receive the data from the peripheral in the hex format i need to parse the data to integer format and is there any method to convert the data to integer format from hex format please help Below values iam getting
NSstring *data is
011f6d000000160000000000040507010500054607db051705173a0600053d
Nslog(#"data is Ð`#PpPT`}°QpQs")
And also please help if there is any method for converting the data to the string also.
Define a struct with all the correct types and order. Point the struct to the data. Then just access the items via the struct elements. Be careful with the types, it is probably best to use well defined types such as int8_t, uint8_t, uint16_t, int16_t, etc to insure the correct element size. You will want a "packed" struct, not alligned to the CPU alignment size.

best way to serialize native C array

I'm currently using NSCoding to serialize a tree of objects, but 1 of them contains as data member a native C float array with 1,000,000 entries, so in order to serialize it using encodeFloat:forKey: for each array entry, I need to apply 1,000,000 useless keys , that might be very slow. what the prefered way to handle this?
for each array entry, I need to apply 1,000,000 useless keys
No, you definitely do not need separate keys for each element. A C array is a contiguous block of memory, so you can simply create a NSData object from that block and store that as Hot Licks suggested. Or, since a million floats will require a fair bit of storage, you might compress the data before storing it. And in fact, you don't really even need NSData -- you can encode a range of bytes directly with -encodeBytes:length:forKey:.

Parse Google Protocol Buffers datagram without .proto file?

is it possible to parse an incoming google protocol buffers datagram without any .proto file? I merely now its been serialized using protocol buffers but have no idea about the IDL file.
I'm looking for a way to just iterate through any value by some sort of reflection? Is this possible?
Thank you!
protoc --decode_raw < my_file
You need to take the following things into account when inspecting the output:
None of the field names are visible, just the tag numbers.
All varint-fields are shown as integers. This is ok for most types, but sint* will appear in the "zigzagged" format.
Doubles and floats will be shown as hex.
Bytes, string fields and submessages all appear the same, i.e. just a bunch of bytes.
If you want to decode the messages programmatically, you can write your own .proto file after you have figured out what the fields mean using the above method.

Best practice to store un-mutable data on iOS

I am looking to create a navigation based reference app for the iOS.
I have considered the following ways to store the data:
hard coding
plist file
some kind of comma delimited file
The data structure that I will be using has a bunch of strings, an array, and a reference to a picture.
What do you think the best way to store this data is without getting into CoreData?
Thanks
Also I dont think it would be more than 500 entries.
Well, this is not a "best-practice" problem for any case.
The data structure that I will be using has a bunch of strings, an array, and a reference to a picture.
What do you think the best way to store this data is without getting into CoreData?
For your needs, I suggest you look into NSKeyedArchiver.
NSString, NSArray, and UIImage all know how to encode and decode themselves. Just use an NSKeyedArchiver. Note that the objects in your collections (e.g. NSArray) must adopt #protocol NSCoding.
If you need to open this on a mac, then convert the UIImage to NSData using a proper image file format representation (e.g. PNG or JPEG) because UIImage is not available.
In detail:
hard coding
That could mean a number of things.
plist file
You're working with large non-plist types. That would mean you would need to convert to and from UIImage<->NSData unnecessarily, which would add a lot of overhead -- memory, CPU, and potentially file size. All these types can encode themselves better than (or as good as) a plist representation.
some kind of comma delimited file
Your image will not allow that to happen (reliably).

Resources