我會轉換的NSString爲unsigned char []
NSString *myString = @"0x10 0x1c 0x37 0x00"; //Aquired reading text file using NSString methods..
到
unsigned char convertedfrommyString[] = { 0x10, 0x1c, 0x37, 0x00 };
我的目標是aquiring他們,然後使用此代碼將其交換:
unsigned char convertedfrommyString[] = { 0x10, 0x1c, 0x37, 0x00 };
int data = *((int *) convertedfromString);
NSLog(@"log = %08x", data);
輸出應該是:
log = 00371c10
任何幫助?
編輯
無論從一月巴蒂斯特尤尼斯和Sven我發現的方式來理解我的問題和與此代碼解決:
NSString *myString = [[@"0x10 0x1c 0x37 0x00" stringByReplacingOccurrencesOfString:@"0x" withString:@""] stringByReplacingOccurrencesOfString:@" " withString:@""];
unsigned result = 0;
NSScanner *scanner = [NSScanner scannerWithString:myString];
[scanner scanHexInt:&result];
int reverse = NSSwapInt(result);
NSLog(@"scanner: %8u", result);
NSLog(@"bytes:%08x", result);
NSLog(@"reverse:%08x (that is what i need!)", reverse);
真行! 但我能接受兩個答案嗎?
like * myString = @「101c3700」然後呢? :) – Mike97 2014-12-02 18:39:46
沒有使用'scannerWithString','scanUpToString:@「」intoString:aNumberString'構造掃描器,然後用另一個掃描器和'scanNexInt:@ myInt'將'aNumberString'變量轉換爲int。 – 2014-12-02 18:48:20
感謝您回答我。我找到了從你的建議中做出我需要的方法,但也從Sven的回答中... – Mike97 2014-12-02 19:22:34