]> rtime.felk.cvut.cz Git - arc.git/blob - common/xtoa.c
Merge with 224fd456bbd275e5c2666f008c2fccca241a38f5
[arc.git] / common / xtoa.c
1 /* -------------------------------- Arctic Core ------------------------------\r
2  * Arctic Core - the open source AUTOSAR platform http://arccore.com\r
3  *\r
4  * Copyright (C) 2009  ArcCore AB <contact@arccore.com>\r
5  *\r
6  * This source code is free software; you can redistribute it and/or modify it\r
7  * under the terms of the GNU General Public License version 2 as published by the\r
8  * Free Software Foundation; See <http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt>.\r
9  *\r
10  * This program is distributed in the hope that it will be useful, but\r
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
12  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License\r
13  * for more details.\r
14  * -------------------------------- Arctic Core ------------------------------*/\r
15 \r
16 /*\r
17  * Itoa based on K&R itoa\r
18  */\r
19 \r
20 \r
21 #include <stdlib.h>\r
22 \r
23 \r
24 \r
25 /**\r
26  * Convert a number to a string\r
27  *\r
28  * @param val           The value to convert\r
29  * @param str           Pointer to a space where to put the string\r
30  * @param base          The base\r
31  * @param negative      If negative or not.\r
32  */\r
33 void xtoa( unsigned long val, char* str, int base, int negative) {\r
34         int i;\r
35         char *oStr = str;\r
36         char c;\r
37 \r
38         if (negative) {\r
39                 val = -val;\r
40         }\r
41 \r
42         if( base < 10 && base > 16 ) {\r
43                 *str = '0';\r
44                 return;\r
45         }\r
46     i = 0;\r
47 \r
48     do {\r
49       str[i++] = "0123456789abcdef"[ val % base ];\r
50         } while ((val /= base) > 0);\r
51 \r
52 \r
53     if (negative) {\r
54         str[i++] = '-';\r
55     }\r
56 \r
57     str[i] = '\0';\r
58     str = &str[i]-1;\r
59     while(str > oStr) {\r
60         c = *str;\r
61         *str-- = *oStr;\r
62         *oStr++=c;\r
63     }\r
64 }\r
65 \r
66 \r
67 #if defined(TEST_XTOA)\r
68 /* Some very limited testing */\r
69 int main( void ) {\r
70         char str[20];\r
71 \r
72         xtoa(123,str,10,0);\r
73         printf("%s\n",str);\r
74         xtoa(-123,str,10,1);\r
75         printf("%s\n",str);\r
76         xtoa(0xa123,str,16,0);\r
77         printf("%s\n",str);\r
78         xtoa(-0xa123,str,16,1);\r
79         printf("%s\n",str);\r
80 \r
81 \r
82         return 0;\r
83 }\r
84 #endif\r
85 \r
86 \r
87 /**\r
88  * Converts an unsigned long to a string\r
89  *\r
90  * @param value  The value to convert\r
91  * @param str    Pointer to the string\r
92  * @param base   The base\r
93  */\r
94 void ultoa(unsigned long value, char* str, int base) {\r
95         xtoa(value, str, base, 0);\r
96 }\r
97 \r
98 /**\r
99  * Converts an integer to a string\r
100  *\r
101  * @param value The value to convert\r
102  * @param str   Pointer to the string to write to\r
103  * @param base  The base\r
104  */\r
105 char * itoa(int value, char* str, int base) {\r
106         xtoa(value, str, base, (value < 0));\r
107         return str;\r
108 }\r
109 \r