将字符串转换为二进制

时间:2012-09-10 05:08:15

标签: objective-c json

我收到来自Web服务的响应为json字符串,我希望它将该字符串转换为二进制文件。

有什么办法吗?

2 个答案:

答案 0 :(得分:0)

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");
}

答案 1 :(得分:0)

您可以使用NSString对象中的dataUsingEnconding消息:

dataUsingEncoding: