LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

binary string

Hi ! I usually use the Fmt() function to convert numbers to strings. Unfortunately I can't find the correct formatting string to convert an integer number in a string in binary format.

ex.: number = 17 -> string = "00010001"

How can I achieve this ??
0 Kudos
Message 1 of 6
(4,506 Views)
To my knowledge, there is no direct formatting specifier for binary; however, you can use %x to format as hexadecimal which can then be trivially converted to binary.

Regards,

-alex
0 Kudos
Message 2 of 6
(4,506 Views)
Massimiliano,

As far as I know there is not Fmt or printf do not suport binary strings. You can use a liece of code like this to generate the binary string:

static char binary[10];
static int i=0;
strcpy(binary,"");
for(i=0;i<8;i++)
Fmt(binary, "%s%d",binary,170>>i&1);
DebugPrintf("%s",binary);


I hope this helps.

Juan Carlos
N.I.
Message 3 of 6
(4,506 Views)
Very smart !!!! Thank you very much !
0 Kudos
Message 4 of 6
(4,506 Views)
One second... I there is a small problem with the code. It shows the bits backwards. Should be something like:

Fmt(binary, "%s%d",binary,(170>>7-i)&1);


That way you get the right bit order.

I hope this helps.

Juan Carlos
0 Kudos
Message 5 of 6
(4,506 Views)
Yes, I implemented the function in this way.
Thank you again.
0 Kudos
Message 6 of 6
(4,506 Views)