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;

}
相关推荐
大白话_NOI20 分钟前
【洛谷 P1303】A*B Problem + 详细分析
c++
小欣加油27 分钟前
leetcode2161 根据给定数字划分数组
数据结构·c++·算法·leetcode·职场和发展
吃着火锅x唱着歌41 分钟前
深度探索C++对象模型 学习笔记 第五章 构造、解构、拷贝语意学(2)
c++·笔记·学习
玖釉-43 分钟前
Vulkan 离屏渲染详解:从 Framebuffer 到后处理、阴影贴图与 Render Texture
c++·windows·计算机视觉·图形渲染
拾贰_C1 小时前
【python | installation 】python 安装 | Windows | 命令使用
linux·数据库·ubuntu
cpp_25011 小时前
P2947 [USACO09MAR] Look Up S
数据结构·c++·算法·题解·单调栈·洛谷
无限进步_1 小时前
Linux进程终止——退出码、exit与_exit
linux·运维·服务器
编程大师哥1 小时前
最高效的 IO 并发方案
linux·网络·python
楼田莉子1 小时前
C++20新特性:协程
开发语言·c++·后端·学习·c++20
炘爚1 小时前
phase1:基础框架——编译 + MySQL + 登录/注册
linux·c++