Reputation: 14934
I'm developing an iOS app in Objective-C, which is using a UniMag card reader which can read Magnetic Stripe Reader. I'm using their SDK. When I scan a card, I receive a NSData object which is encoded in standard ASCII.
Unfortunately, I'm scanning a Danish Medical Card which seems to be encoded in different ASCII, possible ISO/IEC 646:1991 Norwegian/Danish
:
http://www.ascii.ca/iso646.no.htm
For instance, when the persons name on the Medical Card contains a Ø
, it is displayed as \
. This is because the encoding used on the card is using decimal 92 for Ø (see the above link), whereas the standard ASCII decimal 92 evaluates to the char \
. Because of this, I cannot read names if it contains special chars like Æ, Ø and Å.
My question is; how can I convert/encode the NSData object to the right format?
Upvotes: 0
Views: 1856
Reputation: 8292
Unfortunately, ISO 646 does not provide a single definition for each character. It represents 7-bit ASCII characters, and there are certain ranges in the character set that are defined using region-specific variants. See the ISO 6464 Wikipedia page for more information.
As a result, there is probably no specific way to handle this generically so that it will work correctly in any region. I'm not familiar with the card standards, but it would be interesting to know if there are specific bytes in the card's data that tell you which variant to apply. In other words, is there some bit of information you can read that will tell you whether to apply English, German, Danish, or some other variant?
Once you know which variant to apply, your best bet is probably to look for C libraries that support converting this data into ISO 8859-1. Once you have the data in an ISO-8859-1 (or similar) format you can convert it to an NSString using kCFStringEncodingISOLatin1.
I'm not sure if there are any ISO-646 to ISO-8859-1 conversion libraries available on iOS. The most common C libraries I've seen out there for this kind of thing is the libiconv GNU library, but I don't think there's a specific conversion for your case supported on iOS. If it comes down to it, it shouldn't be too hard to manually convert bytes using the conversion tables shown on on this man page. As you can see, there's only a small number of characters that need to be converted for any given variant.
Upvotes: 1
Reputation: 2200
Try this:
[NSString stringWithData:data encoding:NSUTF8StringEncoding]
Upvotes: 0