读取NsArray的十六进制值返回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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议您将文件读为字符串:
read txt file into string
then
parse hex to in int
I would recommend you to read the file into string:
read txt file into string
then
parse hex to int