C语言中字符串到整型以及整型到字符串的转换粗浅实现
2015-05-13 20:34
357 查看
今天心血来潮实现了字符串到整型以及整型到字符串的转换函数, 因算法也不强, 所以效率也高不了, 因C语言用得不好, 就当练习吧. 另一种普遍做法是atoi实现字符串到整型当转换, snprintf实现整型到字符串的转换.
#include <string.h> #include <stdlib.h> #include <stdio.h> char *tostr(int in) { int len = int_len(in); int begin = in < 0 ? 1 : 0; len += begin; char *buf = (char *)malloc(len + 1); int multiple = 1; int oldmultiple, i; buf[len] = '\0'; if(in == 0) { buf[0] = '0'; } else { if (in < 0) { buf[0] = '-'; in = -in; } for(i = len - 1; i >= begin; --i) { oldmultiple = multiple; multiple *= 10; buf[i] = '0' + ((in - (in / multiple * multiple)) / oldmultiple); } } return buf; } int toint(char *str, int len, int *ret) { char *c = str; int length = string_len(str, len); if (length <= 0) { return -1; } int sigm = (*c == '+' || *c == '-') ? 1 : 0; int result = 0; int multiple = 1; int i; for (i = length + sigm - 1; i >= sigm; --i) { result += (c[i] - '0') * multiple; multiple *= 10; } if (c[0] == '-') { result = -result; } *ret = result; return 0; } int int_len(int in) { int len = 0; while(in != 0) { in = in / 10; ++len; } return len; } int string_len(char *str, int len) { int count = 0; char *c = str; while(*c != '\0' && count < len) { if (*c > '9' || *c < '0') { if (count == 0 && (*c == '+' || *c == '-')) { ++c; continue; } else { return -1; } } else { ++c; ++count; } } return count; } int main(void) { printf("%d: %d\n", 0, int_len(0)); printf("%d: %d\n", -0, int_len(-0)); printf("%d: %d\n", 1, int_len(1)); printf("%d: %d\n", -1, int_len(-1)); printf("%d: %d\n", 40285, int_len(40285)); printf("%d: %d\n", -19948799, int_len(-19948799)); printf("-----------------------------------\n"); printf("%d: %s\n", 1, tostr(1)); printf("%d: %s\n", 0, tostr(0)); printf("%d: %s\n", 393, tostr(393)); printf("%d: %s\n", 50938325, tostr(50938325)); printf("%d: %s\n", -4922, tostr(-4922)); printf("%d: %s\n", -1, tostr(-1)); printf("%d: %s\n", -0, tostr(-0)); printf("-----------------------------------\n"); printf("%s: %d\n", "0", string_len("0", 1)); printf("%s: %d\n", "-0", string_len("-0", 2)); printf("%s: %d\n", "1", string_len("1", 1)); printf("%s: %d\n", "-1", string_len("-1", 2)); printf("%s: %d\n", "403295", string_len("403295", 6)); printf("%s: %d\n", "403295", string_len("403295", 9)); printf("%s: %d\n", "-11094885", string_len("-11094885", 9)); printf("%s: %d\n", "+11094885", string_len("+11094885", 9)); printf("%s: %d\n", "1+094885", string_len("1+094885", 8)); printf("%s: %d\n", "+", string_len("+", 1)); printf("%s: %d\n", "", string_len("", 1)); printf("-----------------------------------\n"); int ret = 0; char *temp = "0"; if (!toint(temp, sizeof(temp), &ret)) { printf("%s: %d\n", temp, ret); } temp = "-0"; if (!toint(temp, sizeof(temp), &ret)) { printf("%s: %d\n", temp, ret); } temp = "-1"; if (!toint(temp, sizeof(temp), &ret)) { printf("%s: %d\n", temp, ret); } temp = "1"; if (!toint(temp, sizeof(temp), &ret)) { printf("%s: %d\n", temp, ret); } temp = "+321012"; if (!toint(temp, sizeof(temp), &ret)) { printf("%s: %d\n", temp, ret); } temp = "-4995822"; if (!toint(temp, sizeof(temp), &ret)) { printf("%s: %d\n", temp, ret); } temp = "+d"; if (!toint(temp, sizeof(temp), &ret)) { printf("%s: %d\n", temp, ret); } }
相关文章推荐
- c语言实现字符串转换为整型
- 整型数组处理算法(十四)不用库函数,用C语言实现将一整型数转换成字符串
- 库函数atoi的C语言实现---------将字符串的数字符转换成整型的数字
- c语言实现,将一个字符串转换为对应的整数
- C/C++浮点数的存储方式,以及实现一个ftoa函数将浮点数转换为字符串
- C语言拼接字符串以及进制转换
- 九度oj+原始一个字符串左右交换字符最少步数变成含有2012字串+bfs及字符串和整型转换以及没有标记visit数组大小,只能用stl中的vecotr,实际上set更好。有count函数
- C语言实现 递归法 数字转换成字符串
- @V@ java代码笔记2010-06-12:java控制台输入各类型类实现;以及判断输入字符串里面是否有数字的两种方法:方法1:转换成字符数组;方法2:正则表达式。
- java中字符串处理、串联和转换的几个常用方法,以及如果需要自己编程实现的具体实施步骤。
- C语言strtol()函数:将字符串转换成long(长整型数)
- C#枚举和字符串以及整型数之间的转换
- 不用库函数,用C语言实现将一整型数字转化为字符串
- C语言实现将字符串转换为数字的方法
- C语言实现 字符串转换为整数输出 int ascii_to_integer(char *str);
- 【C语言】编写函数实现库函数atoi,把字符串转换成整形
- C语言中字符串系列函数的实现以及注意事项
- C语言实现字节流与十六进制字符串的相互转换
- C语言中字符串和数字的相互转换实现代码
- C语言实现int转换字符串的一种实现