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;

}
相关推荐
曦云沐12 分钟前
【避坑指南】Ubuntu更新报错“Repository is not signed”的快速修复
linux·ubuntu·docker
kokunka26 分钟前
【源码+注释】纯C++小游戏开发之射击小球游戏
开发语言·c++·游戏
带土11 小时前
10. .out文件
linux
STCNXPARM1 小时前
Linux camera之V4L2子系统详解
android·linux·camera·v4l2架构
yueyuexiaokeai12 小时前
linux kernel常用函数整理
linux·c语言
John_ToDebug2 小时前
浏览器内核崩溃深度分析:从 MiniDump 堆栈到 BindOnce UAF 机制(未完待续...)
c++·chrome·windows
郝亚军3 小时前
ubuntu-18.04.6-desktop-amd64安装步骤
linux·运维·ubuntu
txinyu的博客3 小时前
解析muduo源码之 SocketsOps.h & SocketsOps.cc
c++
Konwledging4 小时前
kernel-devel_kernel-headers_libmodules
linux
Web极客码4 小时前
CentOS 7.x如何快速升级到CentOS 7.9
linux·运维·centos