2012-09-10 285 views
0

我收到來自web服務的響應作爲json字符串,我希望它將該字符串轉換爲二進制。將字符串轉換爲二進制

有沒有辦法做到這一點?

+2

你說的*二進制意味着*?從某種意義上說,所有的計算機數據都是1s和0s ......嗯......也許你的意思是一些其他的*二進制表示? –

回答

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"); 
} 
相關問題