2023-11-01 14:18:52 +08:00
|
|
|
|
//
|
|
|
|
|
|
// HttpManager.m
|
|
|
|
|
|
// myhome
|
|
|
|
|
|
//
|
|
|
|
|
|
// Created by hkh on 16/11/6.
|
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
#import "Pub.h"
|
|
|
|
|
|
#import <CommonCrypto/CommonDigest.h>
|
|
|
|
|
|
|
|
|
|
|
|
@interface Pub (){
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
@implementation Pub{
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
+ (AppDelegate *)getApp{
|
|
|
|
|
|
AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication]delegate];
|
|
|
|
|
|
return app;
|
|
|
|
|
|
}
|
|
|
|
|
|
+ (sysInfo *)getSysInfo{
|
|
|
|
|
|
AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication]delegate];
|
|
|
|
|
|
return app.sysinfo;
|
|
|
|
|
|
}
|
|
|
|
|
|
+ (BaseNavigationController*)getNav{
|
|
|
|
|
|
UITabBarController *tab = (UITabBarController *)[Pub getApp].window.rootViewController;
|
|
|
|
|
|
if(tab){
|
|
|
|
|
|
UINavigationController *nav = tab.viewControllers[tab.selectedIndex];
|
|
|
|
|
|
return nav;
|
|
|
|
|
|
}
|
|
|
|
|
|
return nil;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
+ (NSString *)MD5:(NSString *)input //大写
|
|
|
|
|
|
{
|
|
|
|
|
|
const char *cStr = [input UTF8String];
|
|
|
|
|
|
unsigned char digest[16];
|
|
|
|
|
|
CC_MD5( cStr, (CC_LONG)strlen(cStr), digest ); // This is the md5 call
|
|
|
|
|
|
|
|
|
|
|
|
NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
|
|
|
|
|
|
|
|
|
|
|
|
for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
|
|
|
|
|
|
[output appendFormat:@"%02X", digest[i]];
|
|
|
|
|
|
|
|
|
|
|
|
return output;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
+ (NSString *)md5_:(NSString *)input //大写
|
|
|
|
|
|
{
|
|
|
|
|
|
const char *cStr = [input UTF8String];
|
|
|
|
|
|
unsigned char digest[16];
|
|
|
|
|
|
CC_MD5( cStr, (CC_LONG)strlen(cStr), digest ); // This is the md5 call
|
|
|
|
|
|
|
|
|
|
|
|
NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
|
|
|
|
|
|
|
|
|
|
|
|
for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
|
|
|
|
|
|
[output appendFormat:@"%02x", digest[i]];
|
|
|
|
|
|
|
|
|
|
|
|
return output;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
+ (NSDictionary *)DictionaryWithJsonString:(NSString *)jsonString {
|
|
|
|
|
|
if (jsonString == nil) { return nil; }
|
|
|
|
|
|
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
|
|
|
|
|
|
NSError *err;
|
|
|
|
|
|
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err];
|
|
|
|
|
|
if(err) {
|
|
|
|
|
|
NSLog(@"json解析失败:%@",err);
|
|
|
|
|
|
return nil;
|
|
|
|
|
|
}
|
|
|
|
|
|
return dic;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
+ (void)addHead:(NSString *)head toByte:(Byte *)bb
|
|
|
|
|
|
{
|
|
|
|
|
|
Byte *tempbb;
|
|
|
|
|
|
NSString *tempS;
|
|
|
|
|
|
NSData *tempData;
|
|
|
|
|
|
tempS = @"XXXCID";
|
|
|
|
|
|
tempData = [tempS dataUsingEncoding:NSUTF8StringEncoding];
|
|
|
|
|
|
tempbb = (Byte *)[tempData bytes];
|
|
|
|
|
|
memcpy(bb, tempbb, 6);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
+ (void)addEquid:(NSString *)equId toByte:(Byte *)bb at:(int)pos
|
|
|
|
|
|
{
|
|
|
|
|
|
Byte *tempbb;
|
|
|
|
|
|
NSString *tempS;
|
|
|
|
|
|
NSData *tempData;
|
|
|
|
|
|
//本地
|
|
|
|
|
|
tempS = equId;
|
|
|
|
|
|
tempData = [tempS dataUsingEncoding:NSUTF8StringEncoding];
|
|
|
|
|
|
tempbb = (Byte *)[tempData bytes];
|
|
|
|
|
|
memcpy(bb+pos, tempbb, [tempData length]);
|
|
|
|
|
|
for (int i=(int)[tempData length]; i<20; i++){
|
|
|
|
|
|
bb[i+pos] = 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
+ (int)getBBlen:(Byte *)bb Len:(int)len{
|
|
|
|
|
|
for(int i=0; i<len; i++){
|
|
|
|
|
|
if (bb[i] == 0){
|
|
|
|
|
|
return i;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return len;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
+ (int)addGKB:(NSString *)equId toByte:(Byte *)bb at:(int)pos
|
|
|
|
|
|
{
|
|
|
|
|
|
Byte *tempbb;
|
|
|
|
|
|
NSString *tempS;
|
|
|
|
|
|
NSData *tempData;
|
|
|
|
|
|
//本地
|
|
|
|
|
|
|
|
|
|
|
|
NSStringEncoding gbkEncoding =CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
|
|
|
|
|
|
tempS = equId;
|
|
|
|
|
|
tempData = [tempS dataUsingEncoding:gbkEncoding];
|
|
|
|
|
|
tempbb = (Byte *)[tempData bytes];
|
|
|
|
|
|
memcpy(bb+pos, tempbb, [tempData length]);
|
|
|
|
|
|
return (int)[tempData length] ;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
+ (void)addIp:(NSString *)ip toByte:(Byte *)bb at:(int)pos
|
|
|
|
|
|
{
|
|
|
|
|
|
NSArray *tempA = [ip componentsSeparatedByString:@"."];
|
|
|
|
|
|
for (int i=0; i<tempA.count; i++){
|
|
|
|
|
|
bb[i+pos] = [(NSString *)[tempA objectAtIndex:i] intValue];
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
+ (void)addMac:(NSString *)mac toByte:(Byte *)bb at:(int)pos
|
|
|
|
|
|
{
|
|
|
|
|
|
NSArray *tempA = [mac componentsSeparatedByString:@":"];
|
|
|
|
|
|
for (int i=0; i<tempA.count; i++){
|
|
|
|
|
|
bb[i+pos] = [self Ox:(NSString *)[tempA objectAtIndex:i]];
|
|
|
|
|
|
bb[i+pos] = [self Ox:(NSString *)[tempA objectAtIndex:i]];
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
+ (void)addInt:(int)port toByte:(Byte *)bb at:(int)pos
|
|
|
|
|
|
{
|
|
|
|
|
|
bb[pos+3] = (Byte)((port & 0xFF000000) >> 24);
|
|
|
|
|
|
bb[pos+2] = (Byte)((port & 0x00FF0000) >> 16);
|
|
|
|
|
|
bb[pos+1] = (Byte)((port & 0x0000FF00) >> 8);
|
|
|
|
|
|
bb[pos] = (Byte)((port & 0x000000FF) );
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
+ (void)addShort:(int)port toByte:(Byte *)bb at:(int)pos
|
|
|
|
|
|
{
|
|
|
|
|
|
bb[pos+1] = (Byte)((port & 0x0000FF00) >> 8);
|
|
|
|
|
|
bb[pos] = (Byte)((port & 0x000000FF) );
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
+ (short)getShortFromByte:(Byte *)bb at:(int)pos
|
|
|
|
|
|
{
|
|
|
|
|
|
return bb[pos] + bb[pos+1]*256;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
+ (int)Ox:(NSString *)v
|
|
|
|
|
|
{
|
|
|
|
|
|
int n = 0;
|
|
|
|
|
|
int p = 0;
|
|
|
|
|
|
NSString *s;
|
|
|
|
|
|
for (int i=0; i<v.length; i++){
|
|
|
|
|
|
s = [v substringWithRange:NSMakeRange(i, 1)];
|
|
|
|
|
|
if ([s isEqualToString:@"A"]|[s isEqualToString:@"a"]) p=10;
|
|
|
|
|
|
else if ([s isEqualToString:@"B"]|[s isEqualToString:@"b"]) p=11;
|
|
|
|
|
|
else if ([s isEqualToString:@"C"]|[s isEqualToString:@"c"]) p=12;
|
|
|
|
|
|
else if ([s isEqualToString:@"D"]|[s isEqualToString:@"d"]) p=13;
|
|
|
|
|
|
else if ([s isEqualToString:@"E"]|[s isEqualToString:@"e"]) p=14;
|
|
|
|
|
|
else if ([s isEqualToString:@"F"]|[s isEqualToString:@"f"]) p=15;
|
|
|
|
|
|
else p = [s intValue];
|
|
|
|
|
|
for (int j=i+1; j<v.length; j++){
|
|
|
|
|
|
p = p*16;
|
|
|
|
|
|
}
|
|
|
|
|
|
n += p;
|
|
|
|
|
|
}
|
|
|
|
|
|
return n;
|
|
|
|
|
|
}
|
|
|
|
|
|
+ (NSString *)getLocalIP
|
|
|
|
|
|
{
|
|
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{//转到主线程处理
|
|
|
|
|
|
NSString *address = @"error";
|
|
|
|
|
|
struct ifaddrs *interfaces = NULL;
|
|
|
|
|
|
struct ifaddrs *temp_addr = NULL;
|
|
|
|
|
|
int success = 0;
|
|
|
|
|
|
// retrieve the current interfaces - returns 0 on success
|
|
|
|
|
|
success = getifaddrs(&interfaces);
|
|
|
|
|
|
if (success == 0) {
|
|
|
|
|
|
// Loop through linked list of interfaces
|
|
|
|
|
|
temp_addr = interfaces;
|
|
|
|
|
|
while(temp_addr != NULL) {
|
|
|
|
|
|
if(temp_addr->ifa_addr->sa_family == AF_INET) {
|
|
|
|
|
|
// Check if interface is en0 which is the wifi connection on the iPhone
|
|
|
|
|
|
if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
|
|
|
|
|
|
// Get NSString from C String
|
|
|
|
|
|
address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
temp_addr = temp_addr->ifa_next;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
// Free memory
|
|
|
|
|
|
freeifaddrs(interfaces);
|
|
|
|
|
|
[Pub getApp].sysinfo.equip = address;
|
|
|
|
|
|
});
|
|
|
|
|
|
return [Pub getApp].sysinfo.equip;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//针对ipv6网络环境下适配,ipv4环境直接使用原来的地址
|
|
|
|
|
|
|
|
|
|
|
|
+ (NSString *)getMac
|
|
|
|
|
|
{
|
|
|
|
|
|
int mib[6];
|
|
|
|
|
|
size_t len;
|
|
|
|
|
|
char *buf;
|
|
|
|
|
|
unsigned char *ptr;
|
|
|
|
|
|
struct if_msghdr *ifm;
|
|
|
|
|
|
struct sockaddr_dl *sdl;
|
|
|
|
|
|
|
|
|
|
|
|
mib[0] = CTL_NET;
|
|
|
|
|
|
mib[1] = AF_ROUTE;
|
|
|
|
|
|
mib[2] = 0;
|
|
|
|
|
|
mib[3] = AF_LINK;
|
|
|
|
|
|
mib[4] = NET_RT_IFLIST;
|
|
|
|
|
|
|
|
|
|
|
|
if ((mib[5] = if_nametoindex("en0")) == 0) {
|
|
|
|
|
|
printf("Error: if_nametoindex error/n");
|
|
|
|
|
|
return NULL;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
|
|
|
|
|
|
printf("Error: sysctl, take 1/n");
|
|
|
|
|
|
return NULL;
|
|
|
|
|
|
}
|
|
|
|
|
|
buf = malloc(len);
|
|
|
|
|
|
if (buf == NULL) {
|
|
|
|
|
|
printf("Could not allocate memory. error!/n");
|
|
|
|
|
|
return NULL;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (sysctl(mib, 6, buf, &len, NULL, 0) < 0) {
|
|
|
|
|
|
//printf("Error: sysctl, take 2");
|
|
|
|
|
|
free(buf);
|
|
|
|
|
|
return NULL;
|
|
|
|
|
|
}
|
|
|
|
|
|
ifm = (struct if_msghdr *)buf;
|
|
|
|
|
|
sdl = (struct sockaddr_dl *)(ifm + 1);
|
|
|
|
|
|
ptr = (unsigned char *)LLADDR(sdl);
|
|
|
|
|
|
NSString *outstring = [NSString stringWithFormat:@"%02x:%02x:%02x:%02x:%02x:%02x", *ptr, *(ptr+1), *(ptr+2), *(ptr+3), *(ptr+4), *(ptr+5)];
|
|
|
|
|
|
free(buf);
|
|
|
|
|
|
return [outstring uppercaseString];
|
|
|
|
|
|
}
|
|
|
|
|
|
+ (NSString *)getEquidFrombb:(Byte *)bb at:(int)pos {
|
|
|
|
|
|
NSString *equid = nil;
|
|
|
|
|
|
int equlen=8;
|
|
|
|
|
|
if (bb[pos]==77) {//M
|
|
|
|
|
|
equlen=8;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if(bb[pos]==87) {//W
|
|
|
|
|
|
equlen=5;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if(bb[pos]==72){//H
|
|
|
|
|
|
equlen=12;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if(bb[pos]==83){//S
|
|
|
|
|
|
equlen=12;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if(bb[pos]==0x50){//P
|
|
|
|
|
|
equlen=12;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if(bb[pos]==0x54){//T
|
|
|
|
|
|
equlen=16;
|
|
|
|
|
|
}
|
|
|
|
|
|
else{
|
|
|
|
|
|
equlen=12;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Byte tempbb[equlen];
|
|
|
|
|
|
memcpy(tempbb, bb+pos, equlen);
|
|
|
|
|
|
NSData *aadata = [[NSData alloc] initWithBytes:tempbb length:equlen];
|
|
|
|
|
|
NSStringEncoding gbkEncoding =CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
|
|
|
|
|
|
equid = [[NSString alloc] initWithData:aadata encoding:gbkEncoding];
|
|
|
|
|
|
return equid;
|
|
|
|
|
|
}
|
|
|
|
|
|
+ (NSString *)NSdata2String:(NSData *)data{
|
|
|
|
|
|
NSStringEncoding gbkEncoding =CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
|
|
|
|
|
|
return [[NSString alloc] initWithData:data encoding:gbkEncoding];
|
|
|
|
|
|
}
|
|
|
|
|
|
+ (NSData *)AES128_Encrypt:(NSString *)key encryptData:(NSData *)data{
|
|
|
|
|
|
|
|
|
|
|
|
char keyPtr[kCCKeySizeAES128+1];
|
|
|
|
|
|
bzero(keyPtr, sizeof(keyPtr));
|
|
|
|
|
|
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
|
|
|
|
|
|
|
|
|
|
|
|
//无向量
|
|
|
|
|
|
// char ivPtr[kCCKeySizeAES128+1];
|
|
|
|
|
|
// memset(ivPtr, 0, sizeof(ivPtr));
|
|
|
|
|
|
// [gIv getCString:ivPtr maxLength:sizeof(ivPtr) encoding:NSUTF8StringEncoding];
|
|
|
|
|
|
|
|
|
|
|
|
NSUInteger dataLength = [data length];
|
|
|
|
|
|
size_t bufferSize = dataLength + kCCBlockSizeAES128;
|
|
|
|
|
|
void *buffer = malloc(bufferSize);
|
|
|
|
|
|
size_t numBytesEncrypted = 0;
|
|
|
|
|
|
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,
|
|
|
|
|
|
kCCAlgorithmAES128,
|
|
|
|
|
|
kCCOptionPKCS7Padding,
|
|
|
|
|
|
keyPtr,
|
|
|
|
|
|
kCCBlockSizeAES128,
|
|
|
|
|
|
nil,
|
|
|
|
|
|
[data bytes],
|
|
|
|
|
|
dataLength,
|
|
|
|
|
|
buffer,
|
|
|
|
|
|
bufferSize,
|
|
|
|
|
|
&numBytesEncrypted);
|
|
|
|
|
|
if (cryptStatus == kCCSuccess) {
|
|
|
|
|
|
return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
|
|
|
|
|
|
}
|
|
|
|
|
|
free(buffer);
|
|
|
|
|
|
return nil;
|
|
|
|
|
|
}
|
|
|
|
|
|
+ (NSData *)AES128_Decrypt:(NSString *)key encryptData:(NSData *)data{
|
|
|
|
|
|
char keyPtr[kCCKeySizeAES128+1];
|
|
|
|
|
|
bzero(keyPtr, sizeof(keyPtr));
|
|
|
|
|
|
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
|
|
|
|
|
|
|
|
|
|
|
|
//无向量
|
|
|
|
|
|
// char ivPtr[kCCKeySizeAES128+1];
|
|
|
|
|
|
// memset(ivPtr, 0, sizeof(ivPtr));
|
|
|
|
|
|
// [gIv getCString:ivPtr maxLength:sizeof(ivPtr) encoding:NSUTF8StringEncoding];
|
|
|
|
|
|
|
|
|
|
|
|
NSUInteger dataLength = [data length];
|
|
|
|
|
|
size_t bufferSize = dataLength + kCCBlockSizeAES128;
|
|
|
|
|
|
void *buffer = malloc(bufferSize);
|
|
|
|
|
|
size_t numBytesDecrypted = 0;
|
|
|
|
|
|
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt,
|
|
|
|
|
|
kCCAlgorithmAES128,
|
|
|
|
|
|
kCCOptionPKCS7Padding,
|
|
|
|
|
|
keyPtr,
|
|
|
|
|
|
kCCBlockSizeAES128,
|
|
|
|
|
|
nil,
|
|
|
|
|
|
[data bytes],
|
|
|
|
|
|
dataLength,
|
|
|
|
|
|
buffer,
|
|
|
|
|
|
bufferSize,
|
|
|
|
|
|
&numBytesDecrypted);
|
|
|
|
|
|
if (cryptStatus == kCCSuccess) {
|
|
|
|
|
|
return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
|
|
|
|
|
|
}
|
|
|
|
|
|
free(buffer);
|
|
|
|
|
|
return nil;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-11 10:16:56 +08:00
|
|
|
|
//将NSData转换成十六进制的字符串则可使用如下方式:
|
|
|
|
|
|
+ (NSString *)convertDataToHexStr:(NSData *)data {
|
|
|
|
|
|
if (!data || [data length] == 0) {
|
|
|
|
|
|
return @"";
|
|
|
|
|
|
}
|
|
|
|
|
|
NSMutableString *string = [[NSMutableString alloc] initWithCapacity:[data length]];
|
|
|
|
|
|
|
|
|
|
|
|
[data enumerateByteRangesUsingBlock:^(const void *bytes, NSRange byteRange, BOOL *stop) {
|
|
|
|
|
|
unsigned char *dataBytes = (unsigned char*)bytes;
|
|
|
|
|
|
for (NSInteger i = 0; i < byteRange.length; i++) {
|
|
|
|
|
|
NSString *hexStr = [NSString stringWithFormat:@"%x", (dataBytes[i]) & 0xff];
|
|
|
|
|
|
if ([hexStr length] == 2) {
|
|
|
|
|
|
[string appendString:hexStr];
|
|
|
|
|
|
} else {
|
|
|
|
|
|
[string appendFormat:@"0%@", hexStr];
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}];
|
|
|
|
|
|
|
|
|
|
|
|
return string;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//将十六进制字符串转换为二进制数据
|
|
|
|
|
|
+ (NSData *)dataFromHexString:(NSString *)hexString {
|
|
|
|
|
|
// 去除字符串中的空格和换行符
|
|
|
|
|
|
NSString *cleanedString = [hexString stringByReplacingOccurrencesOfString:@" " withString:@""];
|
|
|
|
|
|
cleanedString = [cleanedString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
|
|
|
|
|
|
|
|
|
|
|
|
// 检查字符串长度是否为偶数
|
|
|
|
|
|
if (cleanedString.length % 2 != 0) {
|
|
|
|
|
|
// 如果长度为奇数,可以根据需要选择在字符串前面添加或去除一个字符
|
|
|
|
|
|
// 例如,你可以选择在前面添加一个零,或者去除最后一个字符
|
|
|
|
|
|
// cleanedString = [@"0" stringByAppendingString:cleanedString];
|
|
|
|
|
|
// 或者
|
|
|
|
|
|
// cleanedString = [cleanedString substringToIndex:cleanedString.length - 1];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 使用一个循环将每两个字符转换为一个字节
|
|
|
|
|
|
NSMutableData *data = [NSMutableData data];
|
|
|
|
|
|
for (NSInteger i = 0; i < cleanedString.length; i += 2) {
|
|
|
|
|
|
NSString *byteString = [cleanedString substringWithRange:NSMakeRange(i, 2)];
|
|
|
|
|
|
NSScanner *scanner = [NSScanner scannerWithString:byteString];
|
|
|
|
|
|
unsigned int byteValue;
|
|
|
|
|
|
[scanner scanHexInt:&byteValue];
|
|
|
|
|
|
uint8_t byte = (uint8_t)byteValue;
|
|
|
|
|
|
[data appendBytes:&byte length:1];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
|
}
|
2023-11-01 14:18:52 +08:00
|
|
|
|
@end
|