c - 如何读取多个字符并转换为 Ascii?
问题描述
我有一个用空格分隔的十六进制格式的输入:(例如)4A 49 5A 我需要读取该行并打印同一行但以 Ascii 格式:JIZ 我实际上做了相反的操作(ascii 到十六进制)但不能使其适应所需的情况。我只能使用stdio.h
,string.h
下面是代码 ascii 到 hex。
char *line = NULL;
char *token;
size_t len = 0;
ssize_t read = 0;
read = getline(&line, &len, stdin);
if (read == -1) {
printf("n/a\n");
return 0;
}
while ((token = strsep(&line, " ")) != NULL) {
printf("%X ", *token);
}
return 1;
还有我的尝试让十六进制到 ascii 工作。据我了解,read
在这种情况下,变量也无法正常工作。
char *token;
size_t len = 0;
ssize_t read = 0;
int e;
read = getline(&line, &len, stdin);
if (read == -1) {
printf("n/a\n");
return 0;
}
printf("%zd", read);
for (int i = 0; i < (sizeof(read)/sizeof(char)); i++) {
e = line[i];
//printf("%c", line[i]);
//printf("%d ", e);
}
// while ((token = strsep(&line, " ")) != NULL) {
// printf("%d", (int)*token);
//}
return 1;
解决方案
推荐阅读
- python - Django REST Framework - Django 在`pip3 list` 中大写
- postgresql - 从 SQL 查询中删除内部联接
- javascript - 在单页应用程序中应用 div(卡片)中的排序方法 - Vanilla Javascript
- javascript - 表示函数上下文的变量?
- html - 创建具有清晰文本元素的固定、较暗、模糊的背景图像
- installation - 在 MacOS Big Sur(M1 芯片)上安装自制软件时收到警告
- mysql - 我们如何分别展示不同类型的产品?
- scrapy - 使用scrapyd.schedule时如何将带有scrapyd的数字/字符串列表发送给蜘蛛
- node.js - nodejs不和谐从用户ID获取昵称
- python - Python Dictionary 实现相关的语法问题