代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
C
】
基本数值类型和字符串的相互转化
作者:
日拱一卒
/ 发布于
2014/7/1
/
765
#include<stdio.h> /************************************************************************/ /* 字符串转数值 */ /************************************************************************/ /*去掉首尾无效的‘0’,例如:“023.4100”改为“23.41”*/ void lxh_strNumFormat(char *str); /*将字符串转化为无符号短整型*/ unsigned short lxh_strToUshort(char *str); /*将字符串转化为无符号整型*/ unsigned int lxh_strToUint(char *str); /*将字符串转化为无符号长整型*/ unsigned long lxh_strToUlong(char *str); /*将字符串转化为短整型*/ short lxh_strToShort(char *str); /*将字符串转化为整型*/ int lxh_strToInt(char *str); /*将字符串转化为长整型*/ long lxh_strToLong(char *str); /*将字符串转化为浮点型*/ float lxh_strToFloat(char *str); /*将字符串转化为双精度型*/ double lxh_strToDouble(char *str); /*将字符串转化为长双精度型*/ long double lxh_strToLongDouble(char *str); /************************************************************************/ /* 数值转字符串 */ /************************************************************************/ /*将无符号短整型转化为字符串*/ /*参数说明:num待转化字符串;str:转化结果*/ void lxh_uShortToStr(unsigned short num, char *str); /*将无符号整型转化为字符串*/ /*参数说明:num待转化字符串;str:转化结果*/ void lxh_uIntToStr(unsigned int num, char *str); /*将无符号长整型转化为字符串*/ /*参数说明:num待转化字符串;str:转化结果*/ void lxh_uLongToStr(unsigned long num, char *str); /*将短整型转化为字符串*/ /*参数说明:num待转化字符串;str:转化结果*/ void lxh_shortToStr(short num, char *str); /*将整型转化为字符串*/ /*参数说明:num待转化字符串;str:转化结果*/ void lxh_intToStr(int num, char *str); /*将长整型转化为字符串*/ /*参数说明:num待转化字符串;str:转化结果*/ void lxh_longToStr(long num, char *str); /*将浮点型转化为字符串*/ /*参数说明:num待转化字符串;str:转化结果;strLen:最大串长*/ void lxh_floatToStr(float num, char *str, unsigned strLen); /*将双精度型转化为字符串*/ /*参数说明:num待转化字符串;str:转化结果;strLen:最大串长*/ void lxh_doubleToStr(double num, char *str, unsigned strLen); /*将长双精度型转化为字符串*/ /*参数说明:num待转化字符串;str:转化结果;strLen:最大串长*/ void lxh_longDoubleToStr(long double num, char *str, unsigned strLen); /************************************************************************/ /* 进制转换 */ /************************************************************************/ /*void convert2ToX(unsigned long numOld, char *numNew, unsigned radixNew); void convertXTo2(unsigned long numOld, char *numNew, unsigned radixOld); void convert10ToX(unsigned long numOld, char *numNew, unsigned radixNew); void convertXTo10(unsigned long numOld, char *numNew, unsigned radixOld); void convert16ToX(unsigned long numOld, char *numNew, unsigned radixNew); void convertXTo16(unsigned long numOld, char *numNew, unsigned radixOld); void convertXToX(char *numOld, char *numNew, unsigned radixOld, unsigned radixNew); */ /************************************************************************/ /* 带格式的八进制和十六进制问题 */ /************************************************************************/ /*将类似“0X12AF”,“032”这样的字符串转化为unsigned long型数值*/ /*参数说明:str待转化字符串*/ unsigned long lxh_strToNumFormal(char *str); /*将unsigned long型数值转化为,类似“032”这样八进制标示的字符串*/ /*参数说明:num带转化数值,str转化后字符串*/ void lxh_numToStrFormal8(unsigned long num, char *str); /*将unsigned long型数值转化为十进制表示的字符串*/ /*参数说明:num带转化数值,str转化后字符串*/ void lxh_numToStrFormal10(unsigned long num, char *str); /*将unsigned long型数值转化为,类似“0X12AF”这样十六进制表示的字符串*/ /*参数说明:num带转化数值,str转化后字符串*/ void lxh_numToStrFormal16(unsigned long num, char *str); /************************************************************************/ /* 函数实现 */ /************************************************************************/ /*去掉首尾无效的‘0’,例如:“-023.4100”改为“-23.41”*/ void lxh_strNumFormat(char *str){ int dotFlag; unsigned i, j, endIndex; if(NULL==str || '\0'==str[0]) return; i = j = '-'!=str[0] ? 0 : 1; for(; str[i]!=0&&str[i]=='0'; i++); endIndex = 0; dotFlag = 0; for(; '\0'!=str[i];){ if(0==dotFlag && '.'==str[i]) dotFlag = 1; if(str[i]!='0' || 0==dotFlag) endIndex = j; str[j++] = str[i++]; } if(str[endIndex]>32 && str[endIndex]!='.') str[endIndex+1] = '\0'; else str[endIndex] = '\0'; } /*将字符串转化为无符号短整型*/ unsigned short lxh_strToUshort(char *str){ unsigned short result; unsigned i; char temp; lxh_strNumFormat(str); if(0 == str[0]) return 0; result = 0; for(i=0; '\0'!=str[i]; i++){ temp = str[i]; if(temp<'0' || temp>'9') break; result = result*10 + temp-'0'; } return result; } /*将字符串转化为无符号整型*/ unsigned int lxh_strToUint(char *str){ unsigned int result; unsigned i; char temp; lxh_strNumFormat(str); if(0 == str[0]) return 0; result = 0; for(i=0; '\0'!=str[i]; i++){ temp = str[i]; if(temp<'0' || temp>'9') break; result = result*10 + temp-'0'; } return result; } /*将字符串转化为无符号长整型*/ unsigned long lxh_strToUlong(char *str){ unsigned long result; unsigned i; char temp; lxh_strNumFormat(str); if(0 == str[0]) return 0; result = 0; for(i=0; '\0'!=str[i]; i++){ temp = str[i]; if(temp<'0' || temp>'9') break; result = result*10 + temp-'0'; } return result; } /*将字符串转化为短整型*/ short lxh_strToShort(char *str){ int signFlag; short result; unsigned i; char temp; lxh_strNumFormat(str); if(0==str[0] || ('-'==str[0] && 0==str[1])) return 0; if ('-' == str[0]) { signFlag = 1; i = 1; } else { signFlag = 0; i=0; } result = 0; for(; '\0'!=str[i]; i++){ temp = str[i]; if(temp<'0' || temp>'9') break; result = result*10 + temp-'0'; } if(1 == signFlag) result = 0-result; return result; } /*将字符串转化为整型*/ int lxh_strToInt(char *str){ int signFlag; int result; unsigned i; char temp; lxh_strNumFormat(str); if(0==str[0] || ('-'==str[0] && 0==str[1])) return 0; if ('-' == str[0]) { signFlag = 1; i = 1; } else { signFlag = 0; i=0; } result = 0; for(; '\0'!=str[i]; i++){ temp = str[i]; if(temp<'0' || temp>'9') break; result = result*10 + temp-'0'; } if(1 == signFlag) result = 0-result; return result; } /*将字符串转化为长整型*/ long lxh_strToLong(char *str){ int signFlag; long result; unsigned i; char temp; lxh_strNumFormat(str); if(0==str[0] || ('-'==str[0] && 0==str[1])) return 0; if ('-' == str[0]) { signFlag = 1; i = 1; } else { signFlag = 0; i=0; } result = 0; for(; '\0'!=str[i]; i++){ temp = str[i]; if(temp<'0' || temp>'9') break; result = result*10 + temp-'0'; } if(1 == signFlag) result = 0-result; return result; } /*将字符串转化为浮点型*/ float lxh_strToFloat(char *str){ int signFlag, dotFlag; float result; unsigned i, divNum; char temp; lxh_strNumFormat(str); if(NULL==str || 0==str[0]) return 0; if ('-' == str[0]) { signFlag = 1; i = 1; } else { signFlag = 0; i=0; } result = 0; dotFlag = 0; for(; '\0'!=str[i]; i++){ temp = str[i]; if('.' ==temp) { dotFlag = 1; divNum = 1; continue; } else if(temp<'0' || temp>'9') break; if(0==dotFlag){ result = result*10 + temp-'0'; }else{ divNum *= 10; result = result + (float)(temp-'0')/divNum; } } if(1 == signFlag) result = 0-result; return result; } /*将字符串转化为双精度型*/ double lxh_strToDouble(char *str){ int signFlag, dotFlag; double result; unsigned i, divNum; char temp; lxh_strNumFormat(str); if(NULL==str || 0==str[0]) return 0; if ('-' == str[0]) { signFlag = 1; i = 1; } else { signFlag = 0; i = 0; } result = 0; dotFlag = 0; for(; '\0'!=str[i]; i++){ temp = str[i]; if('.' ==temp) { dotFlag = 1; divNum = 1; continue; } else if(temp<'0' || temp>'9') break; if(0==dotFlag){ result = result*10 + temp-'0'; }else{ divNum *= 10; result = result + (double)(temp-'0')/divNum; } } if(1 == signFlag) result = 0-result; return result; } /*将字符串转化为长双精度型*/ long double lxh_strToLongDouble(char *str){ int signFlag, dotFlag; long double result; unsigned i, divNum; char temp; lxh_strNumFormat(str); if(NULL==str || 0==str[0]) return 0; if ('-' == str[0]) { signFlag = 1; i = 1; } else { signFlag = 0; i = 0; } result = 0; dotFlag = 0; for(; '\0'!=str[i]; i++){ temp = str[i]; if('.' ==temp) { dotFlag = 1; divNum = 1; continue; } else if(temp<'0' || temp>'9') break; if(0==dotFlag){ result = result*10 + temp-'0'; }else{ divNum *= 10; result = result + (long double)(temp-'0')/divNum; } } if(1 == signFlag) result = 0-result; return result; } /*将无符号短整型转化为字符串*/ void lxh_uShortToStr(unsigned short num, char *str){ unsigned i, j; char r; if(NULL == str) return; j = 0; while(num != 0){ r = num%10; num = num/10; str[j++] = r+'0'; } str[j--] = '\0'; for(i=0; i<j; i++,j--){ r = str[i]; str[i] = str[j]; str[j] = r; } } /*将无符号整型转化为字符串*/ void lxh_uIntToStr(unsigned int num, char *str){ unsigned i, j; char r; if(NULL == str) return; j = 0; while(num != 0){ r = num%10; num = num/10; str[j++] = r+'0'; } str[j--] = '\0'; for(i=0; i<j; i++,j--){ r = str[i]; str[i] = str[j]; str[j] = r; } } /*将无符号长整型转化为字符串*/ void lxh_uLongToStr(unsigned long num, char *str){ unsigned i, j; char r; if(NULL == str) return; j = 0; while(num != 0){ r = (char)(num%10); num = num/10; str[j++] = r+'0'; } str[j--] = '\0'; for(i=0; i<j; i++,j--){ r = str[i]; str[i] = str[j]; str[j] = r; } } /*将短整型转化为字符串*/ void lxh_shortToStr(short num, char *str){ unsigned i, j; char r; if(NULL == str) return; if(num < 0) { str[0] = '-'; i = 1; j = 1; num = 0 - num; }else{ i = 0; j = 0; } while(num != 0){ r = num%10; num = num/10; str[j++] = r+'0'; } str[j--] = '\0'; for(; i<j; i++,j--){ r = str[i]; str[i] = str[j]; str[j] = r; } } /*将整型转化为字符串*/ void lxh_intToStr(int num, char *str){ unsigned i, j; char r; if(NULL == str) return; if(num < 0) { str[0] = '-'; i = 1; j = 1; num = 0 - num; }else{ i = 0; j = 0; } while(num != 0){ r = num%10; num = num/10; str[j++] = r+'0'; } str[j--] = '\0'; for(; i<j; i++,j--){ r = str[i]; str[i] = str[j]; str[j] = r; } } /*将长整型转化为字符串*/ void lxh_longToStr(long num, char *str){ unsigned i, j; char r; if(NULL == str) return; if(num < 0) { str[0] = '-'; i = 1; j = 1; num = 0 - num; }else{ i = 0; j = 0; } while(num != 0){ r = num%10; num = num/10; str[j++] = r+'0'; } str[j--] = '\0'; for(; i<j; i++,j--){ r = str[i]; str[i] = str[j]; str[j] = r; } } /*将浮点型转化为字符串*/ void lxh_floatToStr(float num, char *str, unsigned strLen){ unsigned i, j, dotIndex; long zsbf; float xsbf; char r; if(NULL == str) return; if(num < 0) { str[0] = '-'; i = 1; j = 1; num = 0 - num; }else{ i = 0; j = 0; } zsbf = (long)num; xsbf = num - zsbf; if(0 == zsbf) str[j++] = '0'; while(zsbf != 0){ r = zsbf%10; zsbf = zsbf/10; str[j++] = r+'0'; } dotIndex = j; for(j=dotIndex-1; i<j; i++,j--){ r = str[i]; str[i] = str[j]; str[j] = r; } if(0 == xsbf) { str[dotIndex] = '\0'; return; } else { str[dotIndex] = '.'; } j = dotIndex+1; while(xsbf!=0 && j<strLen){ num = (float)(xsbf*10); r = (char)num; xsbf = num - r; str[j++] = r+'0'; } if(j<strLen) str[j] = '\0'; else str[strLen-1] = '\0'; } /*将双精度型转化为字符串*/ void lxh_doubleToStr(double num, char *str, unsigned strLen){ unsigned i, j, dotIndex; long zsbf; double xsbf; char r; if(NULL == str) return; if(num < 0) { str[0] = '-'; i = 1; j = 1; num = 0 - num; }else{ i = 0; j = 0; } zsbf = (long)num; xsbf = num - zsbf; if(0 == zsbf) str[j++] = '0'; while(zsbf != 0){ r = zsbf%10; zsbf = zsbf/10; str[j++] = r+'0'; } dotIndex = j; for(j=dotIndex-1; i<j; i++,j--){ r = str[i]; str[i] = str[j]; str[j] = r; } if(0 == xsbf) { str[dotIndex] = '\0'; return; } else { str[dotIndex] = '.'; } j = dotIndex+1; while(xsbf!=0 && j<strLen){ num = (float)(xsbf*10); r = (char)num; xsbf = num - r; str[j++] = r+'0'; } if(j<strLen) str[j] = '\0'; else str[strLen-1] = '\0'; } /*将长双精度型转化为字符串*/ void lxh_longDoubleToStr(long double num, char *str, unsigned strLen){ unsigned i, j, dotIndex; long zsbf; long double xsbf; char r; if(NULL == str) return; if(num < 0) { str[0] = '-'; i = 1; j = 1; num = 0 - num; }else{ i = 0; j = 0; } zsbf = (long)num; xsbf = num - zsbf; if(0 == zsbf) str[j++] = '0'; while(zsbf != 0){ r = zsbf%10; zsbf = zsbf/10; str[j++] = r+'0'; } dotIndex = j; for(j=dotIndex-1; i<j; i++,j--){ r = str[i]; str[i] = str[j]; str[j] = r; } if(0 == xsbf) { str[dotIndex] = '\0'; return; } else { str[dotIndex] = '.'; } j = dotIndex+1; while(xsbf!=0 && j<strLen){ num = (float)(xsbf*10); r = (char)num; xsbf = num - r; str[j++] = r+'0'; } if(j<strLen) str[j] = '\0'; else str[strLen-1] = '\0'; } /************************************************************************/ /* TODO:进制转换 */ /************************************************************************/ /*将类似“0X12AF”,“032”这样的字符串转化为unsigned long型数值*/ unsigned long lxh_strToNumFormal(char *str){ unsigned long result = 0; unsigned radix, i; char temp; lxh_strNumFormat(str); if(NULL==str || 0==str[0]) return 0; if('0' == str[0]){ if('x' == str[1] || 'X' == str[1]){ radix = 16; i=2; } else { radix = 8; i=1; } } else { radix = 10; i = 0; } for(;'\0' != str[i]; i++){ temp = str[i]; if(8==radix){ if(temp<'0' || temp>'7') break; } else if(10==radix){ if(temp<'0' || temp>'9') break; } else if(16==radix){ if(temp<'0' || (temp>'9' && temp<'A') || (temp>'F' && temp<'a') || temp>'f') break; } if(temp>='a' && temp<='f') temp = temp-'a'+10; else if(temp>='A' && temp<='F') temp = temp-'A'+10; else temp = temp - '0'; result = result * radix + temp; } return result; } /*将unsigned long型数值转化为,类似“032”这样八进制标示的字符串*/ void lxh_numToStrFormal8(unsigned long num, char *str){ unsigned i, j; char r; if(NULL == str) return; j = 0; while(num != 0){ r = (char)(num%8); num = num/8; str[j++] = r+'0'; } str[j] = '0'; str[j+1] = '\0'; for(i=0; i<j; i++,j--){ r = str[i]; str[i] = str[j]; str[j] = r; } } /*将unsigned long型数值转化为十进制表示的字符串*/ void lxh_numToStrFormal10(unsigned long num, char *str){ unsigned i, j; char r; if(NULL == str) return; j = 0; while(num != 0){ r = (char)(num%10); num = num/10; str[j++] = r+'0'; } str[j--] = '\0'; for(i=0; i<j; i++,j--){ r = str[i]; str[i] = str[j]; str[j] = r; } } /*将unsigned long型数值转化为,类似“0X12AF”这样十六进制表示的字符串*/ void lxh_numToStrFormal16(unsigned long num, char *str){ unsigned i, j; char r; if(NULL == str) return; j = 0; if(0 == num) str[j++] = '0'; while(num != 0){ r = (char)(num%16); num = num/16; if(r>9) str[j++] = r-10+'A'; else str[j++] = r+'0'; } str[j++] = 'X'; str[j] = '0'; str[j+1] = '\0'; for(i=0; i<j; i++,j--){ r = str[i]; str[i] = str[j]; str[j] = r; } }
试试其它关键字
基本数值类型
字符串
同语言下
.
获取手机通讯录 iOS去除数字以外的所有字符
.
异步加载音乐等资源
.
交通罚单管理系统
.
freemark实现,简单的替换
.
计算斐波那契数列
.
base64解码 包括解码长度
.
图像显示
.
冒泡排序
.
输入十进制数,输出指定进制
.
链式栈
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
日拱一卒
贡献的其它代码
(
1
)
.
基本数值类型和字符串的相互转化
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3