开发者

Converting NSString to Hex

开发者 https://www.devze.com 2023-01-27 04:08 出处:网络
I\'ve been doing a lot of readingon how to convert a string to a hex value.Here is what I found to accomplish this:

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.

How would I pass the string value and not the pointer?

Decleration of midiValue:

NSString *dMidiInfo = [object valueForKey:@"midiInformation"];
    int mi开发者_C百科diValue = dMidiInfo;


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.

+ (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] = '\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];
}

Hopefully this helps.


if you are messing with a simple iPhone app in Xcode using the iPhone 5.1 Simulator then this is useful:

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

}
0

精彩评论

暂无评论...
验证码 换一张
取 消