[英]who can tell me what the function's affect?
The first method is: 第一种方法是:
-(NSData *)stringToAddBytes:(NSString*)addString
{
int length = (int)[addString length];
if(length < 2)
{
return nil;
}
Byte buf[length / 2];
for(int i = 0 ;i < length/2 ;i++)
{
NSString *str = [addString substringWithRange:NSMakeRange(i * 2, 2)];
Byte b = [self hexStringToByte:str];
buf[i]=b;
}
NSData * myD = [[NSData alloc]initWithBytes:buf length:length/2];
return myD;
}
THe method that the first method called. 第一个方法调用的方法。
-(Byte)hexStringToByte:(NSString*)str
{
NSArray *charArray = [[NSArray alloc]initWithObjects:@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",
@"A",@"B",@"C",@"D",@"E",@"F",nil];
NSString *str1 = [str substringWithRange:NSMakeRange(0, 1)];
int num1 = (int)[charArray indexOfObject:str1];
NSString *str2 = [str substringWithRange:NSMakeRange(1, 1)];
int num2 = (int)[charArray indexOfObject:str2];
Byte b = num1*16+num2;
return b;
}
Thank you for your answer.It looks change large char to small char. 谢谢您的回答。看起来从大字符变为小字符。
hexStringToByte:
wil convert string with hexadecimal number representation (example @"FF") to Byte
value (in this example 255). hexStringToByte:
将具有十六进制数字表示形式的字符串(例如@“ FF”)转换为Byte
值(在本示例中为255)。
stringToAddBytes:
uses hexStringToByte:
to create NSData
of bytes breaking addString
into two letter peases and converting them to Byte
values. stringToAddBytes:
使用hexStringToByte:
创建字节的NSData
,将addString
分成两个字母豌豆并将其转换为Byte
值。
In other words, this is string serialization. 换句话说,这是字符串序列化。
Example: 例:
// 255 = 0xFF
// 170 = 0xAA
// 136 = 0x88
NSString* addString = @"FFAA88";
NSData* data = [self stringToAddBytes:addString];
// data will be [255, 170, 136]
Be aware that NSData
is not an array, instead, it represents a raw object. 请注意,
NSData
不是数组,而是代表原始对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.