Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1065889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T19:55:23+00:00 2026-05-16T19:55:23+00:00

My TcpClient class accepts vector<char> in its SendData method like this: void CTcpClient::SendData(const vector<char>&

  • 0

My TcpClient class accepts vector<char> in its SendData method like this:

void CTcpClient::SendData(const vector<char>& dataToTransmit)

Therefore, in order to use the function, I have to convert any built in type (int, long, short, long long) to a vector<char>.

I tried several solutions using streams but always end up with an ASCII representation of the number I want to convert (I also tried to use binary flags without success). But I need the binary values of the numbers.

For example:

int num = 0x01234567
vector<char> whatIWant = {0x01, 0x23, 0x45, 0x67}

What solution would you suggest?

Thanks for any help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T19:55:24+00:00Added an answer on May 16, 2026 at 7:55 pm

    Ignoring endianess:

    template< typename T >
    char* begin_binary(const T& obj) {return reinterpret_cast<char*>(&obj);}
    template< typename T >
    char* end_binary  (const T& obj) {return begin_binary(obj)+sizeof(obj);}
    
    int num = 0x01234567;
    vector<char> whatIWant( begin_binary(num), end_binary(num) );
    

    However, I would use unsigned char as a byte type.

    I feel the need to add to this that, as always, the use of reinterpret_cast makes the result of this implementation-specific. I think one could imagine (though barely) an implementation where char has stricter alignment than some type used for T and that reinterpret_cast would trigger a hardware exception. However, I consider this possibility rather academic.

    Further, the two functions would probably benefit from a compile-time assertion restricting T. Commonly, pointers, struct‘s containing pointers, and non-POD types shouldn’t be used with this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I implemented class that serves as TcpCLient server. Looks like this: { [Export] public
I have this: public class ClientSession : TcpClient { public int SessionGUID = 0;
I am having trouble reconnecting a TCPClient (actual class name from C#) to its
I've seen some people creating a is-a relationship like the following: class TCPClient :
I have 3 types, (TCPClient, string, class) Note that the class is defined at
When my Tcpclient is working , with this code : TCPClient.Disconnect; TCPClient.Connect; I get
I have small TcpClient app to connect to a server using SSL (SSlStream class)
Sometimes when reading a socket from the TcpClient class , the stream comes in
I wrote a small tcp client using boost::asio, providing the following function: typedef boost::function<void(const
I'm designing classes for my application (network tool). I this base class: class Descriptor

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.