Array 数组转字符串方法

C++ 语言中经常碰到需要将数组转成字符串,方法如下

cpp 复制代码
std::string arrayToString(unsigned char* arr, int size) {
    std::stringstream ss;
    for (int i = 0; i < size; ++i) {
        ss << arr[i];
    }
    return ss.str();
}

将char data[ ] 数组转换成string类型如下,通过arrayToString函数返回string 类型

cpp 复制代码
int set_modelgroup(unsigned char data[],int len)
{
    int r=0;
    std::string ret;
    std::string str;
    cJSON *setjson = cJSON_Parse(k_app_setodmindex);
    cJSON *message = cJSON_GetObjectItem(setjson, "message");
    cJSON_DeleteItemFromObject(message, "Model_group");
    
    str=arrayToString(data,len); // 将字符串数组转成string类型

    cJSON_AddStringToObject(message, "Model_group", str.c_str());
    char *modified_json = cJSON_PrintUnformatted(setjson);
    std::string s2=modified_json;
    if (0 == s_factoryCmd.get(modified_json, ret)) {
        printf("%s\n", __func__);
        printf("  - %s\n", modified_json);
        printf("    - %s\n", ret.c_str());

        cJSON *json = cJSON_Parse(ret.c_str());
        cJSON *result = cJSON_GetObjectItem(json, "result");
        cJSON *SUCCESS = cJSON_GetObjectItem(result, "SUCCESS");

       if (cJSON_IsNumber(SUCCESS)) {
        printf("SUCCESS: %d\n", SUCCESS->valueint);
       }
        cJSON_Delete(json);
        return SUCCESS->valueint;
    }

    return r;

}
相关推荐
薛定猫AI1 小时前
Codex 与 Claude Code 全平台安装配置指南(Windows / macOS / Linux)
linux·windows·macos
BirdenT2 小时前
20260519紫题训练
c++·算法
kidwjb9 小时前
信号量在进程中的使用
linux·进程间通信
C+++Python10 小时前
C++ 进阶学习完整指南
java·c++·学习
sparEE10 小时前
c++值类别、右值引用和移动语义
开发语言·c++
sulikey11 小时前
个人Linux操作系统学习笔记2 - gcc与库的理解
linux·笔记·学习·操作系统·gcc·
jrrz082811 小时前
Apollo MPC Controller
c++·自动驾驶·apollo·mpc·横向控制·lateral control
二宝哥11 小时前
Linux虚拟机网络配置
linux·运维·服务器
陳103012 小时前
Linux:进程间通信 和 简单进程池
linux·运维·服务器
jimy112 小时前
改.bashrc,直观地判断本地repo是否有改动
linux·服务器