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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:31:31+00:00 2026-05-18T02:31:31+00:00

I’m sure this is a very common operation when doing any kind of network

  • 0

I’m sure this is a very common operation when doing any kind of network programming (or I/O with files) but I can’t find the answer.

Basically I have a datagram packet coming in down the network which has a series of single precision floating point numbers (4 bytes each).

I have written some basic networking code which reads from a socket and stores the data into a buffer which is declared as follows:

char buffer[24];

This is my deserialization code:

for (int i=0; i<6; i++) {
    float *pf = reinterpret_cast<float*>(buffer + i*sizeof(float));
    printf("%f\n", *pf);
}

but it causes my program to crash.

If someone could point me to a good tutorial on this type of thing i.e. managing, storing and interpreting data, I’d really appreciate it! I’ve looked but I don’t know what to search for.

  • 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-18T02:31:31+00:00Added an answer on May 18, 2026 at 2:31 am

    If they are really sending floats — which I wouldn’t recommend, if you have a choice — then you have to do some bit hackery.

    Here is a utility library I wrote to do just this kind of thing. It works on Windows (MSVC) and Linux (GCC). It might not work on other platforms! Use at your own risk.

    /**************  UTILITY ******************************/
    template<class Val> Val ntohx(const Val& in)
    {
        char out[sizeof(in)] = {0};
        for( size_t i = 0; i < sizeof(Val); ++i )
            out[i] = ((char*)&in)[sizeof(Val)-i-1];
        return *(reinterpret_cast<Val*>(out));
    }
    
    template<> uint16_t ntohx<uint16_t>(const uint16_t & v)
    {
        return ntohs(v);
    }
    
    template<> uint32_t ntohx<uint32_t>(const uint32_t & v)
    {
        return ntohl(v);
    }
    
    template<> uint64_t ntohx<uint64_t>(const uint64_t & v)
    {
        uint32_t ret [] =
        {
            ntohl(((const uint32_t*)&v)[1]),
            ntohl(((const uint32_t*)&v)[0])
        };
        return *((uint64_t*)&ret[0]);
    }
    template<> float ntohx<float>(const float& v)
    {
        uint32_t const* cast = reinterpret_cast<uint32_t const*>(&v);
        uint32_t ret = ntohx(*cast);
        return *(reinterpret_cast<float*>(&ret));
    }
    
    template<class Val> bool ieee_isnan(const Val& val)
    {
        // According to the IEEE Standard for floating-point numbers, 
        // NaNs have the interesting attribute of always returning
        // false in comparisons; even to themselves.
        // All platforms we currently support use IEEE floating points,
        // so this should work. [Dib]
        return val != val;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
This could be a duplicate question, but I have no idea what search terms
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

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.