0

So I'm saving the NSString encoding enum value in a database as a string. This is one of the values:

 NSUTF16LittleEndianStringEncoding = 0x94000100,

And I'm saving it as a string with value 0x94000100. My goal is to read this value as an NSString, but then turn it into a value I can use to encode:

        NSString* encodingValueAsString = [object objectForKey:kWSWordlistFilesEncodingKey];

        // Convert 'encodingValueAsString' to variable 'encoding' that can act as enum

        NSString* dataString = [[NSString alloc] initWithData:data
                                                             encoding:encoding];

How do I write that missing line?

0

1 Answer 1

1

There's no reason to convert the enum value to/from a string. Just save it and read it back as an int.

But the short answer to your original question is:

NSStringEncoding encoding = [encodingValueAsString intValue];

Wait - that won't work since you are saving the string as the hex value.

You need to use NSScanner:

NSScanner *scanner = [NSScanner scannerWithString:encodingValueAsString];
NSStringEncoding encoding;
[scanner scanHexInt:&encoding];

See why using int instead of a string would be easier?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.