xcode 将 NSString 转换为十六进制
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4279731/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting NSString to Hex
提问by Loren Zimmer
I've been doing a lot of reading on how to convert a string to a hex value. Here is what I found to accomplish this:
我一直在阅读有关如何将字符串转换为十六进制值的大量内容。这是我发现的实现此目的的方法:
NSString * hexString = [NSString stringWithFormat:@"%x", midiValue];
This returned some "interesting" results and upon reading a little further I found a post that mentioned this
这返回了一些“有趣”的结果,在进一步阅读后,我发现了一篇提到这一点的帖子
"You're passing a pointer to an object representing a numeric value, not the numeric value proper."
“您正在传递一个指向表示数值的对象的指针,而不是正确的数值。”
So I substituted 192 instead of "midiValue" and it did what I expected.
所以我用 192 代替了“midiValue”,它达到了我的预期。
How would I pass the string value and not the pointer?
我将如何传递字符串值而不是指针?
Decleration of midiValue:
midiValue 的声明:
NSString *dMidiInfo = [object valueForKey:@"midiInformation"];
int midiValue = dMidiInfo;
回答by MCannon
You probably need to do something like this:
你可能需要做这样的事情:
NSNumberFormatter *numberFormatter= [[NSNumberFormatter alloc] init];
int anInt= [[numberFormatter numberFromString:string ] intValue];
also, I think there is some example code in the xcode documentation for converting to and from a hex value, in the QTMetadataEditor sample. in the MyValueFormatter class.
另外,我认为在 QTMetadataEditor 示例中,xcode 文档中有一些示例代码用于在十六进制值之间进行转换。在 MyValueFormatter 类中。
+ (NSString *)hexStringFromData:(NSData*) dataValue{
UInt32 byteLength = [dataValue length], byteCounter = 0;
UInt32 stringLength = (byteLength*2) + 1, stringCounter = 0;
unsigned char dstBuffer[stringLength];
unsigned char srcBuffer[byteLength];
unsigned char *srcPtr = srcBuffer;
[dataValue getBytes:srcBuffer];
const unsigned char t[16] = "0123456789ABCDEF";
for (; byteCounter < byteLength; byteCounter++){
unsigned src = *srcPtr;
dstBuffer[stringCounter++] = t[src>>4];
dstBuffer[stringCounter++] = t[src & 15];
srcPtr++;
}
dstBuffer[stringCounter] = '//========================================================
// This action is executed whenever the hex button is
// tapped by the user.
//========================================================
- (IBAction)hexPressed:(UIButton *)sender
{
// Change the current base to hex.
self.currentBase = @"hex";
// Aquire string object from text feild and store in a NSString object
NSString *temp = self.labelDisplay.text;
// Cast NSString object into an Int and the using NSString method StringWithFormat
// which is similar to c's printf format then output into hexidecimal then return
// this NSString object with the hexidecimal value back to the text field for display
self.labelDisplay.text=[NSString stringWithFormat:@"%x",[temp intValue]];
}
';
return [NSString stringWithUTF8String:(char*)dstBuffer];
}
+ (NSData *)dataFromHexString:(NSString*) dataValue{
UInt32 stringLength = [dataValue length];
UInt32 byteLength = stringLength/2;
UInt32 byteCounter = 0;
unsigned char srcBuffer[stringLength];
[dataValue getCString:(char *)srcBuffer];
unsigned char *srcPtr = srcBuffer;
Byte dstBuffer[byteLength];
Byte *dst = dstBuffer;
for(; byteCounter < byteLength;){
unsigned char c = *srcPtr++;
unsigned char d = *srcPtr++;
unsigned hi = 0, lo = 0;
hi = charTo4Bits(c);
lo = charTo4Bits(d);
if (hi== 255 || lo == 255){
//errorCase
return nil;
}
dstBuffer[byteCounter++] = ((hi << 4) | lo);
}
return [NSData dataWithBytes:dst length:byteLength];
}
Hopefully this helps.
希望这会有所帮助。
回答by Chris
if you are messing with a simple iPhone app in Xcode using the iPhone 5.1 Simulator then this is useful:
如果您正在使用 iPhone 5.1 模拟器在 Xcode 中处理一个简单的 iPhone 应用程序,那么这很有用:
##代码##