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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:26:45+00:00 2026-05-13T08:26:45+00:00

I have a little problem where need to do a hash of a number

  • 0

I have a little problem where need to do a hash of a number of about 10 digits into a number of 6 digits. The hash needs to be deterministic.

It’s more important that the hash is not resource intensive.

For example, say that I have some number, x, like 123456789

I want to write an hash function that gives me a number, y, back like 987654.

I’d then like to have a function that takes the x and y as parameters, re-applies the hash on x, and checks that the result is y.

It should be difficult to compute possible input values given the hash.

My first idea of multiplying pairs of digits led to a lot of duplicate hashed values.

I have the feeling that this sort of problem has some kind of elegant solution, but I just can’t think of it myself.

Can anyone help me out here? Thanks in advance 🙂

  • 1 1 Answer
  • 1 View
  • 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-13T08:26:45+00:00Added an answer on May 13, 2026 at 8:26 am

    What you want to do is to try to distribute the hash values as evenly as possible over the range. Some of the built in hashing methods are fairly good at this, so you could perhaps try something like getting the hash code of the string representation, and simply throw away half of the bits:

    ushort code = (ushort)value.ToString().GetHashCode();
    

    However, it also depends on what you are going to use the hash code for. The built in hash codes are not intended to be stored permanently. The algorithms for calculating the hash codes can change with any new version of the framework, so if you store the hash codes in the database they may become useless in the future. In that case you would instead have to create the hashing algorithm yourself from scratch, or use some hashing algorithm that was designed for permanent storage.

    One simple algorithm that is used for hash codes for some values in the framework is to use exclusive or to make all bits in the value matter when the hash code is smaller than the data:

    byte[] b = BitConverter.GetBytes(value);
    ushort code = (ushort)(BitConverter.ToUInt16(b, 0) ^ BitConverter.ToUInt16(b, 2));
    

    or the more efficient but less obvious way to do the same:

    ushort code = (ushort)((value >> 16) ^ value);
    

    This of course has no obfuscating properties for small values, so you might want to throw in some “random” bits to make the hash code significantly different from the value:

    ushort code = (ushort)(0x56D4 ^ (value >> 16) ^ value);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran into a little problem and need some help: If I have an
I have a little problem with preg_replace . I need a function that removes
I have little unusual problem to solve. I need some hint or links to
I have a little problem, I need to make directory from my c code
i have a little problem and see no solution for it. I need flexible
i have a little problem, i'm searching, but i can't found what i need,
I need a little push in the right direction. Here's my problem: I have
I have a little problem about the sort direction of a specific column in
I have a little Problem with the app that I'm developing. In the App
I have a little problem. I need to do some little operations on quite

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.