12-16-2005 11:59 AM
12-17-2005 04:18 PM - edited 12-17-2005 04:18 PM
Message Edited by Roberto Bozzolo on 12-17-2005 11:21 PM
12-20-2005 07:42 AM
Chinook1,
You can also use the ANSI C strtol function. It will allow you to convert from a string representation of a non-base 10 number into a base 10 number.
07-30-2009 05:38 PM
//Can some fix these errors, i got errors when i added my main.i dont know whats the problem
#include <ansi_c.h>
#include <string.h>
char PrintBin(int,char);
int main()
{
PrintBin (int n,string);
return 0;
}
char *PrintBin (int n, char *string)
{
int len = 0;
if (n < 0) return NULL; // Number must be non-negative
// Recursively call the function for 1 to n-1 bits
if (n > 1) string = PrintBin (n / 2, string);
// Dynamic allocation of memory
if (!string) {
string = malloc (2);
memset (string, 0, 2);
}
else {
len = strlen (string);
string = realloc (string, len + 2);
string[len] = 0;
}
// Print the last bit
sprintf (string + strlen (string), "%d", n % 2);
return string;
}
//its still not working in labwindows
07-30-2009 11:54 PM - edited 07-30-2009 11:56 PM
Hi darnell,
iy would be helpful to know which errors you have found... Anyway, the most probable case of error is that you have not defined your variables and the compiler will bother about it. Try this way:
int main()
{
int n;
char *string = NULL;
n = 123; // Put your value here
string = PrintBin (int n,string);
if (!string) {
// Error checking
}
else
DebugPrintf ("%d converted in binary is %s\n", n, string);
free (string); // Don't forget to free the string
return 0;
}
07-31-2009 07:24 AM
wow, this PrintBin() implementation is a nice show-off for an assignment: a recursive function.
i personally would get rid of the recursion, but this is left as an exercise for the original poster...
08-01-2009 01:11 AM
OK I guess im confusing everyone.
ok this what i want my program to do.
Enter BInary number: 1
0000001
Now if you checkmy program allows you to input any number between 1-64 and it will print any binary number. so can some get it to do the same thing on my gui?
//Now im trying to get that simple program to go into some type of box on my gui , whether it be a string box, tex box, a pop up ,anytype of box. I just want it to be displayed.
//Now it does do what i want it to do on the standard i/o screen.
//but I want it to do the samething on my gui.
Can someone fix my code to do that. I attach the entire program already.
08-12-2009 11:08 AM
Do you want to convert a number to a string with a different radix, or do you just want to change the radix as displayed?
The attached example does both. Sorry for the old version (6.0!): it's the only machine I had access to.
If you're using a CVI UIR, the simplest thing is to change the radix of the indicator to whatever format you want. For example
SetCtrlAttribute (panelHandle, PANEL_OUTPUTNUMERIC, ATTR_FORMAT, VAL_BINARY_FORMAT);
SetCtrlAttribute (panelHandle, PANEL_OUTPUTNUMERIC, ATTR_FORMAT, VAL_OCTAL_FORMAT);
SetCtrlAttribute (panelHandle, PANEL_OUTPUTNUMERIC, ATTR_FORMAT, VAL_DECIMAL_FORMAT);
SetCtrlAttribute (panelHandle, PANEL_OUTPUTNUMERIC, ATTR_FORMAT, VAL_HEX_FORMAT);
If you want to create a string with the converted number, the are a couple of different things you can do.
You can convert a number to an octal or hex string using the ANSI C sprintf function. For example
int inputValue;
char outputString[64];
sprintf(outputString, "%X", inputValue); // creates a string in hex format
sprintf(outputString, "%o", inputValue); // creates a string in octal format
sprintf(outputString, "%d", inputValue); // creates a string in decimal format
There is not a similar format control for creating binary strings, so you'll need a function to do that. For example
int currentPosition = 0;
unsigned int inputValue;
char outputString[64];
// count the bits needed
do{
} while (inputValue >= pow(2,currentPosition++));
// show a minumum of 8 bits
if (currentPosition<8)
currentPosition=8;
// terminate the string
outputString[currentPosition] = '\0';
// fill in one bit at a time, from LSB to MSB
do{
if (inputValue & 1 << i++)
outputString[--currentPosition] = '1';
else
outputString[--currentPosition] = '0';
} while (currentPosition > 0);
Darnell posted some code using scanf and printf and asked how to display the results on his GUI.
Instead of using scanf, put an input control on your UIR and call GetCtrlVal()
Instead of using printf, put an output control on your UIR and call SetCtrlVal()
08-17-2009 03:31 AM
responding to AL S with the convert zip program, can someone add to this program or make a program this way
hi do you someone know how to
print out bits like this from 1-5
00000001=1
00000010=2
00000100=3
00001000=4
00010000=5
you can delcare them as a struct or
dont declare them at all but i just need to print out randomly from 1
to 5 at any given time.
so basically the program is random from 1-5 i want to be on a Gui
so i enter any number from 1-5 its going to print out above.
so for example if i enter 3 its going to print out 00000100. these are bits and not binary numbers. so what is the easy way to write this program
can come some write a simple project in CVI and post it, i want to see an easier way to do this. i am doing this because im going to be talking to
a device and the switches recognizes those bits.
, 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.
08-17-2009 04:07 AM
may i suggest you a couple of things ?
first, grab a good programming book and train yourself at programming. from your questions, it is clear that, while you are trying to, you do not have a clear understanding of what any program do.
try to solve simpler problems at start. if you can't write code to solve simple problems, it is very unlikely that you can solve a bigger problem. writing an attenuator may not be the right choice for a beginner's program.
forget about GUI. it may look nice but the programming model for a UI program is a little more complicated than a simple program: you have to understand what a callback is and some other concepts which cannot be grabbed without a thorough understanding of the execution path of a program. at first, use the console, gets() and printf() functions, which leads to a linear program.
last, you have to understand that most people here are professional software developpers: we are paid to write code. we come here and post for free, giving a little of our time to help people. we are not paid to do the job of a random stranger ! so stop asking us to write something for you: show us that you understand your problem, show us that you got your hands dirty, that you tried hard bute got stuck, post th code you write and then we will help you spot the problem.