lib/libc/itoa.c

本文介绍了一个自定义的itoa函数实现方法,该函数将整数转换为指定进制的字符串形式,并且包括了一个字符串反转函数。文章详细解释了函数的工作原理,包括进位计算、字符填充和字符串反转等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/* Copyright (c) 2012, The Linux Foundation. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * * Redistributions of source code must retain the above copyright
 *  notice, this list of conditions and the following disclaimer.
 *  * 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.
 *   * Neither the name of The Linux Foundation 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 "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
 * 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.
*/
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int
itoa(int num, unsigned char* str, int len, int base)
{
	int sum = num;
	int i = 0;
	int digit;
	if (len == 0)
		return -1;
	do
	{
		digit = sum % base;
		if (digit < 0xA)
			str[i++] = '0' + digit;
		else
			str[i++] = 'A' + digit - 0xA;
		sum /= base;
	}while (sum && (i < (len - 1)));
	if (i == (len - 1) && sum)
		return -1;
	str[i] = '\0';
	strrev(str);
	return 0;
}


void
strrev(unsigned char *str)
{
	int i;
	int j;
	unsigned char a;
	unsigned len = strlen((const char *)str);
	for (i = 0, j = len - 1; i < j; i++, j--)
	{
		a = str[i];
		str[i] = str[j];
		str[j] = a;
	}
}

 

报错如下:Requirement already satisfied: python-dateutil>=2.5.0 in d:\anaconda3\lib\site-packages (from pandas->datasets) (2.7.3) Requirement already satisfied: pytz>=2011k in d:\anaconda3\lib\site-packages (from pandas->datasets) (2018.5) Requirement already satisfied: six>=1.5 in d:\anaconda3\lib\site-packages (from python-dateutil>=2.5.0->pandas->datasets) (1.11.0) Building wheels for collected packages: safetensors Building wheel for safetensors (pyproject.toml) ... error error: subprocess-exited-with-error × Building wheel for safetensors (pyproject.toml) did not run successfully. │ exit code: 1 ╰─> [41 lines of output] Running `maturin pep517 build-wheel -i d:\anaconda3\python.exe --compatibility off` 馃嵐 Building a mixed python/rust project 馃敆 Found pyo3 bindings with abi3 support for Python 鈮\xa5 3.8 馃摗 Using build options features, bindings from pyproject.toml Compiling target-lexicon v0.12.16 Compiling once_cell v1.20.3 Compiling proc-macro2 v1.0.93 Compiling unicode-ident v1.0.17 Compiling autocfg v1.4.0 Compiling libc v0.2.170 Compiling serde v1.0.218 Compiling serde_json v1.0.139 Compiling heck v0.5.0 Compiling itoa v1.0.14 Compiling memchr v2.7.4 Compiling ryu v1.0.19 Compiling unindent v0.2.3 Compiling cfg-if v1.0.0 Compiling indoc v2.0.5 Compiling memmap2 v0.9.5 Compiling memoffset v0.9.1 Compiling quote v1.0.38 Compiling syn v2.0.98 Compiling pyo3-build-config v0.23.5 error: failed to run custom build command for `pyo3-build-config v0.23.5` Caused by: process didn't exit successfully: `C:\Users\LENOVO\AppData\Local\Temp\pip-install-ogzmgg8g\safetensors_50fcc1da09bc471f8c4dd918bb0737da\bindings\python\target\release\build\pyo3-build-config-5fdab4ccd5b5a8ac\build-script-build` (exit code: 1) --- stdout cargo:r
04-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值