我想在屏幕上显示std::vector<unsigned char>
中的数字,但在前往收件人的途中,我需要将这些数字填入std::string
。将矢量<无符号字符> {1,2,3}转换为字符串“1-2-3”AS DIGITS
无论我尝试过什么(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''不是一回事。 – NathanOliver
您会如何以_easy方式使用C#或Java来做到这一点? –
@πάνταῥεῖ我刚刚发布了C#示例,肯定是可能改进的主题:) –