一.将字符串转化为对应的数值
/*=============================================================================
#
# FileName: stringToNo.c
# Algorithm: 将字符串转化为对应的整型数值
# Author: Knife
# Created: 2014-06-22 20:37:36
#
=============================================================================*/
#include<stdio.h>
#include<string.h>
int atoi(char* s){
int i, n;
n = 0;
for(i = 0; s[i] >= '0' && s[i] <='9'; ++i){
n = 10 * n + (s[i] - '0');
}
return n;
}
void main(){
char charArr[] = "4562";
int number = atoi(charArr);
printf("%d
",number);
}
二.将数值转化成字符串
/*=============================================================================
#
# FileName: noToString.c
# Algorithm: 将整型数值转化成字符串
# Author: Knife
# Created: 2014-06-22 20:37:36
#
=============================================================================*/
#include<stdio.h>
#include<string.h>
#define MAX 100
//字符串逆转
void reverse(char* charArr){
int j, k, tmp;
j = 0;
k = strlen(charArr)-1;
for(; j < k; j++, k--){
tmp = charArr[j];
charArr[j] = charArr[k];
charArr[k] = tmp;
}
}
void itoa(int n, char* charArr){
int tmpN, i, sign;
i = 0;
//判断是否为负数
if((sign = n) < 0){
n = - n;
}
//循环
while(n > 0){
tmpN = n % 10;
n = n / 10;
charArr[i++] = tmpN + '0';
}
if(sign < 0){
charArr[i++] = '-';
}
charArr[i] = '