【发布时间】:2016-03-21 17:37:00
【问题描述】:
我想在屏幕上显示std::vector<unsigned char> 中的数字,但在发送给收件人的途中,我需要将这些数字填入std::string。
无论我尝试什么(atoi、reinterpret_cast、string.c_str() ...),都给了我一个废话,或者这些原始数字的字母表示 - 即它们对应的 ascii 字符。
那么我如何轻松(最好是标准方法)将vector<unsigned char> {1,2,3} 转换为string "1-2-3"?
在我提到的原始帖子(后来编辑)中,我可以在 C# 或 Java 中做到这一点。 应 πάντα ῥεῖ 的要求,提供 C# 或 Java 中的示例,这是一种快速的 Linq C# 方式:
public static string GetStringFromListNumData<T>(List<T> lNumData)
{
// if (typeof(T) != typeof(IConvertible)) throw new ArgumentException("Expecting only types that implement IConvertible !");
string myString = "";
lNumData.ForEach(x => myString += x + "-");
return myString.TrimEnd('-');
}
【问题讨论】:
-
1和'1'不是一回事。 -
您如何使用 C# 或 Java 以简单的方式做到这一点?
-
@πάντα ῥεῖ 我刚刚发布了 C# 示例,肯定有可能改进:)
标签: c# c++ string vector data-conversion