2019-02-07 05:50:52 +08:00
|
|
|
/****************************************************************************
|
2019-02-18 02:16:28 +08:00
|
|
|
* libs/libc/stdio/lib_ultoa_invert.c
|
2019-02-07 05:50:52 +08:00
|
|
|
*
|
2024-09-25 20:05:00 +08:00
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
* SPDX-FileCopyrightText: 2017, Keith Packard. All rights reserved.
|
2019-02-07 05:50:52 +08:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-02-18 02:16:28 +08:00
|
|
|
#include "lib_ultoa_invert.h"
|
2019-02-07 05:50:52 +08:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-07-14 01:52:48 +08:00
|
|
|
#ifdef CONFIG_LIBC_LONG_LONG
|
2019-02-16 09:10:40 +08:00
|
|
|
FAR char *__ultoa_invert(unsigned long long val, FAR char *str, int base)
|
|
|
|
#else
|
2019-02-07 05:50:52 +08:00
|
|
|
FAR char *__ultoa_invert(unsigned long val, FAR char *str, int base)
|
2019-02-16 09:10:40 +08:00
|
|
|
#endif
|
2019-02-07 05:50:52 +08:00
|
|
|
{
|
|
|
|
int upper = 0;
|
|
|
|
|
|
|
|
if (base & XTOA_UPPER)
|
|
|
|
{
|
|
|
|
upper = 1;
|
|
|
|
base &= ~XTOA_UPPER;
|
|
|
|
}
|
2021-04-02 13:44:54 +08:00
|
|
|
|
2019-02-07 05:50:52 +08:00
|
|
|
do
|
|
|
|
{
|
|
|
|
int v;
|
|
|
|
|
|
|
|
v = val % base;
|
|
|
|
val = val / base;
|
|
|
|
|
|
|
|
if (v <= 9)
|
|
|
|
{
|
|
|
|
v += '0';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (upper)
|
|
|
|
v += 'A' - 10;
|
|
|
|
else
|
|
|
|
v += 'a' - 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
*str++ = v;
|
|
|
|
}
|
|
|
|
while (val);
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|