转换INT []为String在C ++中 [英] Converting int[] to String in C++

查看:118
本文介绍了转换INT []为String在C ++中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串定义为的std ::字符串头=00110033;
现在我需要的字符串来保存的数字的字节值,假设其构造是这样

I have a string defined as std::string header = "00110033"; now I need the string to hold the byte values of the digits as if its constructed like this

char data_bytes[] = { 0, 0, 1, 1, 0, 0, 3, 3};
std::string header = new std::string(data_bytes, 8).c_str());

我用的atoi 转换的初始字符串为 INT 阵列。现在,我不知道如何使串出来。让我知道如果有任何更好的方法。

I converted the initial string to int array using atoi. Now i'm not sure how to make the string out of it. Let me know if there is any better approach.

推荐答案

你可以写一个小功能。

string int_array_to_string(int int_array[], int size_of_array) {
  string returnstring = "";
  for (int temp = 0; temp < size_of_array; temp++)
    returnstring += itoa(int_array[temp]);
  return returnstring;
}

未经测试!

一个稍微不同的方法

string int_array_to_string(int int_array[], int size_of_array) {
  ostringstream oss("");
  for (int temp = 0; temp < size_of_array; temp++)
    oss << int_array[temp];
  return oss.str();
}

这篇关于转换INT []为String在C ++中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆