Last active
August 29, 2015 14:12
-
-
Save rageandqq/2ab31c71e5c2185e20d2 to your computer and use it in GitHub Desktop.
Pebble - Integer to Char Array (i.e. String)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Pebble Doesn't support the default itoa included in the standard library. | |
Here is a working implementation, allowing you to easily use an integer where a "string" is needed. | |
Added from : http://forums.getpebble.com/discussion/comment/29591/ | |
Written by Pebble Forum User: FlashBIOS | |
Gathered and Edited by: Sameer Chitley | |
NOTE: It seems like it only supports positive numbers, so be sure to pass in the absolute value. | |
*/ | |
char *itoa(int num) | |
{ | |
static char buff[20] = {}; | |
int i = 0, temp_num = num, length = 0; | |
char *string = buff; | |
if(num >= 0) { //See NOTE | |
// count how many characters in the number | |
while(temp_num) { | |
temp_num /= 10; | |
length++; | |
} | |
// assign the number to the buffer starting at the end of the | |
// number and going to the begining since we are doing the | |
// integer to character conversion on the last number in the | |
// sequence | |
for(i = 0; i < length; i++) { | |
buff[(length-1)-i] = '0' + (num % 10); | |
num /= 10; | |
} | |
buff[i] = '\0'; // can't forget the null byte to properly end our string | |
} | |
else { | |
return "Unsupported Number"; | |
} | |
return string; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment