Home
Monday, May 29, 2017
4:41:47 AM
Users online: 0   You are here >> Home > Programming

Forums | Programming Forums search
Forum FAQ
   
  1  
how do I convert a number into text (in c)
slimdog360 
16/8/08 3:08:11 PM
Overlord

I want to take the speed, torque among other things of a motor and spit them out into a little LCD.

eg
speed =200; <--number
*magic happens*
which produces the result
textspeed = "200" <--LCD readable form

its in c so obviously the "200" will be more like char[]={2,0,0}. But you get what I mean, I just want to go from a floating point precision number to text so I can feed that into the LCD.

It seems dumb but I cant think of anything.


Edited by slimdog360: 16/8/2008 3:08:41 PM

-----
Quote by Sir_Substance
engineering<---------------width of universe-------------->philosophy



zephyr 
16/8/08 3:54:00 PM
Hero
Titan


 
int speed = 200;
int len = 4; // number of characters + 1 for NULL terminator
char* textspeed = malloc(len * sizeof(char)); // allocate memory
memset(textspeed, NULL, len); // init it
sprintf(textspeed , "%d", speed); // print speed as an integer into textspeed

-----
The most likely way for the world to be destroyed, most experts agree, is by accident. That's where we come in. We're computer professionals. We cause accidents. (N. Borenstein)

Athiril 
16/8/08 4:39:26 PM
Titan

zephyr's solution works if you change one line

 
char* textspeed=(char*) malloc (len * sizeof(char)); // allocate memory



edit: also can use itoa (depending)
 
int speed = 200;
int len = 4; // number of characters + 1 for NULL terminator
char* textspeed = (char*) malloc (len * sizeof(char)); // allocate memory
itoa(speed,textspeed,10);






Edited by Athiril: 16/8/2008 04:45:19 PM

-----
North Coast NSW Photo Community Forums
http://photodan.com.au/forums/

zephyr 
16/8/08 8:44:57 PM
Hero
Titan


Quote by Athiril
zephyr's solution works if you change one line



not necessary. Read: http://www.stanford.edu/~blp/writings/clc/malloc-cast.html

Also, itoa is not part of ANSI C.


Edited by zephyr: 16/8/2008 8:50:08 PM

-----
The most likely way for the world to be destroyed, most experts agree, is by accident. That's where we come in. We're computer professionals. We cause accidents. (N. Borenstein)

freespace 
16/8/08 11:00:20 PM
Hero
Titan


Indeed, as zephyr pointed out, DO NOT CAST MALLOC!

If one _had_ to comment on zephr's solution (:P) then..

* sizeof(char) is redundant, since sizeof(char) by definition is always 1.

* Instead of using magic-number-3, you can work out the number of digits you need, then add 1 to it, and malloc that many characters. Do this by doing log(number)/log(10)+2.

* Also fairly sure the memset is not required, since sprintf will put in a \0 for you. Purists will also insist you use snprintf not just sprintf.

But really, that it just being pedantic :-)

Cheers,
Steve


Edited by freespace: 16/8/2008 11:08:10 PM

-----
By perseverance the snail reached the ark.

http://www.shuningbian.net - blog
http://anonshare.pictorii.com - share files anonymously
http://dailydiscovery.b3ta.org - learn something new
its f reespace damn it!

Athiril 
17/8/08 2:12:02 AM
Titan

Quote by zephyr
Quote by Athiril
zephyr's solution works if you change one line



not necessary. Read: http://www.stanford.edu/~blp/writings/clc/malloc-cast.html

Also, itoa is not part of ANSI C.


Edited by zephyr: 16/8/2008 8:50:08 PM



So how do you get the code to compile, avoiding the invalid conversion from char to void?

-----
North Coast NSW Photo Community Forums
http://photodan.com.au/forums/

Redhatter 
17/8/08 9:06:25 AM
Hero
Titan


Quote by Athiril
So how do you get the code to compile, avoiding the invalid conversion from char to void?



Use a non-retarded C compiler that understands the difference. ;-)

gcc seems to work with the above code.

-----
Stuart Longland (aka. Redhatter, VK4FSJL)
I haven't lost my mind it's backed up on a tape somewhere...
http://atomicdoc.yi.org <-- AtomicDOC Wiki
Resident Coolie-hatted Gentoo geek. (Gentoo MIPS & Mozilla herd member)

zephyr 
17/8/08 10:20:23 AM
Hero
Titan


Quote by freespace Purists will also insist you use snprintf not just sprintf.

But really, that it just being pedantic :-)



well, as an openBSD user, i really have no excuse :P

-----
The most likely way for the world to be destroyed, most experts agree, is by accident. That's where we come in. We're computer professionals. We cause accidents. (N. Borenstein)

slimdog360 
18/8/08 2:02:40 PM
Overlord

thanks guys

-----
Quote by Sir_Substance
engineering<---------------width of universe-------------->philosophy



  1  
Forums | Programming