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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:59:13+00:00 2026-05-31T06:59:13+00:00

I know there is a missing cast to long, but is there a better

  • 0

I know there is a missing cast to long, but is there a better way to do this which is less confusing than the shown approch.

static long getLong(byte[] sourceBytes, int sourceBytesIndex, int numOfBytesToConvert)
{
    long longValue = 0;

    longValue = (sourceBytes[sourceBytesIndex] & 0xFF) +
                         ((sourceBytes[sourceBytesIndex + 1] & 0xFF) << 8);

    if (numOfBytesToConvert > 2)
    {
        longValue += ((sourceBytes[sourceBytesIndex + 2] & 0xFF) << 16) +
                                ((sourceBytes[sourceBytesIndex + 3] & 0xFF) << 24);

        if (numOfBytesToConvert > 4)
        {
            longValue += ((sourceBytes[sourceBytesIndex + 4] & 0xFF) << 32) +
                                    ((sourceBytes[sourceBytesIndex + 5] & 0xFF) << 40);

            if (numOfBytesToConvert > 6)
            {
                longValue += ((sourceBytes[sourceBytesIndex + 6] & 0xFF) << 48) +
                                        ((sourceBytes[sourceBytesIndex + 7] & 0xFF) << 56);
            }
        }
    }

    return longValue;
}
  • 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-31T06:59:14+00:00Added an answer on May 31, 2026 at 6:59 am

    I prefer to use ByteBuffers, you can also use a switch statement.

    static long getLong(ByteBuffer bb, int numOfBytesToConvert) {
        switch (numOfBytesToConvert) {
            case 8:
                return bb.getLong();
            case 6:
                long aChar = bb.getChar();
                long anInt = bb.getInt() & 0xFFFFFFFFL;
                return bb.order() == ByteOrder.LITTLE_ENDIAN
                        ? aChar << 32 + anInt
                        : anInt << 16 + aChar;
            case 4:
                return bb.getInt() & 0xFFFFFFFFL;
            case 2:
                return bb.getChar();
            default:
                throw new IllegalArgumentException();
        }
    }
    

    The ByteBuffer handles both byte endianess as well the position and the end of usable bytes in the buffer. (Using limit())

    I tend to prefer direct ByteBuffers as there can be large without using much heap and are faster than a byte[] when using the native byte order.

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

Sidebar

Related Questions

Alright, I know there's a simple way to do this, but it's been years
I'm sure there's something obvious I'm missing here, but I know that popover controls
I know there have been a few threads on this before, but I have
I know there are a lot of questions on here about this, but most
I know there are many other questions similar to this one, but none of
I know there are plenty of questions regarding this error, but I haven't found
I know there is something dead simple I'm missing, but the following code won't
Is there a way to know which key was involved when a call like
I know there have been plenty of variations on this question before, but none
I know there have been already questions around this topic, but I have not

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.