读取NsArray的十六进制值返回0

发布于 2025-02-13 23:02:38 字数 1667 浏览 0 评论 0原文

在我的Objective-C代码中,我收到了一个包含十六进制值的文件。我读取该文件并将前几个字节转换为NSarray,然后尝试阅读以执行计算。 The conversion seems to work, this is what it looks like:

    NSBundle *mainBundle = [NSBundle mainBundle];
    NSFileHandle *fh;
    NSData *wordBuffer;
    NSMutableArray *myByteArray = [NSMutableArray new];

    NSString *filePath = [mainBundle pathForResource:@"data" ofType:@"txt"];
    fh = [NSFileHandle fileHandleForReadingAtPath: filePath];

    while ((wordBuffer = [fh readDataOfLength:1]) && [wordBuffer length] ) 
    {
        [myByteArray addObject:[self hexStringForData:wordBuffer]];
    }
    return myByteArray];
}

The problem is when I read back from that Array, values that use letter in hexadecimal are completely wrong, as if objective-C ignored the letters. FF返回0,7b返回7,依此类推。

So I get the myByteArray back from the init function, store it in response and try to read it like this:

unsigned long test = [response[1] unsignedLongValue];
NSLog(@"Value in unsigned long: %lu", test);

This crashed the app because of an NSInvalidArgumentException cause by the selector '卢',但我不明白为什么。

unsigned int test = [[answerHeader response][1] unsignedIntValue];
NSLog(@"Value in unsigned long: %u", test);

this returns 0

NSString *str = response[1];
unsigned long red = strtoul([str UTF8String], 0, 16);
NSLog(@"converted long: %lu", red);

this returns 255

I don't think I can really afford to read every single bytes like that (it looks more expensive that array[2] intValue). 发生了什么,我该如何解决? 提前致谢 !

In my objective-C code, I received a file containing hexadecimal values. I read that file and convert the first few bytes to an NSArray which I then try to read to perform computations on. The conversion seems to work, this is what it looks like:

    NSBundle *mainBundle = [NSBundle mainBundle];
    NSFileHandle *fh;
    NSData *wordBuffer;
    NSMutableArray *myByteArray = [NSMutableArray new];

    NSString *filePath = [mainBundle pathForResource:@"data" ofType:@"txt"];
    fh = [NSFileHandle fileHandleForReadingAtPath: filePath];

    while ((wordBuffer = [fh readDataOfLength:1]) && [wordBuffer length] ) 
    {
        [myByteArray addObject:[self hexStringForData:wordBuffer]];
    }
    return myByteArray];
}

The problem is when I read back from that Array, values that use letter in hexadecimal are completely wrong, as if objective-C ignored the letters. FF returns 0, 7b returns 7 and so on.

So I get the myByteArray back from the init function, store it in response and try to read it like this:

unsigned long test = [response[1] unsignedLongValue];
NSLog(@"Value in unsigned long: %lu", test);

This crashed the app because of an NSInvalidArgumentException cause by the selector 'lu', but I don't understand why.

unsigned int test = [[answerHeader response][1] unsignedIntValue];
NSLog(@"Value in unsigned long: %u", test);

this returns 0

NSString *str = response[1];
unsigned long red = strtoul([str UTF8String], 0, 16);
NSLog(@"converted long: %lu", red);

this returns 255

I don't think I can really afford to read every single bytes like that (it looks more expensive that array[2] intValue).
What's going on and how can I fix it ?
Thanks in advance !

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紧拥背影 2025-02-20 23:02:38

我建议您将文件读为字符串:
read txt file into string

NSStringEncoding encoding;
NSError *error;
NSString *fileContents = [[[NSString alloc] initWithContentsOfFile:filePath
                                                      usedEncoding:&encoding
                                                             error:&error]

then
parse hex to in int

unsigned result = 0;
NSScanner *scanner = [NSScanner scannerWithString:@"#01FFFFAB"];

[scanner setScanLocation:1]; // bypass '#' character
[scanner scanHexInt:&result];

I would recommend you to read the file into string:
read txt file into string

NSStringEncoding encoding;
NSError *error;
NSString *fileContents = [[[NSString alloc] initWithContentsOfFile:filePath
                                                      usedEncoding:&encoding
                                                             error:&error]

then
parse hex to int

unsigned result = 0;
NSScanner *scanner = [NSScanner scannerWithString:@"#01FFFFAB"];

[scanner setScanLocation:1]; // bypass '#' character
[scanner scanHexInt:&result];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文