Create an NSData object from a String - string

My app requires the ability to take a String and save it to an NSDictionary as an NSData object, such that when the NSDictionary is written to a .plist file the resulting file contains the data as <data>The String as the user typed it (no encoding)</data>. Is there anyway to do that without manually writing the XML for the .plist file?
Thanks in advance for any help.

There's no way to do what you want.
First, it makes no sense to claim that you don't want the string encoded. Encoding is the process of producing a byte stream from a string. Without encoding, there's no representation of the string. Strings are abstract. They have no concrete representation in and of themselves. Only encoding produces that.
Anyway, that's just not how NSData objects are serialized to property list files. Even if you manually wrote the file, it wouldn't really be a correct property list file and no other property-list-reading program would be able to parse it. How did you come to conclude that your app requires this ability?

Related

How do `Map<String, String>` get to know, the `String` endings in `MethodChannel` arguments

If dart and kotlin code communicate through binary(array of 8-bit integers (0-255)), then how does String end or even int end is represented in, or determined from binary sequence of bytes, is there some special charCode or something else.
Also is there a way to save a List<int> as-it-is to a file.txt, so it can be read directly to List<int> instead of serialization.
Please guide this new dev,
Thanking you...
Since Flutter handles the MethodChannel, in both the Dart side and Kotlin side, it can be allowed to have its own internal protocol to communicate between the native layer and Flutter. In theory they could use JSON but they are probably using something else based on the supported types and also making it more efficient: https://docs.flutter.dev/development/platform-integration/platform-channels?tab=type-mappings-kotlin-tab#codec
For saving a List<int> to a file, you need to determine how you want to encode the content in the file and then how you want to decode it. It can be as simply as just saving each number separated by comma or encode the list into JSON.
If your list of numbers can be represented with Uint8List or Int8List, then you can basically just save the numbers as raw bytes to the file and then read them again.
But List<int> is a list of 64-bit numbers and you should therefore determine how you want to encode this exactly.
For writing to files, there are several ways to do it but the specific way depends on what you exactly want. So without any more details I can just suggest you check the API: https://api.dart.dev/stable/2.17.3/dart-io/File-class.html

Is it possible to save a Map directly to the disk with Node.js?

With the regular Object we are able to convert it into JSON string before saving it to the disk but the Map is not an entirely new thing and it cannot always be converted into string (answered here: How do you JSON.stringify an ES6 Map?).
Is it possible to save the Map to the disk as a binary format and read it into a Map later on with Node.js?
Is it possible to save the Map to the disk as a binary format and read it into a Map later on with Node.js?
There is no built-in facility for that. A Map is just an association between a set of keys and values. And, if you do
let summaryArray = Array.from(yourMapObject);
summaryArray will be something like this:
[['key1', value1], ['key2', value2]]
Then, assuming your Map object contains things (both keys and values) that can be expressed in JSON (not symbols and not objects with circular references and not custom object types), you can easily save that summaryArray to JSON.
Furthermore, you can regenerate the Map object by reading the JSON from disk, parsing it back into the summaryArray form and passing that to the Map constructor.
let yourMapObject = new Map(summaryArray);
If your Map object does contain things that can't be expressed in JSON, then you will have to invent your own way of storing those things or converting them to/from something that can be expressed in JSON.

base 64 Decode XML values using Groovy script

I will be receiving the following XML data in a variable.
<order>
<name>xyz</name>
<city>abc</city>
<string>aGVsbG8gd29ybGQgMQ==</string>
<string>aGVsbG8gd29ybGQgMg==</string>
<string>aGVsbG8gd29ybGQgMw==</string>
</order>
Output:
<order>
<name>xyz</name>
<city>abc</city>
<string>hello world 1</string>
<string>hello world 2</string>
<string>hello world 3</string>
</order>
I know how I can decode from base64 but the problem is some of the values are decoded already and some are encoded. What is the best approach to decode this data using groovy so that I get the output as shown?
Always: tag value will be encoded. rest all other tags and value will be decoded.
Since there's no uncertainty on which nodes could come encoded and which not, hence no need to detect base64 encoding, the way to do it is pretty simple:
Parse it. There's two preferable ways to do that in Groovy: XmlSlurper & XmlParser. They differ in computation & mem consumption modes, both provide object/structure representation in the end, though.
Work with that object structure: traverse all required elements, decode the content/attributes you need to decode.
Either proceed further with the data with them and/or serialize it back to the XML text.
Articles to look at:
Load, modify, and write an XML document in Groovy
https://www.baeldung.com/groovy-xml
https://groovy-lang.org/processing-xml.html
and many, many more.
Another cheat sheet always useful for Groovy noobs: http://groovy-lang.org/groovy-dev-kit.html
Check out how to traverse the structures there, for instance.

Md5-Hash-ByteArray is working in programm but not in external validator, (need to convert it to some kind of String)

I am creating a raw MD5-Bytearray of some file and store it in a DB. Then, when I upload the file I validate the Checksum. So far everything works fine.
Now I am trying to display the Checksum in the standard form like, for example, this:
0709bfccaec24cbb5734b905dda8d616
but all I got were some cryptic things, e.g.
[B#1fd3b78a
How do I best get the String?
So the representation I was looking for is appearantly the Hexadezimal representation of the bytes. I converted and saved it as a String:
How to convert a byte array to a hex string in Java?

Groovy says my Unicode string is too long

As part of my probably wrong and cumbersome solution to print out a form I have taken a MS-Word document, saved as XML and I'm trying to store that XML as a groovy string so that I can ${fillOutTheFormProgrammatically}
However, with MS-Word documents being as large as they are, the String is 113100 unicode characters and Groovy says its limited to 65536. Is there some way to change this or am I stuck with splitting up the string?
Groovy - need to make a printable form
That's what I'm trying to do.
Update: to be clear its too long of a Groovy String.. I think a regular string might be all good. Going to change strategy and put some strings in the file I can easily find like %!%variable_name%!% and then do .replace(... uh i feel a new question coming on here...
Are you embedding this string directly in your groovy code? The jvm itself has a limit on the length of string constants, see the VM Spec if you are interested in details.
A ugly workaround might be to split the string in smaller parts and concatenate them at runtime. A better solution would be to save the text in an external file and read the contents from your code. You could also package this file along with your code and access it from the classpath using Class#getResourceAsStream.

Resources