当前位置:网站首页>C language (Itoa function)
C language (Itoa function)
2022-07-22 20:37:00 【An Ruoxi~】
itoa Function is to convert a number into its corresponding hexadecimal number format
for example -10 To 10 Base number -10
4 To 2 Base number 100
The main idea is The only special case is the decimal form of negative numbers , As long as it is specially treated
The method of finding Radix is generally rolling division ( Be careful : The data obtained here is inverse , Need inverse )
#include<stdio.h>
#include<assert.h>
char* my_itoa(int value,char* buff,unsigned int radix) {
assert(buff!=NULL&&radix>1);// Assertion buff It is not empty and the decimal number is greater than 1
char index[] = "0123456789abcdefghijklmnopqrstuvwxyz";
// Define character groups , It is used to represent various numbers
unsigned int temp = (unsigned int)value;// There will be a sign value Value is strongly converted to unsigned
int flag = value < 0 && radix == 10 ? 1 : 0;// Judge whether it is a decimal system ( Special treatment )
if (flag == 1) {// It is judged to be decimal and negative
buff[0] = '-';// The first element is a negative sign
temp *= -1;// take value Become a positive number
}
int i = flag;
for (; temp != 0;i++) {
buff[i] = index[temp % radix];
temp /= radix;
}
buff[i] = '\0';
// Because the reverse order is obtained by using the rolling division , So you need to reverse the entire array
for (int j = 0; j < (i - flag)/2;j++) {
int temp = buff[j+flag];
buff[j + flag] = buff[i - 1 - j];
buff[i - 1 - j] = temp;
}
return buff;
}
int main() {
char buff[256];
char* res = my_itoa(11,buff,16);
printf("%s\n",res);
return 0;
}
The test case :( The first number is the number to be transferred , The second number is a base number )
边栏推荐
- 模拟学生信息输入界面
- JSON output to file line by line in format
- LeetCode32——next permutation
- c语言bitField
- LeetCode53——Maximum Subarray——3 different methods
- 1080 Graduate Admission (30 分)
- 1049 counting ones (30 points)
- DETR 论文精读,并解析模型结构
- Chen Danqi: I hope girls can get more opportunities, and the gap between boys and girls will gradually disappear
- Kubernetes基础
猜你喜欢
docker搭建mysql主从复制
[literature reading and thought notes 13] pre trained image processing transformer
C语言(itoa函数)
[literature reading and thought notes 14] beyond a Gaussian noise: residual learning of deep CNN for image recognition
Redis亿级数据存储方案哈希槽分区
Kubernets原理分解
Pytoch sets different learning rates at different levels
Kubernetes基础
本地镜像发布到私有库
Leetcode0002——Add Two Numbers——Linked List
随机推荐
Kubernetes基础入门
1076 forwards on Weibo (30 points)
Her power series 6 - Yang Di 1: when the girl grows up, she can be good at mathematics and chemistry, and scientific research can be very fresh
sql优化技巧及注意事项
【文献阅读与想法笔记13】Pre-Trained Image Processing Transformer
7-1 fake news (20 points)
YOLO v1、v2、v3
1068 find more coins (30 points)
Pat-2021 winter exam (Full Score)
1091 Acute Stroke (30 分)
JNI data type usage
Aminer paper recommendation
1034 head of a gang (30 points)
1057 Stack (30 分)
Pytorch实现网络部分层的固定不进行回传更新
她力量系列四丨读博6年两次换导师,靠一点点“倔”,俞舟成为social chatbot的开拓者之一
LeetCode103——zigzagLevelOrder of binary tree
redission看门狗实现过程详解
DETR 论文精读,并解析模型结构
她力量系列七丨兰艳艳:理想温暖10年科研路,女性可以柔和,更要自信、专业 | 妇女节特辑