Read Byte Array From URL and Convert To PDF in iOS - ios

I have a service in Windows that returns a PDF data, which it's in the database in a varbinary field. This service returns the PDF data as a Byte Array in JSON format
How can I read this data and convert it to PDF again, in my app in Xcode?
Which is the correct structure data to retrieve this byte array? NSArray? NSMutableArray?
I want to store this data, and I'm using a NSData structure...
Thanks

An NSArray would be for arrays of objects, you want an array of bytes, which an NSData is.
To get the data out of JSON, you would use the NSJSONSerialization class to extract the data from your JSON object -- the deserializer will probably return your literal data as an NSString, which you then would convert into an NSData by decoding it.
Presumably your raw data is in the JSON as Base64 or some such.

Related

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

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

Decoding an obfuscated value - Unable to convert data to string

I am working on importing a file from a legacy C++ codebase. The file itself is technically a JSON but one of the values is obfuscated with an XOR function in the C++ codebase before I receive it and looks something like this.
{"version": 15, "data": "C\u0016Q45\u0010 46QY\\\u0011\n\u0019a\u0003\u0019}\u001apg"}
The value for "data" is technically a UTF8 string.
I know the algorithm used to decode that data into a usable JSON string. However, I am unable to get swift to parse this into a [String : String] apparently due to formatting issues.
Error Domain=NSCocoaErrorDomain Code=3840 "Unable to convert data to string around character 31."
So far I have managed to get the specific data in question by isolating the subdata
let encryptedData = data.subdata(in: dataPrefixLength ..< dataLength - 2)
Even still, I cannot seem to get swift to parse this into a String when I attempt to do so with UTF8 encoding.
String(data: encryptedData, encoding: .utf8)
It's also worth noting that the String description seems alright in the debugger when inspecting the data itself.
I'd really appreciate any advice.
Thanks!
From your example it looks as if the obfuscation generates binary data and the binary data is put into a string. That's not allowed in JSON. Therefore, it's invalid JSON and any parser with a reasonable amount of validation will reject it.
As an example, take the start of the string:
"C\u0016
It starts with the character "C". That's valid.
Then an escaped character with a hexadecimal values follows: 0016. However, U+0016 is not a valid Unicode codepoint. Therefore it's rejected. It's probably supposed to be the binary byte 0x16. But you can't put that into a JSON string.
You have two options:
Fix the source of the data, e.g. by using a Base64 encoding before putting binary data into JSON.
Write your own JSON parser to handle the invalid JSON.

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.

How to store and retrieve multiple NSStrings in NSData

Is there any way I can store and retrieve multiple text strings in an NSData object?
I am looking into external accessories and just wondering how multiple arbitrary parameters are transmitted to and from the devices.
I assume that they will need to be decoded into NSString objects from an NSData object. So is there any way to distinguish between separate NSString objects from one stream of data?
Or, will multiple string parameters be stored in separate NSData objects? With a buffer for each?
Thanks
Use the NSKeyedArchiver to to archive an array of NSStrings.
+ (NSData *)archivedDataWithRootObject:(id)rootObject
NSKeyedArchiver Reference

how to decode image data with JSON format in Rails 3?

In Rails 3, I want to post the bitmap image data in JSON format to server, so I do the following steps.
1. In client, translate the bitmap image to string.
2. Encode the string in JSON format and post to the server.
3. Decode the bitmap image data of JSON format.
Now the problem is: In bitmap image, there are many 0 bytes or other unreadable bytes, after encoding in JSON format, 0 byte will be translated to /u0000, space byte will to /u000a.
In the server end, I use ActiveResource::Formats::JsonFormat.decode to decode the JSON string, but the method will stop when it meets /u0000, for example,
JSON string "\u0066\u0066\u0000\u0066\u0066" will be decoded to be "ff", and the rest three bytes will be discarded silently.
So how to resolve this problem? should I write a function to decode the JSON string myself?
You should really be POSTing that data as binary in a multipart form.
If you must encode it into a string, use base64.

Resources