|
|
View previous topic :: View next topic |
Author |
Message |
SkeeterHawk
Joined: 16 Oct 2010 Posts: 31 Location: Florissant, CO
|
Very general question on changing Variable types |
Posted: Sat Nov 10, 2012 10:42 pm |
|
|
Hello everyone.
I am pretty new to C and would appreciate a little bit of insight.
I am working with a system that is using RS-232 over a serial port using a FTDI RS232R chip, so this is pretty straight forward in hardware.
My question is in the type conversions. I am pretty clear on the Serial Port and the conversions there, but I am using the Flex_LCD driver from PCM Programmer to display what came over the serial port. This works great, but can I get some tips on where I can find information on converting from HEX to ASCII and back for this type of Function? Thanks so much for this elementary information.
Jonathan _________________ Life is too short to only write code in assembly... |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19561
|
|
Posted: Sun Nov 11, 2012 3:51 am |
|
|
Look at printf in the CCS manual. Third entry in it's 'syntax' summary:
"printf (fname, cstring, values...)"
and the comment below:
"fname is a function name to be used for outputting (default is putc is none is specified)."
printf, can be used to drive the LCD, just as it drives the serial port. So:
printf(lcd_putc,"HiThere");
Here 'fname' is 'lcd_putc' (the function to put a character to the LCD), and putc then behaves exactly as it would when using the serial port, except sending it's output to the LCD. So:
printf(lcd_putc,"%04x",var);
Will display 'var' using four hex digits, with leading zeros, on the LCD.
Best Wishes |
|
|
SkeeterHawk
Joined: 16 Oct 2010 Posts: 31 Location: Florissant, CO
|
|
Posted: Sun Nov 11, 2012 2:41 pm |
|
|
Thank you very much, Ttelmah.
I thought that I could do it with the printf function, but didn't know how. Thank you very much for your answer. That really helps. _________________ Life is too short to only write code in assembly... |
|
|
SkeeterHawk
Joined: 16 Oct 2010 Posts: 31 Location: Florissant, CO
|
|
Posted: Mon Nov 12, 2012 10:13 pm |
|
|
Along this same subject, if I have a 32-bit HEX value, how would I use printf to convert that into an ascii string? The following really simplified code doesn't work:
int32 Result;
Result = spi_xfer (Chamber,0,32); // Chamber is the Stream ID
printf ("%s", Result);
I'm sure that there is something really fundamental I am missing here. I sincerely appreciate the insight here to get this to output something more man-readable.
Also, do I need to send the "0" on the spi_xfer? I don't have a Dout pin. Just wondering...
Thanks so much, as always, for your insight and patience. _________________ Life is too short to only write code in assembly... |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19561
|
|
Posted: Tue Nov 13, 2012 2:38 am |
|
|
You need to get a basic C book. %s, _outputs_ a string value. ie., something already stored as a string. Result is a number. Nothing else. Everything printf outputs is ASCII text (not actually a 'string', since a string implies _null terminated_ in C).
Just print it (remember it is 'long').
so:
printf("%Lx",Result);
Yes, SPI is always bi-directional, even if you don't 'use' the second direction. The functions require the output data.
Best Wishes |
|
|
SkeeterHawk
Joined: 16 Oct 2010 Posts: 31 Location: Florissant, CO
|
|
Posted: Tue Nov 13, 2012 9:40 am |
|
|
Thanks again for your answer, Ttelmah.
By this point I have a pretty sizable stack of C books that I am referring to constantly, but none is very clear on what all printf can do. I can do this in assembly in a snap (though it would take time), but I am thinking that I can rely on printf to do all of these conversions for me. I guess not.
What I am wanting to do is to output the 32-bit number formatted in ASCII. In assembly, I would convert the whole thing to BCD and then link the BCD to the ASCII equivalent before pumping it over the UART. It seems that if you are just talking about one number or character, printf will actually do this for you, but it seems to be getting confused when working with a string (as you said it would).
I guess another thing that is confusing me a little is if I converted the number to BCD manually, I would potentially have a 10 digit number. How would I handle this in C as it seems to stop giving you options at 32 bits unless you are talking about a string (I think).
I'm going to review "The K&R Bible" (among others) yet again for variable types, and type conversions...but I would sincerely appreciate a veteran's practical outlook on such a fundamental part of this language. There's something not getting through my skull apparently.
Thanks, as always. _________________ Life is too short to only write code in assembly... |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19561
|
|
Posted: Tue Nov 13, 2012 9:49 am |
|
|
No, you are missing the point.
Printf _always_ outputs in ASCII.
You do not have a 'string', you have a number. Result is a number, not a string.
You _do not have a string, or need to ever get involved in a string_......
Strings in C, are null terminated character arrays containing ASCII values.
You do not _have_ such an array, hence the %s format won't work.
printf("%Lx",Result);
will print it out in ASCII. If you want to force this to be 8 hex digits, then:
printf("%08Lx",Result);
will always output four digits, with leading zeros.
Printf, _outputs_ in ASCII all the time. Nothing else.
Best Wishes |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|