Tizen Native API
Functions
Convert
Tools

Functions

int eina_convert_itoa (int n, char *s)
 Converts an integer number to a string in decimal base.
int eina_convert_xtoa (unsigned int n, char *s)
 Converts an integer number to a string in hexadecimal base.
int eina_convert_dtoa (double d, char *des)
 Converts a double to a string.
Eina_Bool eina_convert_atod (const char *src, int length, long long *m, long *e)
 Converts a string to a double.
int eina_convert_fptoa (Eina_F32p32 fp, char *des)
 Converts a 32.32 fixed point number to a string.
Eina_Bool eina_convert_atofp (const char *src, int length, Eina_F32p32 *fp)
 Converts a string to a 32.32 fixed point number.

This group discusses the functions that allow you to convert an integer or real numbers to a string or conversely.

To use these functions, you have to call eina_init() first, and then eina_shutdown() when eina is 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 buffer that is sufficiently large to store all the cyphers.

Here is an example for use:

 #include <stdlib.h>
 #include <stdio.h>

 #include <Eina.h>

 int main(void)
 {
    char tmp[128];

    if (!eina_init())
    {
        printf ("Error during the initialization of eina.\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_shutdown();

    return EXIT_SUCCESS;
 }

Compile this code with the following command:

 gcc -Wall -o test_eina_convert test_eina.c `pkg-config --cflags --libs eina`
Remarks:
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 has the following format (which is the result obtained with snprintf() and the 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 mantissa and exponent:

 mantiss  : [-]hhhhhh
 exponent : 2^([+-]e - 4 * n)

with n being the number of cyphers after the point in the string format. To obtain the double number from the mantissa and exponent, use ldexp().

Here is an example for use:

 #include <stdlib.h>
 #include <stdio.h>
 #include <math.h>

 #include <Eina.h>

 int main(void)
 {
    char      tmp[128];
    long long int m = 0;
    long int  e = 0;
    double    r;

    if (!eina_init())
    {
        printf ("Error during the initialization of eina.\n");
        return EXIT_FAILURE;
    }

    printf("initial value : 40.56\n");
    eina_convert_dtoa(40.56, tmp);
    printf("result dtoa   : %s\n", tmp);

    eina_convert_atod(tmp, 128, &m, &e);
    r = ldexp((double)m, e);
    printf("result atod   : %f\n", r);

    eina_shutdown();

    return EXIT_SUCCESS;
 }

Compile this code with the following command:

 gcc -Wall -o test_eina_convert test_eina.c `pkg-config --cflags --libs eina` -lm

Function Documentation

Eina_Bool eina_convert_atod ( const char *  src,
int  length,
long long *  m,
long *  e 
)

Converts a string to a double.

This function converts the string s of length length that represents a double in hexadecimal base to a double. It is used to replace the use of snprintf() with the modifier, which is missing on some platform (like Windows (tm) or OpenBSD).

Since :
2.3

The string must have the following format:

 [-]0xh.hhhhhp[+-]e

where h are the hexadecimal cyphers of the mantissa and e is the exponent (a decimal number). If n is the number of cyphers after the point, the returned mantissa and exponent are:

 mantiss  : [-]hhhhhh
 exponent : 2^([+-]e - 4 * n)

The mantissa and exponent are stored in the buffers pointed by m and e respectively.

If the string is invalid, the error is set to:

In those cases, EINA_FALSE is returned, otherwise EINA_TRUE is returned.

Parameters:
[in]srcThe string to convert
[in]lengthThe length of the string
[out]mThe mantissa
[out]eThe exponent
Returns:
EINA_TRUE on success, otherwise EINA_FALSE
Eina_Bool eina_convert_atofp ( const char *  src,
int  length,
Eina_F32p32 *  fp 
)

Converts a string to a 32.32 fixed point number.

This function converts the string src of length length that represents a double in hexadecimal base to a 32.32 fixed point number stored in fp. The function always tries to convert the string with eina_convert_atod().

The string must have the following format:

 [-]0xh.hhhhhp[+-]e

where h are the hexadecimal cyphers of the mantissa and e is the exponent (a decimal number). If n is the number of cyphers after the point, the returned mantissa and exponent are:

 mantiss  : [-]hhhhhh
 exponent : 2^([+-]e - 4 * n)

The mantissa and exponent are stored in the buffers pointed by m and e respectively.

Remarks:
If the string is invalid, the error is set to:

In those cases, or if fp is NULL, EINA_FALSE is returned, otherwise fp is computed and EINA_TRUE is returned.

Since :
2.3
Remarks:
The code uses eina_convert_atod() and does the correct bit shift to compute the fixed point number.
Parameters:
[in]srcThe string to convert
[in]lengthThe length of the string
[out]fpThe fixed point number
Returns:
EINA_TRUE on success, otherwise EINA_FALSE
int eina_convert_dtoa ( double  d,
char *  des 
)

Converts a double to a string.

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 null terminated and has the following format:

Since :
2.3
 [-]0xh.hhhhhp[+-]e

where h are the hexadecimal cyphers of the mantissa and e is the exponent (a decimal number).

Remarks:
The returned value is the length of the string, including the null terminated character.
Parameters:
[in]dThe double to convert
[in]desThe destination buffer to store the converted double
Returns:
EINA_TRUE on success, otherwise EINA_FALSE
int eina_convert_fptoa ( Eina_F32p32  fp,
char *  des 
)

Converts a 32.32 fixed point number to a string.

This function converts the 32.32 fixed point number fp to a string. The string is stored in the buffer pointed by des and must be sufficiently large to contain the converted fixed point number. The returned string is terminated and has the following format:

 [-]0xh.hhhhhp[+-]e

where h are the hexadecimal cyphers of the mantissa and e is the exponent (a decimal number).

Since :
2.3
Remarks:
The returned value is the length of the string, including the null terminating character.
The code is the same as eina_convert_dtoa() except that it implements the frexp() function for fixed point numbers and does some optimisations.
Parameters:
[in]fpThe fixed point number to convert
[in]desThe destination buffer to store the converted fixed point number
Returns:
EINA_TRUE on success, otherwise EINA_FALSE
int eina_convert_itoa ( int  n,
char *  s 
)

Converts an integer number to a string in decimal base.

This function converts n to a null 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.

Since :
2.3
Remarks:
The returned value is the length of the string, including the null terminated character.
Parameters:
[in]nThe integer to convert
[in]sThe buffer to store the converted integer
Returns:
The length of the string, including the null terminated character
int eina_convert_xtoa ( unsigned int  n,
char *  s 
)

Converts an integer number to a string in hexadecimal base.

This function converts n to a null 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.

Since :
2.3
Remarks:
The returned value is the length of the string, including the null terminated character.
Parameters:
[in]nThe integer to convert
[in]sThe buffer to store the converted integer
Returns:
The length of the string, including the null terminated character

Except as noted, this content - excluding the Code Examples - is licensed under Creative Commons Attribution 3.0 and all of the Code Examples contained herein are licensed under BSD-3-Clause.
For details, see the Content License