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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T19:41:25+00:00 2026-05-18T19:41:25+00:00

I have a byte array sent via UDP from x-plane. The bytes (4) are

  • 0

I have a byte array sent via UDP from x-plane. The bytes (4) are all floats or integers…
I tried to cast them to floats but no luck so far…

Example array:
byte data[41] = {-66,30,73,0};

How do I convert 4 bytes into int or float and doesn’t float use 8 bytes?

  • 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-18T19:41:26+00:00Added an answer on May 18, 2026 at 7:41 pm

    Note: I recommend @Ophidian’s ByteBuffer approach below, it’s much cleaner than this. However this answer can be helpful in understanding the bit arithmetic going on.

    I don’t know the endianness of your data. You basically need to get the bytes into an int type depending on the order of the bytes, e.g.:

    int asInt = (bytes[0] & 0xFF) 
                | ((bytes[1] & 0xFF) << 8) 
                | ((bytes[2] & 0xFF) << 16) 
                | ((bytes[3] & 0xFF) << 24);
    

    Then you can transform to a float using this:

    float asFloat = Float.intBitsToFloat(asInt);
    

    This is basically what DataInputStream does under the covers, but it assumes your bytes are in a certain order.

    Edit – On Bitwise OR

    The OP asked for clarification on what bitwise OR does in this case. While this is a larger topic that might be better researched independently, I’ll give a quick brief. Or (|) is a bitwise operator whose result is the set of bits by individually or-ing each bit from the two operands.

    E.g. (in binary)

       10100000
    |  10001100
    -----------
       10101100
    

    When I suggest using it above, it involves shifting each byte into a unique position in the int. So if you had the bytes {0x01, 0x02, 0x03, 0x04}, which in binary is {00000001, 00000010, 00000011, 00000100}, you have this:

                                      0000 0001   (1)
                            0000 0010             (2 <<  8)
                  0000 0011                       (3 << 16)
      | 0000 0100                                 (4 << 24)
      --------------------------------------------------------
        0000 0100 0000 0011 0000 0010 0000 0001   (67 305 985)
    

    When you OR two numbers together and you know that no two corresponding bits are set in both (as is the case here), bitwise OR is the same as addition.

    See Also

    • Wikipedia: Bitwise OR
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a byte array in memory, read from a file. I would like
I have byte array as input. I would like to convert that array to
I have a byte[] array, the contents of which represent a TIFF file (as
I have a byte array that represents a complete TCP/IP packet. For clarification, the
I have an EXE loaded into a byte array, and I am trying to
Pretty simple scenario. I have a web service that receives a byte array that
Overview: I have an array of 20 byte strings that needs to be stored
I have to process a large byte array that is passed to my function.
I currently have a method that receives a byte array of an excel file.
I have a byte array and I want to read this array byte by

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.