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 6715109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:34:31+00:00 2026-05-26T08:34:31+00:00

I’m trying to write some code that uses boost::asio’s sockets to send a message

  • 0

I’m trying to write some code that uses boost::asio’s sockets to send a message from one end (the client) to another (the server).

My particular goal right now is to prepend every message being sent with a 1-byte unsigned integer (uint8_t) telling the receiver how many bytes long the rest of the message is.

Note that the reason I’m storing and transferring the size as a uint8_t instead of a size_t is because I want to ensure that it will be exactly 1 byte on both machines. A little googling has indicated that size_t can be of a different size on machine A versus machine B, which could mess things up.

So here I’m calculating the length of the message. The message consists of just two parts: a uint16_t, followed by a variable “data” which is a (void*)-cast chunk of memory that will serve varying purposes (the target machine has ways of interpreting that data, but I won’t get into that here).

uint8_t sizeOfMessage = sizeof(uint16_t) + sizeof(data);

In this example, sizeof(data) == 4, so sizeOfMessage should equal 6 (since uint16_t is a 2-byte integer). However, if I std::cout << sizeOfMessage, what’s displayed is not 6, but a special character: a spade (as in the suit of cards). Looking at the data in VS2010’s debugger, it shows up as 6 ‘-‘ . However, if I define sizeOfMessage as a size_t and print that out, everything is fine (but of course, I can’t send that over the network because the size in bytes of a size_t is not guaranteed from one machine to the next).

I take this to mean something is going wrong in how size_t is being cast into a uint8_t after the arithmetic. sizeof returns a size_t, so what my code is doing is adding two size_t’s and then casting that into a uint8_t.

Am I correct in my conclusion that the value is not being cast correctly? If so, how can I solve this?

Thanks in advance.

  • 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-26T08:34:31+00:00Added an answer on May 26, 2026 at 8:34 am

    The value being assigned to your little (uint8_t) variable is not a printable ASCII character code that would display like ‘6’. Instead, it is a binary value, which is equivalent to coding ‘\006’ in your source code. If your data transfer protocol does work with binary code bytes, you’re probably good to go. What does the receiving program get in its instance of (uint8_t) which it reads one byte (char) into? And of course using (uint8_t) limits your total message length to 0 through 255 (but you knew that).

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

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

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.