Convert
[Tools]
These functions allow you to convert integer or real numbers to string or conversely. More...
Functions | |
EAPI int | eina_convert_init (void) |
Initialize the eina convert internal structure. | |
EAPI int | eina_convert_shutdown (void) |
Shut down the eina convert internal structures. | |
EAPI int | eina_convert_itoa (int n, char *s) |
Convert an integer number to a string in decimal base. | |
EAPI int | eina_convert_xtoa (unsigned int n, char *s) |
Convert an integer number to a string in hexadecimal base. | |
EAPI int | eina_convert_dtoa (double d, char *des) |
Convert a double to a string. | |
EAPI Eina_Bool | eina_convert_atod (const char *src, int length, long long *m, long *e) |
Convert a string to a double. | |
Variables | |
EAPI Eina_Error | EINA_ERROR_CONVERT_P_NOT_FOUND |
Error identifier corresponding to string not containing 'p'. | |
EAPI Eina_Error | EINA_ERROR_CONVERT_0X_NOT_FOUND |
Error identifier corresponding to string not containing '0x'. | |
EAPI Eina_Error | EINA_ERROR_CONVERT_OUTRUN_STRING_LENGTH |
Error identifier corresponding to length of the string being too small. |
Detailed Description
These functions allow you to convert integer or real numbers to string or conversely.
To use these function, you have to call eina_convert_init() first, and eina_convert_shutdown() when they are not used anymore.
Conversion from integer to string
To convert an integer to a string in the decimal base, eina_convert_itoa() should be used. If the hexadecimal base is wanted, eina_convert_xtoa() should be used. They all need a bufffer sufficiently large to store all the cyphers.
Here is an exemple of use:
#include <stdlib.h> #include <stdio.h> #include <eina_convert.h> int main(void) { char *tmp[128]; if (!eina_convert_init()) { printf ("Error during the initialization of eina_convert module\n"); return EXIT_FAILURE; } eina_convert_itoa(45, tmp); printf("value: %s\n", tmp); eina_convert_xtoa(0xA1, tmp); printf("value: %s\n", tmp); eina_convert_shutdown(); return EXIT_SUCCESS; }
Compile this code with the following commant:
gcc -Wall -o test_eina_convert test_eina.c `pkg-config --cflags --libs eina`
- Note:
- The alphabetical cyphers are in lower case.
Conversion double / string
To convert a double to a string, eina_convert_dtoa() should be used. Like with the integer functions, a buffer must be used. The resulting string ghas the following format (which is the result obtained with snprintf() and the %a modifier):
[-]0xh.hhhhhp[+-]e
To convert a string to a double, eina_convert_atod() should be used. The format of the string must be as above. Then, the double has the following mantiss and exponent:
mantiss : [-]hhhhhh exponent : 2^([+-]e - 4 * n)
with n being number of cypers after the point in the string format. To obtain the double number from the mantiss and exponent, use ldexp().
Here is an exemple of use:
#include <stdlib.h> #include <stdio.h> #include <eina_convert.h> int main(void) { char *tmp[128]; long long int m = 0; long int e = 0; doule r; if (!eina_convert_init()) { printf ("Error during the initialization of eina_convert module\n"); return EXIT_FAILURE; } eina_convert_dtoa(40.56, tmp); printf("value: %s\n", tmp); eina_convert_atod(tmp, 128, &m, &e); r = ldexp((double)m, e); printf("value: %s\n", tmp); eina_convert_shutdown(); return EXIT_SUCCESS; }
Compile this code with the same command as above.
Function Documentation
EAPI int eina_convert_init | ( | void | ) |
Initialize the eina convert internal structure.
- Returns:
- 1 or greater on success, 0 on error.
This function sets up the error module of Eina and registers the errors EINA_ERROR_CONVERT_0X_NOT_FOUND, EINA_ERROR_CONVERT_P_NOT_FOUND and EINA_ERROR_CONVERT_OUTRUN_STRING_LENGTH. It is also called by eina_init(). It returns 0 on failure, otherwise it returns the number of times it has already been called.
References eina_error_init(), and eina_error_msg_register().
EAPI int eina_convert_shutdown | ( | void | ) |
Shut down the eina convert internal structures.
- Returns:
- 0 when the convert module is completely shut down, 1 or greater otherwise.
This function just shuts down the error module. It is also called by eina_shutdown(). It returns 0 when it is called the same number of times than eina_convert_init().
References eina_error_shutdown().
EAPI int eina_convert_itoa | ( | int | n, | |
char * | s | |||
) |
Convert an integer number to a string in decimal base.
- Parameters:
-
n The integer to convert. s The buffer to store the converted integer.
- Returns:
- The length of the string, including the nul terminated character.
This function converts n
to a nul terminated string. The converted string is in decimal base. As no check is done, s
must be a buffer that is sufficiently large to store the integer.
The returned value is the length os the string, including the nul terminated character.
Referenced by eina_convert_dtoa().
EAPI int eina_convert_xtoa | ( | unsigned int | n, | |
char * | s | |||
) |
Convert an integer number to a string in hexadecimal base.
- Parameters:
-
n The integer to convert. s The buffer to store the converted integer.
- Returns:
- The length of the string, including the nul terminated character.
This function converts n
to a nul terminated string. The converted string is in hexadecimal base and the alphabetical cyphers are in lower case. As no check is done, s
must be a buffer that is sufficiently large to store the integer.
The returned value is the length os the string, including the nul terminated character.
EAPI int eina_convert_dtoa | ( | double | d, | |
char * | des | |||
) |
Convert a double to a string.
- Parameters:
-
d The double to convert. des The destination buffer to store the converted double.
- Returns:
- EINA_TRUE on success, EINA_FALSE otherwise.
This function converts the double d
to a string. The string is stored in the buffer pointed by des
and must be sufficiently large to contain the converted double. The returned string is terminated and has the following format:
[-]0xh.hhhhhp[+-]e
where the h are the hexadecimal cyphers of the mantiss and e the exponent (a decimal number).
The returned value is the length of the string, including the nul character.
References eina_convert_itoa(), and EINA_FALSE.
EAPI Eina_Bool eina_convert_atod | ( | const char * | src, | |
int | length, | |||
long long * | m, | |||
long * | e | |||
) |
Convert a string to a double.
- Parameters:
-
src The string to convert. length The length of the string. m The mantisse. e The exponent.
- Returns:
- EINA_TRUE on success, EINA_FALSE otherwise.
This function converts the string s
of length length
that represent a double in hexadecimal base to a double. It is used to replace the use of snprintf() with the %a modifier, which is missing on some platform (like Windows (tm) or OpenBSD).
The string must have the following format:
[-]0xh.hhhhhp[+-]e
where the h are the hexadecimal cyphers of the mantiss and e the exponent (a decimal number). If n is the number of cypers after the point, the returned mantiss and exponents are:
mantiss : [-]hhhhhh exponent : 2^([+-]e - 4 * n)
The mantiss and exponent are stored in the buffers pointed respectively by m
and e
.
If the string is invalid, the error is set to:
- EINA_ERROR_CONVERT_0X_NOT_FOUND if no 0x is found,
- EINA_ERROR_CONVERT_P_NOT_FOUND if no p is found,
- EINA_ERROR_CONVERT_OUTRUN_STRING_LENGTH if
length
is not correct.
In those cases, EINA_FALSE is returned, otherwise EINA_TRUE is returned.
References EINA_ERROR_PDBG, eina_error_set(), EINA_FALSE, and EINA_TRUE.