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

  • Home
  • SEARCH
  • 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 6691889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:48:15+00:00 2026-05-26T05:48:15+00:00

I got a Problem. I’ve got a MODBUS TCP answer, which is hex coded

  • 0

I got a Problem. I’ve got a MODBUS TCP answer, which is hex coded like this: 0 0 0 0 0 7 1 4 4 41 B8 66 64. For explanation: the first five zeroes are a specification of Modbus, the 7 are the numbers of bytes following after it. the 1 is the client address in the Modbus network and not relevant. the first 4 is the function code which is used. the second 4 is again the numbers of bytes following. The last four bytes is the hex coded answer, which should be converted to double. The array, in which this is stored is a unsigned char array.
Here a few examples how i tried it.
Here the first example:

value = (ibuf[9]<<24) + (ibuf[10]<<16) + (ibuf[11]<<8) + ibuf[12];

Value is the used double variable and ibuf the used char array.
Here the second one:

for(i = 0; i < k; i++)
{
    if (i==0)
    {
        sprintf(ergebnis, "%x%x", ibuf[9], ibuf[10]);
    }
    else
    {
        //sprintf(buffer,"%x%x",ibuf[9+i+i], ibuf[10+i+i]);
        //strcat( ergebnis, buffer );
        sprintf(ergebnis, "0x41b451e8");
        sscanf(ergebnis, "%l %lf ", &Value);
    }
    printf("Ergebnis %s\n", ergebnis);
}

Here I used a fixed value, but the problem is always the conversion from hex to double.
I’m glad about every help I could get.

  • 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-26T05:48:15+00:00Added an answer on May 26, 2026 at 5:48 am

    You’re almost there. Unfortunately, conversion occurs automatically during promotion – the most “straightforward” way of circumventing this is through dereferencing a float pointer cast of an int, as so:

    int ival = (ibuf[9]<<24)+(ibuf[10]<<16)+(ibuf[11]<<8)+ibuf[12];
    float fval = *(float*)&ival;
    

    For the data supplied, this gives 23.049995 instead of 1.1026039e+009, which you get from conversion.

    EDIT:

    As Mike Seymour pointed out in the comment below, a preferred way to write this is:

    float fval = *reinterpret_cast<float*>(&ival);
    

    Note that you cannot do this:

    float fval = reinterpret_cast<float>(ival);
    

    This will raise an error (as seen here in VS2005):

    error C2440: 'reinterpret_cast' : cannot convert from 'int' to 'float'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got problem with some special characters. I have defined the meta tag as
I got problem with two AJAX calls. I'm calling it from a jquery object.
I got problem inserting the string AM/PM to my mySQL table $timeFormat = mysql_real_escape_string($_POST['timeFormat']);
Hi I've got problem with foreach statement. I have my controller method: public ActionResult
I've got problem with sorting entries in JqGrid. Orderby seem to not work. I
I've got problem while including header files, in every header file i use #pragma
hi i am working with Twitter, i got problem with images, i have tweet
I just want to trace boundaries in my gray image. I got problem by
I made a custom drop-down list. And I want it to function like the
I have a bunch of posts stored in text files formatted in yaml/textile (from

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.