LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How do I convert decimal or hex to binary

Darnell:

 

dummy_decoy makes a couple of good points.  I'll add a few comments on his reply before responding to your latest questions.

Programming books can be a good resource.  I have a couple that I still pull out after 15 years of programming experience.  There are many good books out there.  Here are a couple on my shelf.

Programming in ANSI C by Stephen G Kochan.  This is the first book I reach for.  Here it is at Amazon.

http://www.amazon.com/Programming-ANSI-Stephen-G-Kochan/dp/0672303396/ref=sr_1_1?ie=UTF8&s=books&qid...

There is a newer edition available, but I haven't seen it.

No library is complete without going back to the source: K&R.  The C Programming Language by Brian W Kernighan and Dennis M Ritchie.  Dennis Ritchie is the original developer of C.  The book is pretty bare bones, so I wouldn't make this your only book, but it's good.  It's also available in other languages.

http://www.amazon.com/Programming-Language-Prentice-Hall-Software/dp/0131103628/ref=sr_1_6?ie=UTF8&s...

Workout C by David Himmel has a lot of good exercises.

http://www.amazon.com/Workout-Learn-Through-Exercises-floppy/dp/187873914X/ref=sr_1_3?ie=UTF8&s=book...

If you venture into C++, you need to read Bjarne Stroustrup. 

http://www.amazon.com/Bjarne-Stroustrup/e/B000AQ349S/ref=ntt_aut_sim_1_1

 

You need to learn about data types and representation in C: how numbers are stored (integers and floating point); how characters are stored; how strings are stored.  You need to learn about arrays and pointers.

 

One of the things I like about LabWindows/CVI is that it makes Windows GUI programming easy.  You can create your GUI graphically, and have CVI generate the programming shell for you.  I don't think it's much harder to drop a control on the UIR and then use GetCtrlVal or SetCtrlVal than it is to use gets and printf.  But dummy_decoy is right: if you're trying to learn too much at once you can easily get lost and frustrated.

 

Now back to your latest questions.

In C, a string is an array of characters terminated by the NULL character (\0).  For you first question where you just want to set one bit (instead of translating to binary), do something like this.  Note the difference between the double quote " (for null-terminated strings) and the single quote ' (for single characters).

 

char myString[9];   // 8 characters followed by NULL

int x;

int bitValue;

 

strcpy(myString, "00000000");

 

// note that array indexes in C are 0 based

// set user bit 1 = set array index 0

myString[0] = '1';

 

// reset all bits to 0 

strcpy(myString, "00000000");

// set user bit 5 = set array index 4

myString[4] = '1';

 

// reset all bits to 0 

strcpy(myString, "00000000");

// set user bit x

x = 3;

myString[x-1] = '1';

 

// create a number with a single bit set

// set user bit x

x = 3;

bitValue = pow(2, x-1);

 

Now for your final question:

 

"now for my attenuator i want binary numbers up to eight bits from 1-64.

 

so if i enter 5, its going to print out 00000101."

 

The project I posted already does that.

Build and run the project.

Select input format Decimal.

Enter 5 for Integer Input.

Convert to Binary.

 

0 Kudos
Message 11 of 12
(2,404 Views)

I was with you all the way to:

 

// create a number with a single bit set

// set user bit x

x = 3;

bitValue = pow(2, x-1);

 

Overkill! For this application, something more along the lines of:

 

// create a number with a single bit set

// set user bit x

x = 3;

bitValue = 1 << x;

  

would be much more appropriate.

 

JR

Message 12 of 12
(2,394 Views)