This repository has been archived by the owner on Sep 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 62
/
NSString-UPCEAN.m
executable file
·48 lines (43 loc) · 2.06 KB
/
NSString-UPCEAN.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// -----------------------------------------------------------------------------------
// NSString-UPCEAN.m
// -----------------------------------------------------------------------------------
// Created by Jeff LaMarche on Thu May 30 2002.
// �2002 Naked Software. All rights reserved.
// -----------------------------------------------------------------------------------
// THIS SOURCE CODE IS PROVIDED AS-IS WITH NO WARRANTY OF ANY KIND
// -----------------------------------------------------------------------------------
// You may use and redistribute this source code without limitation
// -----------------------------------------------------------------------------------
#import "NSString-UPCEAN.h"
@implementation NSString (UPCEAN)
// -----------------------------------------------------------------------------------
-(NSString *)swapHandedness
// -----------------------------------------------------------------------------------
{
int i;
NSMutableString *ret = [NSMutableString stringWithString:@""];
char *code = (char *) [self cStringUsingEncoding:NSStringEncodingConversionAllowLossy];
for (i = 0; i < strlen(code); i++)
{
if (code[i] == '0')
[ret appendString:@"1"];
else
[ret appendString:@"0"];
}
return ret;
}
// -----------------------------------------------------------------------------------
-(NSString *)swapParity
// -----------------------------------------------------------------------------------
{
/*
To arrive at the even encoding, work from the left encoding and do the following: 1) Change all the 1's to 0's and 0's to 1. 2) Read the resulting encoding in reverse order (from right to left). The result is the "left-hand even" encoding pattern.
*/
char *tmp = (char *)[[self swapHandedness] cStringUsingEncoding:NSStringEncodingConversionAllowLossy];
NSMutableString *ret = [NSMutableString stringWithString:@""];
int i;
for (i = strlen(tmp)-1; i >= 0; i--)
[ret appendString:[NSString stringWithFormat:@"%c", tmp[i]]];
return ret;
}
@end