The Hackerlab at `regexps.com`

## Converting Between Numbers and Strings

up: libhackerlab next: Unicode prev: Eight-bit Characters and Strings

The functions in this chapter convert between numbers and ASCII strings.

NOTE: Floating point conversions are not included with this release of the Hackerlab C Library. They will be included in a future release.

### Converting Integers to and from Strings

The functions in this chapter convert between integer types and strings.

- Converting Integers to and from Strings
- Converting Integers to Strings
- Converting Decimal Strings to Integers
- Converting Hexadecimal Strings to Integers
- Converting Octal Strings to Integers

#### Converting Integers to Strings

up: Converting Between Numbers and Strings next: Converting Decimal Strings to Integers prev: Converting Integers to and from Strings

Function `cvt_ulong_to_decimal`

    void cvt_ulong_to_decimal (t_uchar * nbuf, unsigned long n);

Convert `n` to a 0-terminated decimal number.

Function `cvt_long_to_decimal`

    void cvt_long_to_decimal (t_uchar * nbuf, long n);

Convert `n` to a 0-terminated decimal number.

Function `cvt_ulong_to_octal`

    void cvt_ulong_to_octal (t_uchar * nbuf, unsigned long n);

Convert `n` to a 0-terminated octal number.

Function `cvt_long_to_octal`

    void cvt_long_to_octal (t_uchar * nbuf, long n);

Convert `n` to a 0-terminated octal number.

Function `cvt_ulong_to_HEX`

    void cvt_ulong_to_HEX (t_uchar * nbuf, unsigned long n);

Convert `n` to a 0-terminated hexadecimal number using upper-case hex digits A..F .

Function `cvt_long_to_HEX`

    void cvt_long_to_HEX (t_uchar * nbuf, long n);

Convert `n` to a 0-terminated hexadecimal number using upper-case hex digits A..F .

Function `cvt_ulong_to_hex`

    void cvt_ulong_to_hex (t_uchar * nbuf, unsigned long n);

Convert `n` to a 0-terminated hexadecimal number using lower-case hex digits a..f .

Function `cvt_long_to_hex`

    void cvt_long_to_hex (t_uchar * nbuf, long n);

Convert `n` to a 0-terminated hexadecimal number using lower-case hex digits a..f .

#### Converting Decimal Strings to Integers

up: Converting Between Numbers and Strings next: Converting Hexadecimal Strings to Integers prev: Converting Integers to Strings

Function `cvt_decimal_to_ulong`

    int cvt_decimal_to_ulong (int * errn,
                              unsigned long * answerp,
                              const t_uchar * text,
                              size_t len);

Convert the decimal number `text` to an unsigned long integer.

Return `0` upon success, `-1` on error.

On error, `*errn` may be:

         ERANGE  the decimal number will not fit in an `unsigned long'.
         EINVAL  `text' is not a valid decimal number.

Function `cvt_decimal_to_uint`

    int cvt_decimal_to_uint (int * errn,
                             unsigned int * answerp,
                             const t_uchar * text,
                             size_t len);

Convert the decimal number `text` to an unsigned integer.

Return `0` upon success, `-1` on error.

On error, `*errn` may be:

         ERANGE  the decimal number will not fit in an `unsigned int'.
         EINVAL  `text' is not a valid decimal number.

Function `cvt_decimal_to_long`

    int cvt_decimal_to_long (int * errn,
                             long * answerp,
                             const t_uchar * text,
                             size_t len);

Convert the decimal number `text` to a long integer.

Return `0` upon success, `-1` on error.

On error, `*errn` may be:

         ERANGE          the decimal number will not fit in a `long'.
         EINVAL          `text' is not a valid decimal number.

Function `cvt_decimal_to_int`

    int cvt_decimal_to_int (int * errn,
                            int * answerp,
                            const t_uchar * text,
                            size_t len);

Convert the decimal number `text` to an integer.

Return `0` upon success, `-1` on error.

On error, `*errn` may be:

         ERANGE          the decimal number will not fit in an `int'.
         EINVAL          `text' is not a valid decimal number.

#### Converting Hexadecimal Strings to Integers

up: Converting Between Numbers and Strings next: Converting Octal Strings to Integers prev: Converting Decimal Strings to Integers

Function `cvt_hex_to_ulong`

    int cvt_hex_to_ulong (int * errn,
                          unsigned long * answerp,
                          const t_uchar * text,
                          size_t len);

Convert the hexadecimal number `text` to an unsigned long integer.

Return `0` upon success, `-1` on error.

On error, `*errn` may be:

         ERANGE  the hexadecimal number will not fit in an `unsigned long'.
         EINVAL  `text' is not a valid hexadecimal number.

Function `cvt_hex_to_uint`

    int cvt_hex_to_uint (int * errn,
                         unsigned int * answerp,
                         const t_uchar * text,
                         size_t len);

Convert the hexadecimal number `text` to an unsigned integer.

Return `0` upon success, `-1` on error.

On error, `*errn` may be:

         ERANGE  the hexadecimal number will not fit in an `unsigned int'.
         EINVAL  `text' is not a valid hexadecimal number.

Function `cvt_hex_to_long`

    int cvt_hex_to_long (int * errn,
                         long * answerp,
                         const t_uchar * text,
                         size_t len);

Convert the hexadecimal number `text` to a long integer.

Return `0` upon success, `-1` on error.

On error, `*errn` may be:

         ERANGE          the hexadecimal number will not fit in a `long'.
         EINVAL          `text' is not a valid hexadecimal number.

Function `cvt_hex_to_int`

    int cvt_hex_to_int (int * errn,
                        int * answerp,
                        const t_uchar * text,
                        size_t len);

Convert the hexadecimal number `text` to an integer.

Return `0` upon success, `-1` on error.

On error, `*errn` may be:

         ERANGE          the hexadecimal number will not fit in an `int'.
         EINVAL          `text' is not a valid hexadecimal number.

#### Converting Octal Strings to Integers

up: Converting Between Numbers and Strings prev: Converting Hexadecimal Strings to Integers

Function `cvt_octal_to_ulong`

    int cvt_octal_to_ulong (int * errn,
                            unsigned long * answerp,
                            const t_uchar * text,
                            size_t len);

Convert the octal number `text` to an unsigned long integer.

Return `0` upon success, `-1` on error.

On error, `*errn` may be:

         ERANGE  the octal number will not fit in an `unsigned long'.
         EINVAL  `text' is not a valid octal number.

Function `cvt_octal_to_uint`

    int cvt_octal_to_uint (int * errn,
                             unsigned int * answerp,
                             const t_uchar * text,
                             size_t len);

Convert the octal number `text` to an unsigned integer.

Return `0` upon success, `-1` on error.

On error, `*errn` may be:

         ERANGE  the octal number will not fit in an `unsigned int'.
         EINVAL  `text' is not a valid octal number.

Function `cvt_octal_to_long`

    int cvt_octal_to_long (int * errn,
                             long * answerp,
                             const t_uchar * text,
                             size_t len);

Convert the octal number `text` to a long integer.

Return `0` upon success, `-1` on error.

On error, `*errn` may be:

         ERANGE          the octal number will not fit in a `long'.
         EINVAL          `text' is not a valid octal number.

Function `cvt_octal_to_int`

    int cvt_octal_to_int (int * errn,
                            int * answerp,
                            const t_uchar * text,
                            size_t len);

Convert the octal number `text` to an integer.

Return `0` upon success, `-1` on error.

On error, `*errn` may be:

         ERANGE          the octal number will not fit in an `int'.
         EINVAL          `text' is not a valid octal number.

libhackerlab: The Hackerlab C Library
The Hackerlab at `regexps.com`