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

  • Home
  • SEARCH
  • 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 6926745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:57:31+00:00 2026-05-27T10:57:31+00:00

I need to generate a random 128 byte key (Strength of randomness is not

  • 0

I need to generate a “random” 128 byte key (Strength of randomness is not important at the moment). I do this in Javascript with the following code:

var buffer = "";
for(var i=0; i<128; i++)
{
    buffer += String.fromCharCode(Math.round(Math.random()*255));
}

However, when I send this key to a PHP script via POST, I find that certain characters in my key do not have the same encoding! For example when I output the encoding of ò in Javascript I get 254, yet the same character has an encoding of 195 in PHP.

Certain characters, such as A-Z, a-z, and 0-9 have the same encoding in both Javascript and PHP.

To output character codes, I use Javascript’s .charCodeAt() method and PHP’s ord() function.

I was hoping someone could explain to me why the character encodings differ. Thank you!

  • 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-27T10:57:32+00:00Added an answer on May 27, 2026 at 10:57 am

    Javascripts .charCodeAt() returns the Unicode code point for each string character. Strings in Javascript use UCS-2 or UTF-16.

    PHP on the other hand only treats strings as streams of bytes. It doesn’t know much of charsets actually. Basically it considers strings ASCII or Latin-1 per default. (It’s binary-safe at least.)

    Now parameters transferred via URL or form values usually get encoded as UTF-8. That will work in PHP, as UTF-8 was specifically designed to work with systems that are unaware of its existence.

    The UTF-8 encoding of ò is "\xC3\xB2". So when you access the first character in PHP with $string[0] it will only see the first byte, which is hex C3 or decimal 195.

    There are the mb_string functions in PHP however to deal with UTF-8 etc, if you need it. (The workaround here is to convert the string from UTF-8 to UCS-2 and then extract the first word to get the Unicode code point. Or longwinded approaches like How to get code point number for a given character in a utf-8 string?)

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

Sidebar

Related Questions

I need to generate random string for my automated tests. I use the following
Hi I need to generate a random number in JavaScript between 15 and 225
what's the equivalent of this function in javascript: http://php.net/manual/en/function.uniqid.php Basically I need to generate
I need to generate a random number, now this number needs to be somewhere
I need to generate a random Date. I do not need time in my
I need to generate a nine digit numeric code (random preferably) which is unique
I need to generate multiple random values under SQL Server 2005 and somehow this
Like my question, i need to generate random numbers that have identical pairs between
I need to generate a random port number between 2000-65000 from a shell script.
I need to generate an random array from a bigger array of int without

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.