c - 如何在 C 中将类型为 AA:BB:CC:DD:EE:FF 的字符串转换为 0xaabbccddeeff?
问题描述
输入:AA:BB:CC:DD:EE:FF 预期输出:0xaabbccddeeff。输入:AA:BB:65:F0:E4:D4 预期输出:0xaabb65f0e4d4
char arr[20]="AA:BB:CC:DD:EE:FF";
char t[20]="0x";
char *token=strtok(arr[i], ":");
while(token !=NULL){
printf("%s\n", token);
token = strtok(NULL, ":");
strcat(t, token);
}
printf("The modified string is %s\n", t);
我看到了分段错误。
解决方案
您正在尝试strcat
使用 null 令牌进行决赛。在拨打电话之前尝试移动您的条件以检查strcat
:
#include <ctype.h>
#include <stdio.h>
#include <string.h>
void lower(char *c) {
for (; *c = tolower(*c); *c++);
}
int main() {
char s[] = "AA:BB:CC:DD:EE:FF";
char t[15] = "0x";
char *token = strtok(s, ":");
if (token) {
lower(token);
strcat(t, token);
while (token = strtok(NULL, ":")) {
lower(token);
strcat(t, token);
}
}
printf("The modified string is %s\n", t);
}
输出:
The modified string is 0xaabbccddeeff
推荐阅读
- reporting-services - 转至行动报告。没有后退选项
- oracle - SQLDeveloper,有错误的 Oracle 过程
- python - 当启用急切执行时,传递给 Optimizer.compute_gradients 的 `loss` 应该是一个函数
- drmaa - Blender中基于DRM的计算
- asp.net - 我可以防止我的 asp.net mvc-4 Web 应用程序中的 Elmah 暴露用户的密码吗
- r - 是否有代码可以在 dplyr 等 R 安装包中查找函数的数量
- python - 如何过滤任意结果的查询?
- python - 如何创建一个字典,将 csv 列内容作为键,将出现次数作为值?
- expect - 偶尔期望发送命令被截断
- r - R - 合并分布在数据帧列表中的标识符值