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;

}
相关推荐
星马梦缘16 小时前
算法与数据结构
数据结构·c++·算法·动态规划·克鲁斯卡尔·kahn
学好statistics和DS16 小时前
命令替换(Command Substitution)详解
linux
zimoyin16 小时前
浅浅了解下0拷贝技术
java·linux·开发语言
你的冰西瓜16 小时前
C++中的array容器详解
开发语言·c++·stl
ba_pi16 小时前
每天写点什么2026-01-09-linux基础
linux·运维·服务器
wdfk_prog16 小时前
[Linux]学习笔记系列 -- 底层CPU与体系结构宏
linux·笔记·学习
Ccjf酷儿16 小时前
C++语言程序设计 (郑莉)第六章 数组、指针和字符串
开发语言·c++
陌路2017 小时前
C++28 STL容器--array
开发语言·c++
行走的bug...17 小时前
cmake总结
linux·运维·服务器
zfxwasaboy17 小时前
DRM KMS 子系统(3)CRTC
linux·c语言