简体   繁体   English

谁能告诉我该功能的影响?

[英]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.

相关问题 有人可以告诉我此控制器的名称吗? - Can someone tell me what is the name of this controller? 无法解析“./elements/Marker” - 有人可以告诉我出了什么问题吗? - Unable to resolve “./elements/Marker” - can somebody tell me what's wrong? 使用afnetworking 3.0.4发生崩溃,[NSProgress setTotalUnitCount:],任何可以告诉我原因的人 - there is a crash use the afnetworking 3.0.4 , [NSProgress setTotalUnitCount:], anyone who can tell me the reason 苹果可以告诉我用户购买了什么吗? - Can Apple tell me what purchases a user has? 有人能告诉我导航窗口中的这些问号是什么意思吗? (Xcode 4.2) - Can someone tell me what what these question marks in my navigator window mean? (Xcode 4.2) 有人可以告诉我iOS中的移动设备证书身份验证是什么吗? 使用它的目的是什么? - Can someone tell me what is mobile device certificate authentication in iOS? what is the purpose of using it? 使用React Native在xCode中存档时出错。 你能告诉我什么问题吗? - Error while archive in xCode using react native. Can you tell me what issue? iOS,Cocos2d-iphone:可以告诉我这些“ @”是什么代码吗? - iOS, Cocos2d-iphone: Can any tell me what are these codes @“”? 您能告诉我像Facebook应用中用于屏幕导航的控件的名称是什么吗? - Can you tell me what is the name of the control that is used for screen navigation like that in Facebook app? 谁能告诉我 iOS 中的这些手势名称。 任何好的示例代码都会有所帮助 - Can any one tell me these gesture name's in iOS . Any good example code will be helpful
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM