Skip to content

Instantly share code, notes, and snippets.

@davedelong
Created September 23, 2011 05:07
Show Gist options
  • Save davedelong/1236778 to your computer and use it in GitHub Desktop.
Save davedelong/1236778 to your computer and use it in GitHub Desktop.
Snippet to discover the BOM of a text encoding
NSData *bom = nil;
NSStringEncoding encoding = ...;
NSData *a = [@"a" dataUsingEncoding:encoding];
NSData *aa = [@"aa" dataUsingEncoding:encoding];
if ([a length] * 2 != [aa length]) {
NSUInteger characterLength = [aa length] - [a length];
bom = [a subdataWithRange:NSMakeRange(0, [a length]-characterLength)];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment