[問題] char to NSString

看板MacDev作者 (baw)時間13年前 (2012/03/25 17:25), 編輯推噓1(104)
留言5則, 3人參與, 最新討論串1/3 (看更多)
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)

03/25 19:37, , 1F
不是有個stringWithCString:encoding:可以用嗎?
03/25 19:37, 1F

03/25 19:38, , 2F
還是有什麼理由不能用上面說的event?
03/25 19:38, 2F

03/26 16:31, , 3F
用了stringWithCString:encoding: 還是一樣EXC_BADACCESS
03/26 16:31, 3F

03/26 17:13, , 4F
[data bytes] 傳回的是不是pointer?stringWithFormat
03/26 17:13, 4F

03/26 17:13, , 5F
接受的應該是value喔
03/26 17:13, 5F
※ 編輯: heyshi 來自: 111.255.191.185 (03/26 19:00)
文章代碼(AID): #1FRkI8jc (MacDev)
討論串 (同標題文章)
文章代碼(AID): #1FRkI8jc (MacDev)