Converting NSdata to bytes & then converting the first n bytes to int

后端 未结 3 1809
谎友^
谎友^ 2021-02-03 16:36

I\'ve a NSData object. First I want to convert NSData object to bytes, then read the first four bytes in this NSData object and then conve

3条回答
  •  旧时难觅i
    2021-02-03 17:02

    int n ; // first n bytes
    NSData *data; // your data
    
    NSData *subData = [data subdataWithRange:NSMakeRange(0, n)]; // make sure if data has n bytes
    
    NSString *stringData = [subData description];
    stringData = [stringData substringWithRange:NSMakeRange(1, [stringData length]-2)];
    
    unsigned dataAsInt = 0;
    NSScanner *scanner = [NSScanner scannerWithString: stringData];
    [scanner scanHexInt:& dataAsInt];
    

提交回复
热议问题