帮助码友解决问题,从而复习了一下oc中调用c函数的方式
1,新建c 头文件 test.h
定义 c 函数
#ifndef test_h
#define test_h
void verification(unsigned char INPUT[], unsigned int OUTPUT[]);
#endif /* test_h */
2,新建 c 实现文件,新建模板选中 c File test.c
3.实现函数
// // test.c #include <stdio.h> #include "test.h" #define N 10 void verification(unsigned char INPUT[], unsigned int OUTPUT[]) { //第一步 unsigned char *A = INPUT; //第二步 unsigned int S[N] = {0}; for(int i = 0; i < N; ++i) { S[i] = A[i]*A[i]; } //第三步 unsigned int P[N] = {0}; for(int i = 0; i < N; ++i) { P[i] = S[i] * (6+i); } //第四步 int E = 0; for(int i = 0; i <= 6; ++i) { E += P[i]; } OUTPUT[0] = (unsigned int)E&0XFFFF; OUTPUT[1] = P[7]&0XFFFF; OUTPUT[2] = P[8]&0XFFFF; OUTPUT[3] = P[9]&0XFFFF; }
4,oc 中调用,引用 c 头文件 test.h
#import "ViewController.h" #import "test.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; [self verificationWithNumber:@"M201476052"]; } -(NSString *)verificationWithNumber:(NSString *)Number{ unsigned char css[10]; memcpy(css, [Number cStringUsingEncoding:NSASCIIStringEncoding], [Number length]); unsigned int OUTPUT[4]; verification(css,OUTPUT); NSMutableArray *ary = [NSMutableArray array]; for(int i = 0; i< 4; ++i) { [ary addObject:[NSString stringWithFormat:@"%04X", OUTPUT[i]]]; } NSString *str = [ary componentsJoinedByString:@"-"]; NSLog(@"str = %@", str); return str; } @end