跳转到内容
主菜单
主菜单
移至侧栏
隐藏
导航
首页
最近更改
随机页面
MediaWiki帮助
代码酷
搜索
搜索
中文(中国大陆)
外观
创建账号
登录
个人工具
创建账号
登录
未登录编辑者的页面
了解详情
贡献
讨论
编辑“︁
C 语言字符串转换
”︁
页面
讨论
大陆简体
阅读
编辑
编辑源代码
查看历史
工具
工具
移至侧栏
隐藏
操作
阅读
编辑
编辑源代码
查看历史
常规
链入页面
相关更改
特殊页面
页面信息
外观
移至侧栏
隐藏
您的更改会在有权核准的用户核准后向读者展示。
警告:
您没有登录。如果您进行任何编辑,您的IP地址会公开展示。如果您
登录
或
创建账号
,您的编辑会以您的用户名署名,此外还有其他益处。
反垃圾检查。
不要
加入这个!
{{DISPLAYTITLE:C语言字符串转换}} '''C语言字符串转换'''是指将字符串与其他数据类型(如整数、浮点数)相互转换的过程,或在不同字符串编码格式(如ASCII与宽字符)间进行转换的操作。由于C语言中字符串本质是字符数组,这类转换需要特定的库函数和技巧。本条目将详细介绍常见的字符串转换方法及其应用场景。 == 基本概念 == 在C语言中,字符串是以空字符(<code>\0</code>)结尾的字符数组。字符串转换通常涉及以下两类操作: # '''数据类型转换''':字符串与数值类型(如<code>int</code>、<code>double</code>)的相互转换 # '''编码转换''':不同字符编码(如ASCII与UTF-8)或字符宽度(如<code>char</code>与<code>wchar_t</code>)间的转换 == 字符串与数值转换 == === 字符串转数值 === C标准库提供以下函数(需包含头文件<code><stdlib.h></code>): * <code>atoi()</code>:字符串转<code>int</code> * <code>atof()</code>:字符串转<code>double</code> * <code>strtol()</code>:字符串转<code>long</code>(可指定进制) <syntaxhighlight lang="c"> #include <stdio.h> #include <stdlib.h> int main() { char str_int[] = "12345"; char str_float[] = "3.1415"; int num_int = atoi(str_int); double num_float = atof(str_float); printf("整数: %d\n浮点数: %f\n", num_int, num_float); return 0; } </syntaxhighlight> '''输出:''' <pre> 整数: 12345 浮点数: 3.141500 </pre> 更安全的替代方案(C11起): <syntaxhighlight lang="c"> #include <stdio.h> #include <stdlib.h> int main() { char str[] = "42 apples"; char *endptr; long num = strtol(str, &endptr, 10); printf("数值: %ld\n剩余字符串: '%s'\n", num, endptr); return 0; } </syntaxhighlight> '''输出:''' <pre> 数值: 42 剩余字符串: ' apples' </pre> === 数值转字符串 === 常用方法: * <code>sprintf()</code>:格式化输出到字符串 * <code>snprintf()</code>:安全版本(指定缓冲区大小) <syntaxhighlight lang="c"> #include <stdio.h> int main() { char buffer[50]; int num = 255; snprintf(buffer, sizeof(buffer), "十进制: %d, 十六进制: 0x%X", num, num); puts(buffer); return 0; } </syntaxhighlight> '''输出:''' <pre> 十进制: 255, 十六进制: 0xFF </pre> == 字符编码转换 == === 窄字符与宽字符转换 === 需包含头文件<code><wchar.h></code>和<code><locale.h></code>: <syntaxhighlight lang="c"> #include <stdio.h> #include <wchar.h> #include <locale.h> int main() { setlocale(LC_ALL, ""); // 设置本地化环境 char narrow_str[] = "你好, 世界!"; wchar_t wide_str[50]; // 窄字符转宽字符 mbstowcs(wide_str, narrow_str, sizeof(wide_str)/sizeof(wchar_t)); wprintf(L"宽字符: %ls\n", wide_str); return 0; } </syntaxhighlight> '''输出:''' <pre> 宽字符: 你好, 世界! </pre> === 大小写转换 === 使用<code><ctype.h></code>中的函数: <syntaxhighlight lang="c"> #include <stdio.h> #include <ctype.h> void str_toupper(char *str) { for (; *str; ++str) *str = toupper((unsigned char)*str); } int main() { char text[] = "Case Conversion Example"; str_toupper(text); puts(text); return 0; } </syntaxhighlight> '''输出:''' <pre> CASE CONVERSION EXAMPLE </pre> == 实际应用案例 == === 案例1:配置文件解析 === 解析键值对格式的配置文件(如<code>port=8080</code>): <syntaxhighlight lang="c"> #include <stdio.h> #include <stdlib.h> #include <string.h> void parse_config(const char *line) { char key[50], value[50]; if (sscanf(line, "%49[^=]=%49s", key, value) == 2) { if (strcmp(key, "port") == 0) { int port = atoi(value); printf("配置端口: %d\n", port); } } } int main() { parse_config("port=8080"); return 0; } </syntaxhighlight> === 案例2:URL编码解码 === URL编码将特殊字符转换为<code>%XX</code>格式: <mermaid> graph LR A[原始字符 'A'] -->|ASCII 65| B[十六进制 41] B --> C[URL编码 '%41'] </mermaid> 实现部分解码功能: <syntaxhighlight lang="c"> #include <stdio.h> #include <ctype.h> void url_decode(char *dst, const char *src) { char a, b; while (*src) { if (*src == '%' && isxdigit(a = src[1]) && isxdigit(b = src[2])) { *dst++ = (a - (isdigit(a) ? '0' : 'A'-10)) * 16 + (b - (isdigit(b) ? '0' : 'A'-10)); src += 3; } else { *dst++ = *src++; } } *dst = '\0'; } int main() { char encoded[] = "Hello%20World%21"; char decoded[50]; url_decode(decoded, encoded); puts(decoded); return 0; } </syntaxhighlight> '''输出:''' <pre> Hello World! </pre> == 数学公式示例 == ASCII字符转换数值的数学表达: 对于数字字符<code>'0'</code>到<code>'9'</code>: <math>value = char - '0'</math> 十六进制字符转换: <math> value = \begin{cases} char - '0' & \text{如果 } char \in ['0','9'] \\ 10 + (char - 'A') & \text{如果 } char \in ['A','F'] \\ 10 + (char - 'a') & \text{如果 } char \in ['a','f'] \end{cases} </math> == 安全注意事项 == * 使用<code>strtol()</code>而非<code>atoi()</code>以检测转换错误 * 所有字符串操作应检查缓冲区边界 * 宽字符函数需要正确设置本地化环境 [[Category:C语言字符串操作]] [[Category:C语言标准库]] [[Category:编程语言]] [[Category:C]] [[Category:C 语言字符串]]
摘要:
请注意,所有对代码酷的贡献均被视为依照知识共享署名-非商业性使用-相同方式共享发表(详情请见
代码酷:著作权
)。如果您不希望您的文字作品被随意编辑和分发传播,请不要在此提交。
您同时也向我们承诺,您提交的内容为您自己所创作,或是复制自公共领域或类似自由来源。
未经许可,请勿提交受著作权保护的作品!
取消
编辑帮助
(在新窗口中打开)