我从 web 服务收到响应为 json 字符串,我希望它将该字符串转换为二进制。
有什么办法吗?
char s[255] = "JSON String value";
//for each character, print it's binary aoutput
int i,c,power;
for ( i=0 ; s[i]!='\0' ; i++ )
{
//c holds the character being converted
c = s[i];
//for each binary value below 256 (because ascii values < 256)
for ( power=7 ; power+1 ; power-- )
//if c is greater than or equal to it, it is a 1
if ( c >= (1<<power) )
{
c -= (1<<power); //subtract that binary value
printf("1");
}
//otherwise, it is a zero
else
printf("0");
}
您可以使用来自 NSString 对象的 dataUsingEnconding 消息: