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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:26:39+00:00 2026-06-13T06:26:39+00:00

I’ve a fallowing code which receives byte and probably has to perform convert to

  • 0

I’ve a fallowing code which receives byte and probably has to perform convert to float and represent its converted values :

 public float DecodeFloat(byte[] data)
    {
        float x = data[3]|data[2]<<8|data[1]<<16|data[0]<<24;
        return x;
    }

     // receive thread 
  private  void ReceiveData() 
    {
        int count=0;

       IPEndPoint remoteIP = new IPEndPoint(IPAddress.Parse("10.0.2.213"), port);
       client = new UdpClient(remoteIP);
        while (true) 
        {
           try 
            {
                IPEndPoint anyIP = new IPEndPoint(IPAddress.Any, 0);
                byte[] data = client.Receive(ref anyIP);

                Vector3 vec,rot;
                float x= DecodeFloat (data);
                float y= DecodeFloat (data + 4);
                float z= DecodeFloat (data + 8);
                float alpha= DecodeFloat (data + 12);
                float theta= DecodeFloat (data +16);
                float phi= DecodeFloat (data+20);

                vec.Set(x,y,z);
                rot.Set (alpha,theta,phi);


                print(">> " + x.ToString() + ", "+ y.ToString() + ", "+ z.ToString() + ", "
                    + alpha.ToString() + ", "+ theta.ToString() + ", "+ phi.ToString());

                // latest UDPpacket
                lastReceivedUDPPacket=x.ToString()+" Packet#: "+count.ToString();
                count = count+1;



            }

Is there anyone to put me in the right way, please?

  • 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-06-13T06:26:40+00:00Added an answer on June 13, 2026 at 6:26 am

    Given 4 bytes, you would normally only “shift” (<<) if it is integer data. The code in the question basically reads the data as an int (via “shift”), then casts the int to a float. Which is almost certainly not what was intended.

    Since you want to interpret it as float, you should probably use:

    float val = BitConverter.ToSingle(data, offset);
    

    where offset is the 0, 4, 8, 12 etc shown in your data + 4, data + 8, etc. This treats the 4 bytes (relative to offset) as raw IEEE 754 floating point data. For example:

    float x= BitConverter.ToSingle(data, 0);
    float y= BitConverter.ToSingle(data, 4);
    float z= BitConverter.ToSingle(data, 8);
    float alpha= BitConverter.ToSingle(data, 12);
    float theta= BitConverter.ToSingle(data, 16);
    float phi= BitConverter.ToSingle(data, 20);
    

    Note that this makes assumptions about “endianness” – see BitConverter.IsLittleEndian.


    Edit: from comments, it sounds like the data is other-endian; try:

    public static float ReadSingleBigEndian(byte[] data, int offset)
    {
        if (BitConverter.IsLittleEndian)
        {
            byte tmp = data[offset];
            data[offset] = data[offset + 3];
            data[offset + 3] = tmp;
            tmp = data[offset + 1];
            data[offset + 1] = data[offset + 2];
            data[offset + 2] = tmp;
        }
        return BitConverter.ToSingle(data, offset);
    }
    public static float ReadSingleLittleEndian(byte[] data, int offset)
    {
        if (!BitConverter.IsLittleEndian)
        {
            byte tmp = data[offset];
            data[offset] = data[offset + 3];
            data[offset + 3] = tmp;
            tmp = data[offset + 1];
            data[offset + 1] = data[offset + 2];
            data[offset + 2] = tmp;
        }
        return BitConverter.ToSingle(data, offset);
    }
    ...
    float x= ReadSingleBigEndian(data, 0);
    float y= ReadSingleBigEndian(data, 4);
    float z= ReadSingleBigEndian(data, 8);
    float alpha= ReadSingleBigEndian(data, 12);
    float theta= ReadSingleBigEndian(data, 16);
    float phi= ReadSingleBigEndian(data, 20);
    

    If you need to optimize this massively, there are also things you can do with unsafe code to build an int from shifting (picking the endianness when shifting), then do an unsafe coerce to get the int as a float; for example (noting that I haven’t checked endianness here – it might misbehave on a big-endian machine, but most people don’t have those):

    public static unsafe float ReadSingleBigEndian(byte[] data, int offset)
    {
        int i = (data[offset++] << 24) | (data[offset++] << 16) |
                (data[offset++] << 8) | data[offset];
        return *(float*)&i;
    }
    public static unsafe float ReadSingleBigEndian(byte[] data, int offset)
    {
        int i = (data[offset++]) | (data[offset++] << 8) |
                (data[offset++] << 16) | (data[offset] << 24);
        return *(float*)&i;
    }
    

    Or crazier, and CPU-safer:

    public static float ReadSingleBigEndian(byte[] data, int offset)
    {
        return ReadSingle(data, offset, false);
    }
    public static float ReadSingleLittleEndian(byte[] data, int offset)
    {
        return ReadSingle(data, offset, true);
    }
    private static unsafe float ReadSingle(byte[] data, int offset,
        bool littleEndian)
    {
        fixed (byte* ptr = &data[offset])
        {
            if (littleEndian != BitConverter.IsLittleEndian)
            {   // other-endian; swap
                byte b = ptr[0];
                ptr[0] = ptr[3];
                ptr[3] = b;
                b = ptr[1];
                ptr[1] = ptr[2];
                ptr[2] = b;
            }
            return *(float*)ptr;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an array which has BIG numbers and small numbers in it. I
I'm trying to select an H1 element which is the second-child in its group
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 want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.