2010-11-25 142 views
2

我一直在大量阅读如何将字符串转换为十六进制值。以下是我发现做到这一点:将NSString转换为十六进制

NSString * hexString = [NSString stringWithFormat:@"%x", midiValue]; 

这返回的是一些“有趣”的结果,并在阅读远一点我发现,提到这个

“你传递一个指针后代表数值的对象,而不是数值本身。“

所以我用192代替了“midiValue”,它做了我所期望的。

如何传递字符串值而不是指针?

Decleration midiValue的:

NSString *dMidiInfo = [object valueForKey:@"midiInformation"]; 
    int midiValue = dMidiInfo; 
+0

“有趣”的结果并没有告诉我们是什么。你能指定吗?你也可以显示'midiValue`的声明吗? – zneak 2010-11-25 17:55:46

+0

我不明白这个问题。你说你想要将一个字符串转换为十六进制,但代码示例生成一个字符串作为输出,而不是将字符串作为输入。 – JWWalker 2010-11-25 17:59:02

回答

4

你可能需要做这样的事情:

NSNumberFormatter *numberFormatter= [[NSNumberFormatter alloc] init]; 
int anInt= [[numberFormatter numberFromString:string ] intValue]; 

还可以,我觉得是Xcode文档中的一些示例代码的转换和从十六进制值,在QTMetadataEditor示例中。在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] = "ABCDEF"; 

    for (; byteCounter < byteLength; byteCounter++){ 
     unsigned src = *srcPtr; 
     dstBuffer[stringCounter++] = t[src>>4]; 
     dstBuffer[stringCounter++] = t[src & 15]; 
     srcPtr++; 
    } 
    dstBuffer[stringCounter] = '\0'; 

    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]; 
} 

希望这会有所帮助。

2

,如果你是使用iPhone 5.1模拟器在Xcode简单的iPhone应用程序搞乱,那么这是有用的:

//======================================================== 
// 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]]; 

}