GeekDoc 226 Posted January 24, 2011 Author Share Posted January 24, 2011 Chugging along now, but hit a snag. Does anyone have a good routine to turn an unsigned int into its string equivalent (to send to LCD)? This routine throws errors in completely unrelated functions when I add it (commenting it out builds clean) :? /* Takes an int and returns a char[5] * a 16-bit unsigned int can be a max of 5 characters (65535) */ char[] int_to_str(unsigned int i) { char[5] the_str; int str_pos = 0; //parse into char[] for(str_pos = 0; str_pos < 4; str_pos++) { the_str[str_pos] = i / (10^(4 - str_pos)); i -= i / (10^(4 - str_pos)); } return the_str; } Quote Link to post Share on other sites
zeke 693 Posted January 24, 2011 Share Posted January 24, 2011 I use sprintf() to do this. Here's an example that I ripped off the net somewhere: #include // ^ must include to use sprintf() // we will start with a simple integer ex. int main() { char buff[50]; int ret, a = 34, b = 234; ret = sprintf(buff, "%d minus %d equals %d",a,b,a-; printf ("(%s) is the result of our sprintf, which is %d characters long",buff,ret); return 0; } Instead of a real string, you could just put your number in there somewhere like this: ret = sprintf( buff, "%d", youruint); Does that help? Quote Link to post Share on other sites
zeke 693 Posted January 24, 2011 Share Posted January 24, 2011 I just noticed that in the ctype.h library, there is a function called toascii(). See if that might help you. Quote Link to post Share on other sites
zeke 693 Posted January 24, 2011 Share Posted January 24, 2011 Yeah, in the ctype.h file you should find this macro: #define _toascii(a) ((a) & 0x7F) That should convert an integer to ASCII. Helpful? Quote Link to post Share on other sites
cde 334 Posted January 24, 2011 Share Posted January 24, 2011 I think Geekdoc needs something that turns the number "234" into the string "234", instead of number "234" into ascii char 234 which is " Quote Link to post Share on other sites
RobG 1,892 Posted January 24, 2011 Share Posted January 24, 2011 How about something like this: //assume data is an array of your chars and myInt is the int you want to convert //0x30 is where numbers start in ASCII unsinged int charPos = 0; while(myInt > 0) { data[charPos] = 0x30 + (myInt % 10); myInt /= 10; charPos++; } Quote Link to post Share on other sites
GeekDoc 226 Posted January 24, 2011 Author Share Posted January 24, 2011 I think Geekdoc needs something that turns the number "234" into the string "234", instead of number "234" into ascii char 234 which is " Quote Link to post Share on other sites
GeekDoc 226 Posted January 24, 2011 Author Share Posted January 24, 2011 How about something like this: //assume data is an array of your chars and myInt is the int you want to convert //0x30 is where numbers start in ASCII unsinged int charPos = 0; while(myInt > 0) { data[charPos] = 0x30 + (myInt % 10); myInt /= 10; charPos++; } I started that way, but it will reverse the digits: 2345 % 10 = 5 234 % 10 = 4 ... Going to check through libraries. This is a commonly needed function, so it should be there somewhere. Quote Link to post Share on other sites
RobG 1,892 Posted January 24, 2011 Share Posted January 24, 2011 Well, then you can pass your first position in the array, for example charPos = 10, and then work your way back, charPos--; Quote Link to post Share on other sites
GeekDoc 226 Posted January 24, 2011 Author Share Posted January 24, 2011 Well, then you can pass your first position in the array, for example charPos = 10, and then work your way back, charPos--; Thought of that, too. Problem is that it requires knowing the number of digits in the supplied int, or having a fixed size for the string. As with most programming problems, someone has solved this already. I just have to find where (libraries). Quote Link to post Share on other sites
RobG 1,892 Posted January 24, 2011 Share Posted January 24, 2011 How about this then, a function that will determine the size of the array if(i<10) arraySize=1 else if(i<100) arraySize=2 and so on until you reach max int size. Another way is to rebuild your initial array afterward since you have the size. unsinged int charPos = 0; while(myInt > 0) { data[charPos] = 0x30 + (myInt % 10); myInt /= 10; charPos++; } unsigend int size = charPos; unsigned int reversed[size]; while(charPos >0){ charPos--; reversed[charPos] = data[size-1-charPos]; } Quote Link to post Share on other sites
cde 334 Posted January 24, 2011 Share Posted January 24, 2011 Counting number of digits in an int. http://stackoverflow.com/questions/5545 ... tring-cast Since we know its an unsigned 16bit int, so a number ranging from 0 to 65535 const unsigned int test[] = { 1, 10, 100, 1000, 10000 }; unsigned int digits = 0; while(importantnumber >= test[digits]) ++digits; Digits will be 0 for an int with value 0 (or nll or unintialized?) while 1 for 1-9, 2 for 10-99, etc. RobG 1 Quote Link to post Share on other sites
RobG 1,892 Posted January 25, 2011 Share Posted January 25, 2011 Awesome! Quote Link to post Share on other sites
GeekDoc 226 Posted January 25, 2011 Author Share Posted January 25, 2011 cde and Google to the rescue! Turns out that this (sprintf()) is not strictly in the C/C++ language, but is widely implemented in stdio libraries. Also, adding a couple of global char arrays (strings) made things a lot easier. Easy, 4-line solution: char* int_to_str(unsigned int i, char* the_str) { //sprintf() returns # chars, but this does not currently use the info int total_chars; total_chars=sprintf(the_str, "%d", i); return the_str; } Result: On to capturing and working with the inputs! EDIT: Just noticed I'm up to 1.8K! Going to have to a) hope the rest of the code doesn't add much, b)find ways to trim size, or c) wait for newer, higher capacity value-line chips (still waiting on samples). Probably will go with combo of a) and c). bluehash 1 Quote Link to post Share on other sites
bluehash 1,581 Posted January 25, 2011 Share Posted January 25, 2011 Looks like good progress! Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.