oc中調用c函數 實現將字符串轉換成unsigned char


幫助碼友解決問題,從而復習了一下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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM