作者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)
1F:→ Killercat:不是有个stringWithCString:encoding:可以用吗? 03/25 19:37
2F:→ Killercat:还是有什麽理由不能用上面说的event? 03/25 19:38
3F:→ heyshi:用了stringWithCString:encoding: 还是一样EXC_BADACCESS 03/26 16:31
4F:推 Blueshiva:[data bytes] 传回的是不是pointer?stringWithFormat 03/26 17:13
5F:→ Blueshiva:接受的应该是value喔 03/26 17:13
※ 编辑: heyshi 来自: 111.255.191.185 (03/26 19:00)