hi
I'm trying to print in binary the data stored in a string..
here's the string:
char test[20];
strcpy(test, "any data");
so how to print the bits?
is there an escape sequence that i can use with printf to do that?
thanks
Printable View
hi
I'm trying to print in binary the data stored in a string..
here's the string:
char test[20];
strcpy(test, "any data");
so how to print the bits?
is there an escape sequence that i can use with printf to do that?
thanks
take in each character,convert it into binary form, and output to the screen..
this function converts an integer into binary form and displays it...i leave it to you to modify this to print the string...it wont be difficult.Code:void bin(int a)
{
if(a/2==1)
printf("1");
else
bin(a/2);
printf("%d",a%2);
}
hum are you sure about this function? this will work for numbers between 0 and 10...I guess I'll have to add a loop..
it looks like it works for numbers > 10...
you will have to add a loop and yes, i am pretty much sure bout this function.Quote:
hum are you sure about this function? this will work for numbers between 0 and 10...I guess I'll have to add a loop..
You could do something with this bits_block.Quote:
Originally Posted by majoub
Code:int main(void)
{
const char test[] = "any data";
char binary [ (CHAR_BIT + 1) * sizeof test ];
puts(test);
puts(bits_block(binary, test, sizeof test));
return 0;
}
/* my output
any data
01100001-01101110-01111001-00100000-01100100-01100001-01110100-01100001-00000000
*/