c - 将 Int 数组转换为 Ascii 字符串
问题描述
我有一个 Int 数组,例如:
int testarray[20];
testarray[0] = 0x5A;
...
testarray[19] = 0x57;
并希望将其转换为 Ascii 字符串 (char*)。我怎样才能做到这一点?
解决方案
只需将值复制int
到char
数组中,不要忘记字符串终止符。
像这样的东西:
#include <stddef.h>
#include <stdlib.h>
#define ARR_SIZE 3
char *to_str(const int testarray[ARR_SIZE])
{
char *str = malloc(ARR_SIZE + 1);
if (!str) {
return NULL;
}
for (int i = 0; i < ARR_SIZE; ++i)
str[i] = testarray[i];
str[ARR_SIZE] = '\0';
return str;
}
推荐阅读
- laravel - Laravel 8 项目部署到共享主机 - 安全问题和 public_html 文件夹
- python - 列表列表中的自定义切片
- javascript - 当我尝试提交表单时,它显示 TypeError: Object(...) is not a function
- firebase - Firebase 托管重写 URL
- python - 使用 Python 按变量名对 Terraform variables.tf 进行排序
- r - 如何全部转换
到 - python - 加载包列表时出错:请求失败,状态码为 404
- c++ - 具有多个用户输入的数组的模板类
- operating-system - 页面替换策略如何适用于具有 TLB 的系统?
- coldfusion - 以正确的方式进行coldfusion 11循环