1

I am scanning some barcodes and decoding them to Swift strings. The specific scanner provides an object that holds the information I need to build an NSData:

let rawData = decodedData.getData() // UnsafeMutablePointer<UInt8>
let rawDataSize = decodedData.getDataSize() // UInt32
let data = NSData(bytes: rawData, length: Int(rawDataSize)) // NSData

I then decode this into a string:

let string = NSString(data: data, encoding: NSUTF8StringEncoding) as? String

I find that certain barcodes return nil when decoding unless I switch to NSASCIIStringEncoding:

let string = NSString(data: data, encoding: NSASCIIStringEncoding) as? String

My understanding of string encoding is limited, but I was under the impression that any ASCII string could be decoded as UTF8 since ASCII is a subset of UTF8. Is this accurate?

If so, what else might be causing this issue?

1 Answer 1

2

The problem is that not every sequence of bytes is valid if interpreted as UTF-8. For example, a single byte with a value of 0xff = 255 is never valid in UTF-8. On the other hand, it might be that the ASCII encoding allows every byte value, even though this is not really correct.

You better have a good look at the data and see what encoding it actually is. And if it is just random bytes, then please do NOT convert them to a string.

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

1 Comment

It's certainly a string (the decoded data is meaningful and recognizable). How might I go about determining the encoding? My current method has been trial and error - NSASCIIStringEncoding was the only encoding that correctly decoded all of my examples into the text I was anticipating.

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.