作者heyshi (baw)
看板MacDev
標題[問題] char to NSString
時間Sun Mar 25 17:25:25 2012
NSData *buf;
NSString *str;
Byte *p =(Byte*) [[buf subdataWithRange:NSMakeRange(127, 1)] bytes];
// p裡的值為 <0d> 想取出13的值
int charToDecimal = 0;
charToDecimal = *p;
str = [NSString stringWithFormat:@"%d",charToDecimal];
想請問有沒有更短的寫法??
我改寫成以下寫法就爆了 Q_Q
str = [NSString stringWithFormat:@"%d",
[[buf subdataWithRange:NSMakeRange(127, 1)] bytes]];
zoble大提供的方法, obj-c跟C混搭真的不習慣Q_Q
str = [NSString stringWithFormat:@"%d",
*(char *)[[buf subdataWithRange:NSMakeRange(127, 1)] bytes]];
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 111.255.186.33
※ 編輯: heyshi 來自: 111.255.172.228 (03/25 19:26)
→ Killercat:不是有個stringWithCString:encoding:可以用嗎? 03/25 19:37
→ Killercat:還是有什麼理由不能用上面說的event? 03/25 19:38
→ heyshi:用了stringWithCString:encoding: 還是一樣EXC_BADACCESS 03/26 16:31
推 Blueshiva:[data bytes] 傳回的是不是pointer?stringWithFormat 03/26 17:13
→ Blueshiva:接受的應該是value喔 03/26 17:13
※ 編輯: heyshi 來自: 111.255.191.185 (03/26 19:00)