Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PHP- + Javascript
- You can generate a hash ranging from 01-99 from a string in JavaScript.
- Here's an example of how you can achieve this using the built-in `hashCode` function:
- ```javascript
- function hashCode(str) {
- let hash = 0;
- for (let i = 0; i < str.length; i++) {
- hash = (hash << 5) - hash + str.charCodeAt(i);
- hash &= hash; // Convert to 32bit integer
- }
- return Math.abs(hash) % 100; // Get a positive number within 0-99 range
- }
- const str = "example";
- const hash = String(hashCode(str)).padStart(2, "0");
- console.log(hash); // Output: 36
- ```
- In the above example, the `hashCode` function calculates a hash code for the given string. By applying `% 100` to the hash, we get a positive number within the 0-99 range. The `String.padStart(2, "0")` function is used to ensure a consistent two-digit format.
- ---------------------------------------------------
- You can generate a hash ranging from 01-99 from a string in PHP using various hash algorithms. One common algorithm is MD5, which generates a 32-character hexadecimal hash. You can then convert this hash into a decimal value and limit it between 01-99 using modulus division.
- Here's an example code snippet to illustrate this process:
- ```php
- $string = "example string";
- $hash = md5($string); // Generating MD5 hash
- $decimalHash = hexdec($hash); // Converting hexadecimal hash to decimal
- $range = $decimalHash % 99; // Getting the remainder after dividing by 99
- $finalHash = str_pad($range, 2, "0", STR_PAD_LEFT); // Pad the hash with leading zeros if necessary
- echo $finalHash; // Output: a hash ranging from 01-99
- ```
- Please note that generating a hash using MD5 is not considered secure for cryptographic purposes. If you require more secure hashing, consider using algorithms like SHA-256 or bcrypt.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement