To the Top

Unicode-String/Text To ASCII Converter (Ajax Javascript)

Buy Me A Coffee

This page implements a Javascript Ajax converter that calls the API to convert a unicode string/text to ASCII number array (hexadecimal, binary, decimal, octal).

String-to-ASCII API (Application Programming Interface)

The String-to-ASCII API is implemented via CloudFlare Worker Serverless Function/API. It is live on CloudFlare Edge Network Nodes.

https://string-to-ascii.justyy.workers.dev/api/string-to-ascii/?cached&s=您+好!
It will return JSON-encoded data:
"230 130 168 32 229 165 189 33"
https://string-to-ascii.justyy.workers.dev/api/string-to-ascii/?cached&s=您+好!&n=2
It will return JSON-encoded data:
"11100110 10000010 10101000 00100000 11100101 10100101 10111101 00100001"
Parameter n can be 2, 8, 10 (default) or 16 representing, binary, octal, decimal and hexadecimal respectively.
Parameter d is the delimiter between numbers.

Complete API Source Code (PHP) of String-To-Ascii API

  $s = '';
  if (isset($_GET['s'])) {
     $s = $_GET['s'];
  } else {
    if (isset($_POST['s'])) {
     $s = $_POST['s'];
    }
  }
  $data = '';
  $ss = str_split($s);
  $n = 10;
  if (isset($_GET['n'])) {
     $n = (integer)$_GET['n'];
  }
  if (($n != 8) && ($n != 16) && ($n != 10) && ($n != 2)) {
     $n = 10;
  }
  $d = ' ';
  if (isset($_GET['d'])) {
     $d = $_GET['d'];
  }
  if ($n == 10) {
      foreach ($ss as $c) {
              $data .= ord($c) . $d;
      }
  } else if ($n == 8) {
      foreach ($ss as $c) {
              $data .= '0'.decoct(ord($c)) . $d;
      }
  } else if ($n == 16) {
      foreach ($ss as $c) {
              $data .= '0x'.dechex(ord($c)) . $d;
      }
  } else {
      foreach ($ss as $c) {
              $data .= sprintf( "%08d", decbin(ord($c))) . $d;
      }
  }
  header("Access-Control-Allow-Origin: *");
  header('Content-Type: application/json');
  die(json_encode(trim($data, $d)));

If $_GET parameter s is not specified, this API will use the $_POST variable s instead. It is highly recommended to pass the values via the POST method because passing by POST gives you more security and allows you to deal with lengthy input. However, unlike _GET API, the results will not be cached by CloudFlare edge servers (e.g. 1 hour) if you decide to invoke the API via POST method.

Moreover, the GET URL containing too many language keywords e.g. SQL, may be blocked by CloudFlare Web Firewall for security purpose. It might be treated as suspicious or malicious URL e.g. SQL injection.

curl -X POST "https://string-to-ascii.justyy.workers.dev/api/string-to-ascii/?cached" -d "s=This page Rocks!"

API Servers for Converting Strings to ASCII code

You can use the following API servers, for free, on a fair-use policy:

PHP Principle of API String-To-ASCII

We use the following PHP code:

  $s = '';
  if (isset($_GET['s'])) {
     $s = $_GET['s'];
  } else {
    if (isset($_POST['s'])) {
     $s = $_POST['s'];
    }
  }
  $data = '';
  $ss = str_split($s);
  foreach ($ss as $c) {
          $data .= ord($c) . ' ';
  }
  header("Access-Control-Allow-Origin: *");
  header('Content-Type: application/json');
  die(json_encode(trim($data)));

Share: List of Many Other Online Tools