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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:04:32+00:00 2026-05-24T03:04:32+00:00

I have the following C function: unsigned int DJBHash(char* str, unsigned int len) {

  • 0

I have the following C function:

unsigned int DJBHash(char* str, unsigned int len)
{
   unsigned int hash = 5381;
   unsigned int i    = 0;

   for(i = 0; i < len; str++, i++)
   {
      hash = ((hash << 5) + hash) + (*str);
   }

   return hash;
}

I’m trying to convert it to Javascript. I’m having trouble with the (*str) part of line 8

(`hash=((hash << 5) + hash) + (*str)`).

How can I efficiently convert my javascript string into the same representation as is done in C?

Here’s what I’ve done sofar, but it’s not working: when I add zero to “str”, it simply appends a character “0” to my str. What am I doing wrong?

function DJBHash(str,len){
        var hash=5381;
        var i=0;

        for(i=0;i<len;i++){
                hash=((hash<<5)+hash)+(str+0);
        }
        return hash;
}
  • 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-24T03:04:33+00:00Added an answer on May 24, 2026 at 3:04 am

    There are no pointers in Javascript. Treat the input as a string instead of a pointer to a string. The string has a length, so you don’t need to send that as a parameter, and the string object has the charCodeAt method that you can use to get the character code of a specific character during the loop:

    function DJBHash(str) {
      var hash = 5381;
      for(var i = 0; i < str.length; i++) {
        hash = ((hash << 5) + hash) + str.charCodeAt(i);
      }
      return hash;
    }
    

    However, the C code might rely on the int to have a specific size (which is however not according to the C specifications), to use the overflow to limit the result to a specific number of bits. As Javascript doesn’t have any integer type, you would have to use integer operations to limit the result in the same way. This would produce a 32 bit result:

    function DJBHash(str) {
      var hash = 5381;
      for(var i = 0; i < str.length; i++) {
        hash = (((hash << 5) + hash) + str.charCodeAt(i)) & 0xffffffff;
      }
      return hash;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following function: CREATE FUNCTION fGetTransactionStatusLog ( @TransactionID int ) RETURNS varchar(8000)
Let's say I have the following function: sumAll :: [(Int,Int)] -> Int sumAll xs
I have the following Oracle function: function get_job_no return number is V_job_no number; begin
I have the following function that will convert a string into a numeric data
I have the following function: void writeResults(FILE* fp, FILE* fpw, Vector w, int size)
I have the following function: <script type=text/javascript> function changeText(elem){ var oldHTML = document.getElementById(elem).innerHTML; var
I have the following function to convert an integer of arbitrary size to a
i have an unsigned char test_SHA1[20] of 20 bytes which is a return value
I have the following C code: static void* heap; static unsigned int ptr; int
I have the following function that is pulling data from a database. The ajax

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.