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

  • Home
  • SEARCH
  • 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 7398431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:52:08+00:00 2026-05-29T03:52:08+00:00

I wanted to check if there was a way of encoding an integer into

  • 0

I wanted to check if there was a way of encoding an integer into bytes that would be binary comparable.

Example: say I have integers -1 and 1

a) Write -1 to byte array b1[], write 1 to b2[]

b) b1[] should compare less than b2[]

If we write in big endian order I believe it works for positive integers but not negative ones.

I am trying to do this for a Hadoop custom raw key comparator…

Thanks

Ajai

  • 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-29T03:52:08+00:00Added an answer on May 29, 2026 at 3:52 am

    This is in C#, but you should be able to do something similar to this in your language of choice, it’ll give you the correct sort order for positive as well as negative integers.
    Note that you need to use an unsigned byte type to sort correctly.

    static byte[] EncodeToBytes(int val)
    {
        val -= int.MinValue;
        var result = new byte[4];
        for (int i = 0; i < 4; i++, val <<= 8)
            result[i] = (byte)(val >> 24);
        return result;
    }
    
    static int DecodeFromBytes(byte[] val)
    {
        var result = 0;
        for (int i = 0; i < 4; i++)
            result = (result << 8) | (val[i] & 0xff);
        return result + int.MinValue;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I assume not, but I just wanted to check - is there any way
Just wanted to check to see if there's a more elegant way to accomplish
Just wanted to check if there is way to do distinct by multiple columns.
I wanted to check whether the variable is defined or not. For example, the
Is there a way to disable the drag&drop functionality for Solution Explorer? Why would
Is there a way to access a multidimensional array from any position? example: $arr
Is there any way of executing the already executed code in java. for example
I have a small program that has several checkedboxlists in VS2010. I wanted to
I have a very uncommon font that I would like to use for a
If I wanted to find checked check boxes on an ASP.NET page I could

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.