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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:14:45+00:00 2026-05-27T04:14:45+00:00

I need help converting this code from C# to Java. public static ulong GetChecksum(byte[]

  • 0

I need help converting this code from C# to Java.

    public static ulong GetChecksum(byte[] data)
    {
        ulong sum = 0;
        foreach (var item in data)
            sum += item;

        return ulong.MaxValue - sum + 1;
    }

The Java version would return long, instead ulong.

Edit: I haven’t tried anything, because I don’t know how to handle the last line ulong.MaxValue. Basically, what I need for the end result is to produce the same number that is (long)GetChecksum(buffer) in C# to be the same as getChecksum(buffer) in java.
I’m very new to java, which is why I ask this.

Edit2: Here is the final solution. Many helpful comments. Thank you guys.

public static byte[] getChecksum(byte[] data) {
        BigInteger sum = BigInteger.ZERO;
        for (byte s : data)
            sum = sum.add(BigInteger.valueOf((long) ((char) s & 0xFF)));

        return new BigInteger("FFFFFFFFFFFFFFFF", 16).subtract(sum).add(BigInteger.ONE).toByteArray();
    }

Edit2: Too bad this question got downrated. It has many useful comments.

  • 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-27T04:14:45+00:00Added an answer on May 27, 2026 at 4:14 am

    Since Java does not throw errors on arithmetic overflow, you should be able to do something like:

    long sum = 0;
    for (byte b : data)
        sum += (long) b;
    
    return sum;
    

    The return was simplified based on the assumption that you want the exact same result (in binary rather than in value) as in C#. ulong.MaxValue is 64 bits of 1, which is -1 in signed long which cancels the “+1”.

    Edit: I told a lie here. The result should be logically -sum, which due to the default two’s compliment implementation, can’t be easily done. You should consider the BigInteger solution instead.

    Edit2: Because signed subtraction behaves non-uniformly, the unsigned operation ulong.MaxValue - sum can’t be simulated trivially. What I suggest you do is:

    long sum = 0;
    for (byte b : data)
        sum += ((long) b) & 0xFFL; // To prevent sign-expansion. Sorry, I missed this the first time as well.
    
    return (new BigInteger("FFFFFFFFFFFFFFFF", 16)).subtract((new BigInteger(Long.toHexString(sum), 16))).add(BigInteger.ONE);
    

    This looks ugly but I tried my best to use native arithmetic as much as possible.

    You can use BitInteger’s “toByteArray()” method to get the raw binary for comparison. Do not use longValue() as it destroys the first bit.

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

Sidebar

Related Questions

Converting to ODB.NET from System.Data.OracleClient and need help converting my connection string. Here is
Need help writing a script downloads data from google insight using c# this is
I need help converting this into PHP: Public Function Encrypt(ByVal text As String) As
I need help converting a VB.NET handles statement to C#. This is the VB
I need help on this following aspx code aspx Code: <asp:Label ID =lblName runat
I am converting code from c# to java. I have the following xml structure:
I need help converting eregi_replace to preg_replace (since in PHP5 it's depreciated): function makeClickableLinks($text)
I need help with this route map routes.MapRoute(Blog_Archive, Blog/Archive/{year}/{month}/{day}, new { controller = Blog,
I'm having some problems with converting some JSON and I'm after some help. This
I need to convert MimeMessage into byte array, but while converting some characters are

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.