简体   繁体   English

我正在字符串<0000000a>中获取十六进制值吗? 如何在Objective-C中将其转换为整数?

[英]I am getting hex value in string <0000000a>? How can i convert it into integer in objective-c?

First i have to convert Hex but this <> generates an exception when i am trying to convert it to integer 首先,我必须转换十六进制,但是当我尝试将其转换为整数时,此<>会生成异常

NSString *steps =characteristic.value;
int value2= [steps intValue];

First i have to removce <>, then convert this hexadecimal string into integer value. 首先,我必须删除<>,然后将此十六进制字符串转换为整数值。

NSString *strVal = [[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding]; NSString * strVal = [[NSString alloc] initWithData:characteristic.value编码:NSUTF8StringEncoding]; int intValue = strVal.intValue; int intValue = strVal.intValue;

You have to prefix your string with 0x : 您必须在字符串0x加上0x

NSString *orig = @"000000ae";
NSString *str = [NSString stringWithFormat:@"0x%@" , orig];

to be able to use NSScanner : 能够使用NSScanner

unsigned int outVal;
NSScanner* scanner = [NSScanner scannerWithString:str];
[scanner scanHexInt:&outVal];

outVal holds your result now. outVal现在保存您的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在objective-c中将整数转换为相应的单词? - How do I convert an integer to the corresponding words in objective-c? 为什么我在objective-c中得到指针转换错误的整数? - Why am I getting an integer to pointer conversion error in objective-c? 如何在 Objective-C 中将 HEX 转换为 NSString? - How to convert HEX to NSString in Objective-C? Objective-C-如何将字节数组转换为NSString? - Objective-C - How can I convert Byte Array to NSString? 如何根据标签值或十六进制颜色代码为按钮背景提供颜色,就像以前在 Objective-C 中一样? - How can I provide colour to button background in accordance with Hashtag Value or Hex Colour Code , like it used to be in Objective-C? Objective-C中的HEX值 - HEX value in Objective-C 如何将NSdata转换为十六进制字符串? - How can i convert NSdata to hex string? 如何在iOS上使用Objective-C使用NSRegularExpression将十六进制html实体转换为文本字符串中的十进制html实体? - How can I use an NSRegularExpression to convert hexadecimal html entities to decimal html entities in a string of text, using Objective-C on iOS? 在Objective-C中将十六进制代码转换为Unicode - Convert Hex code to Unicode in objective-C 如何在Objective C中将基于XML的String值转换为整数 - How to convert XML based String value to integer in Objective C
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM