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;

}
相关推荐
水饺编程9 小时前
第2章:Linux 基础,切换到超级用户
linux·c语言·汇编
便利店10249 小时前
系统活动一抓留存!深入 Linux C/C++ 调试工具之 sysdig
linux·运维·服务器·sysdig
qetfw10 小时前
Bash 变量:export、位置参数与默认值展开验证
linux·开发语言·chrome·bash
syagain_zsx10 小时前
BRpc 二次封装:Channel 管理、服务管理与回调工厂
c++·学习·程序人生·rpc·谷歌
其实防守也摸鱼10 小时前
Cylab在线靶场——CTF中的通用技能
linux·数据库·学习·算法·靶场·ctf·学习网络安全
toooooop811 小时前
CentOS 7 内核 3.10.0-1160.119.1 升级指南
linux·运维·centos
码少女11 小时前
Linux——UDP协议和TCP协议
linux·网络·udp·tcp
fpcc11 小时前
计算机原理—如何高效的操作文件
c++·计算机原理
GeW11 小时前
新质生产力发展路线图Linux、数据库、AI 一个都不能少
linux
kkkkkkkkkk_Z12 小时前
新手自学嵌入式 | 学习日记:Linux驱动复习之系统移植(U-Boot篇)
linux·运维·学习