【问题标题】:Transform NSData to int in Objective-c在 Objective-c 中将 NSData 转换为 int
【发布时间】:2014-08-26 14:19:03
【问题描述】:

我想在 Objective-c 中将 NSData 转换为 int。

在下面的代码中,“index2”是“15”,但“value”是“0”。 (NSLog 显示“15,0”。)

准确地说,“值”必须是“15”。

 NSString *index = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
 NSString *index2 = [index stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
 int value = [index2 intValue];
 NSLog(@"%@,%d",index2,value);

你能告诉我如何解决这个问题吗?

【问题讨论】:

标签: objective-c nsstring


【解决方案1】:

如果您想简单地将 nsdata 转换为 int,请尝试以下操作。

int theInteger;
[completeData getBytes:&theInteger length:sizeof(theInteger)];

检查this link,您可能需要使用范围读取 NSData。

【讨论】:

  • 感谢您的回复。然而我做不到。 theInteger 变为 -272647185。准确地说,它必须是 15。
  • 谢谢。我不明白NSString index1是'15',但是int index2不是15而是0。为什么[将NSString转换为int]会出错?
猜你喜欢
  • 2016-10-22
  • 2011-09-18
  • 2012-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-11
  • 1970-01-01
相关资源
最近更新 更多