当前位置:网站首页>Conversion between integer and string in C language
Conversion between integer and string in C language
2022-07-25 18:06:00 【smile_ 5me】
There was a need to convert integers to strings , Refer to other great God blogs , Here is a record of , Easy to use at the back .
char* itoa(int value,char*string,int radix);//value: The integer to convert ,string: Converted String ,radix: Convert decimal numbers , Such as 2,8,10,16 Base system, etc char* itoa(int num,char* str,int radix)
{
char index[]="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";// Index table
unsigned unum;// Store the absolute value of the integer to be converted , The converted integer may be negative
int i=0,j,k;//i Used to indicate the corresponding bit of the set string , After the transformation i It's actually the length of the string ; The order after conversion is reverse , There are positive and negative situations ,k Used to indicate the start of the adjustment sequence ;j Used to indicate the exchange when adjusting the order .
// Get the absolute value of the integer to be converted
if(radix==10&&num<0)// To convert to decimal and negative
{
unum=(unsigned)-num;// take num The absolute value of is assigned to unum
str[i++]='-';// Set to... At the beginning of the string '-' Number , And index plus 1
}
else unum=(unsigned)num;// if num Being positive , Directly assign to unum
// Conversion part , Note that the conversion is in reverse order
do
{
str[i++]=index[unum%(unsigned)radix];// take unum Last digit of , And set to str Corresponding bit , Indicates the index plus 1
unum/=radix;//unum Get rid of the last one
}while(unum);// until unum by 0 Exit loop
str[i]='\0';// Add... At the end of the string '\0' character ,c Language string to '\0' end .
// Adjust the order
if(str[0]=='-') k=1;// If it's a negative number , The symbols don't have to be adjusted , Adjust from the back of the symbol
else k=0;// It's not a negative number , Everything has to be adjusted
char temp;// Temporary variable , When you swap two values, you use
for(j=k;j<=(i-1)/2;j++)// End to end symmetrical exchange ,i It's actually the length of the string , Index maximum is less than length 1
{
temp=str[j];// The header is assigned to a temporary variable
str[j]=str[i-1+k-j];// The tail is assigned to the head
str[i-1+k-j]=temp;// The value of the temporary variable ( It's actually the previous head value ) To the tail
}
return str;// Return the converted String
}int main(void)
{
int index = 100;
unsigned char num[10];
itoa(index,num,10);
printf("%s\n",num);
return 0;
}Reference blog :https://www.runoob.com/w3cnote/c-int2str.html
边栏推荐
- Drawing PDF tables (I) drawing excel table styles in PDF and downloading them through iText (supporting Chinese fonts)
- Could not stop Cortex-M device! please check the JTAG cable的解决办法
- C语言 cJSON库的使用
- C# Linq 去重&去重求和
- 越来越成熟的Rust,都应用了哪些场景呢?
- UnitTest框架应用
- P2P 之 UDP穿透NAT的原理与实现
- Cloud XR面临的问题以及Cloud XR主要应用场景
- Error when starting MySQL on Linux
- Optimistic lock pessimistic lock applicable scenario
猜你喜欢

Keil5 "loading PDSC debug description failed for STMicroelectronics stm32hxxxxxxx" solution

排序还需要了解的信息以及链表

Landmark buildings around the world
SQL optimizer parsing | youth training camp notes

SLA 、SLO & SLI

泛域名配置方法

Take you to a preliminary understanding of multiparty secure computing (MPC)
Principle and implementation of UDP penetration NAT in P2P

Cloud VR: the next step of virtual reality specialization

2022/7/23
随机推荐
Auditing相关注解
简述冒泡排序与快速排序
喜讯!瑞云科技被授予“海上扬帆”5G融合应用专委会成员单位
Talking about Devops monitoring, how does the team choose monitoring tools?
专访即构科技李凯:音视频的有趣、行业前沿一直吸引着我
STM32F105RBT6 内部flash调试
「行话」| 用DevOps高效交付游戏,是种什么体验?
Briefly describe synchronized and lock upgrade
Oracle导入出错:IMP-00038: 无法转换为环境字符集句柄
Hit the test site directly: summary of common agile knowledge points in PMP examination
New and malloc
Mock service Moco series (I) - introduction, first demo, get request, post request
PageHelper can also be combined with lambda expressions to achieve concise paging encapsulation
十九岁的总结
虚拟偶像代言产品出问题谁负责?
What is an IP SSL certificate and how to apply for it?
C语言 整数与字符串的相互转换
Joseph Ring problem
MySQL page lock
MySQL lost the previous 0 after the decimal number type select