1

I have the following situation:

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]);

example result:
ANAG;FCA;11:20:27;NL0010877643;FIAT CHRYSLER AUTO;16.85;0.0;0

when I get it into my program, I have it like: 657865...

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

Thanks in advance

3
  • 4
    Why the image? Can you just type it into the question Commented Jan 5, 2018 at 10:48
  • 2
    How does that data arrive to your program? How are you printing it? Is the server actually hex encoding it or it is an artifact of how you manipulate it? Commented Jan 5, 2018 at 10:49
  • Show at least some code. We have no idea what you're doing. Answer below is probably what you're looking for Commented Jan 5, 2018 at 11:04

1 Answer 1

2

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

depending on the host system implementation, char might be signed so anything larger than 127 will result in a ie narrowing conversion of '255' from 'int' to 'char' Check with godbolt.org for things like this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.