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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T17:53:20+00:00 2026-05-29T17:53:20+00:00

I am converting the incoming string into hash code by doing the following function

  • 0

I am converting the incoming string into hash code by doing the following function but some of the values are negative. I don’t think hash values should be negative. Please tell me what I am doing wrong.

int combine = (srcadd + dstadd + sourceport + destinationport + protocol).hashCode();
System.out.println(combine);
  • 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-29T17:53:29+00:00Added an answer on May 29, 2026 at 5:53 pm

    I don’t think hash values should be negative.

    Why not? It’s entirely valid to have negative hash codes. Most ways of coming up with a hash code naturally end up with negative values, and anything dealing with them should take account of this. However, I’d consider a different approach to coming up with your hash codes, e.g.

    int hash = 17;
    hash = hash * 31 + srcadd.hashCode();
    hash = hash * 31 + dstadd.hashCode();
    hash = hash * 31 + sourceport; // I'm assuming this is an int...
    hash = hash * 31 + destinationport; // ditto
    hash = hash * 31 + protocol.hashCode();
    return hash;
    

    It’s not clear what the types of these expressions are, but I’m guessing you’re ending up taking the hash code of a string… a string that you don’t really need to create in the first place. While there are better approaches for getting hash codes for known domains, the above approach works well as a general-purpose hash generation technique.

    Note that it would also help the readability of your code if you avoided abbreviations, and used camel casing, e.g. sourceAddress instead of srcadd.

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

Sidebar

Related Questions

While converting an old code, I encountered the following problem. Given an HTML string,
Imagine the following simple code: public void F<T>(IList<T> values) where T : struct {
Converting numbers to strings is no problem, but the string created uses ascii numbers.
Converting my current code project to TDD, I've noticed something. class Foo { public
Converting to ODB.NET from System.Data.OracleClient and need help converting my connection string. Here is
I'm converting an application to use Java 1.5 and have found the following method:
Converting a C++ string to a char array is pretty straightorward using the c_str
converting some CSS to Sass, for example: .ptn, .pvn, .pan{padding-top:0px !important} to this @mixin
Converting non-negative Integer to its list of digits is commonly done like this: import
Does converting to a string(i.e .toString() ) consumes lots of memory or even leads

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.