在C ++中将字节字符串转换为普通字符串 [英] Convert byte string to normal string in C++
问题描述
我有以下情况:
我实际上是试图将服务器中的流数据读取到Visual Studio中的标签中。
I'm actually trying to read streamdata from a server into a label in Visual Studio:
//Receive a reply from the server
if((recv_size = recv(ConnectSocket , server_reply , 2000 , 0)) == SOCKET_ERROR){
MessageBox::Show("recv failed","");
//exit(1);
}
this->label1->Text = Convert::ToString(server_reply[0]);
示例结果:
ANAG; FCA; 11:20:27; NL0010877643 ;菲亚特克莱斯勒汽车; 16.85; 0.0; 0
example result:
ANAG;FCA;11:20:27;NL0010877643;FIAT CHRYSLER AUTO;16.85;0.0;0
当我将其添加到程序中时,它会像:
657865 ...
when I get it into my program, I have it like: 657865...
我认为这是相应字符的字节表示形式(例如:65 = A,78 = N等)。
which I think is the byte representation of the corresponding characters (eg.: 65 = A, 78 = N, etc.).
问题是:如何将这些字节码转换为正常的字符串?
Question is: How do I convert these bytecodes into a normal string of characters?
服务器似乎正在发送字节数据
The server seems to be sending byte data
预先感谢
推荐答案
您只需要从中创建一个字符串。
You just need to create a string from it.
#include <iostream>
#include <string>
using namespace std;
int main()
{
char byteArray[] = { 65, 78, 65, 71 }; // .... your input
std::string s(byteArray, sizeof(byteArray));
cout << s;
return 0;
}
这篇关于在C ++中将字节字符串转换为普通字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!