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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:22:14+00:00 2026-05-22T16:22:14+00:00

Possible Duplicate: Convert 4 bytes to int I’m trying to pack 4 bytes into

  • 0

Possible Duplicate:
Convert 4 bytes to int

I’m trying to pack 4 bytes into an int using some of the solutions found here, but it doesn’t seem to work for one of my tests.

This is the code I’m using:

public static int pack(int c1, int c2, int c3, int c4)
{
    return (c1 << 24) | (c2 << 16) | (c3 << 8) | (c4);
}

Now when I use it on something simple like 0x34, 0x68, 0x77, and 0x23 I get what I expect: 0x34687723. But when I use it on 0xBA, 0xAD, 0xBE, and 0xEF I get something way off. Does anyone see what the problem might be?

EDIT

The above code was able to give me what I wanted, and the “wrong value” I mention below is just another way of representing 0xBAADBEEF in a decimal form.

  • 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-22T16:22:15+00:00Added an answer on May 22, 2026 at 4:22 pm

    A number stored in a Java int can only represent positive values up to 0x7fffffff (2147483647, Integer.MAX_VALUE), as it’s a signed type (like all Java number types), and in the internal representation the most significant bit is used as a sign bit.

    To hold positive numeric values larger than that you need to use a long instead:

    public static long pack(int c1, int c2, int c3, int c4)
    {
            return ((c1 << 24) | (c2 << 16) | (c3 << 8) | (c4)) & 0xffffffffL;
    }
    

    Note the explicit long mask operation at the end which is necessary to ensure that sign extension doesn’t cause a negative integer result from being turned into a negative long.

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

Sidebar

Related Questions

Possible Duplicate: Convert Array into a primative method to express the same result, using
Possible Duplicate: convert string to 2D array using php I have the string like
Possible Duplicate: Convert HTML to PDF in .NET I am using VSTS 2008 +
Possible Duplicate: Convert some code from C++ to C I've got some code that
Possible Duplicate: C++ convert int and string to char* Hello, i am making a
Possible Duplicate: Convert string to DateTime in c# I am trying to convert a
Possible Duplicate: difference between Convert.ToInt32 and (int) What it difference between (int) and convert.toint()
Possible Duplicate: how can I convert String to Int ? Hi, I have the
Possible Duplicate: Convert std::string to const char* or char* Simple question but I'm new
Possible Duplicate: How to convert an ArrayList containing Integers to primitive int array? How

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.