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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:56:35+00:00 2026-06-14T20:56:35+00:00

I have written a generator of strings, but I don’t know how to create

  • 0

I have written a generator of strings, but I don’t know how to create a random hex string with length, for instance 100 digits, for inserting into a database. All these strings have to be same length.

How can I generate random hex strings?

  • 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-06-14T20:56:36+00:00Added an answer on June 14, 2026 at 8:56 pm

    While this answers OP’s question, if what you are looking for is random, then @danorton answer may be a better fit.


    Like this:

    $val = '';
    for( $i=0; $i<100; $i++ ) {
       $val .= chr(rand(65, 90));
    }
    

    65 is A while 90 is Z. if you do not like "magic numbers" this form may be more readable:

    $val = '';
    for( $i=0; $i<100; $i++ ) {
       $val .= chr(rand(ord('A'), ord('Z')));
    }
    

    I’d make ord() result a variable and move it out of the loop though for performance reasons:

    $A = ord('A');
    $Z = ord('Z');
    $val = '';
    for( $i=0; $i<100; $i++ ) {
       $val .= chr(rand($A, $Z));
    }
    

    Or you could glue output of sha1()s (three of them) and cut down to 100 chars. Or use md5() instead (but I’d stick to sha1()).

    EDIT sha1() outputs 40 chars long string, md5() 32 chars long. So if you do not want to glue char by char (as in loop I gave above) try this function

    function getId($val_length) {
        $result = '';
        $module_length = 40;   // we use sha1, so module is 40 chars
        $steps = round(($val_length/$module_length) + 0.5);
    
        for( $i=0; $i<$steps; $i++ ) {
          $result .= sha1(uniqid() . md5(rand());
        }
    
        return substr($result, 0, $val_length);
    }
    

    where function argument is length of string to be returned. Call it getId(100);

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

Sidebar

Related Questions

I have list of unique variable length strings composed of letters, numbers and special
I have written a parser-generator that has expressions like this: a = f:foo bar
I have written code to perform click event on JMenuItem but that is not
I'm testing a self written element generator ( ICollection<string> ) and compare the calculated
have written this little class, which generates a UUID every time an object of
I have written a java program generates lots of files (say txt files) in
I have written a Python script that will generate a series of graphs and
I have written an O/R database wrapper that generates some wrapper methods for stored
I have written a simple Java class to generate the hash values of the
I have a web application written in pure JavaScript (no pre-generated HTML except for

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.