2010-11-25 143 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]]; 

}