0

I'm using the answer from this question to create random characters:

String.fromCharCode(0x30A0 + Math.random() * (0x30FF - 0x30A0 + 1));

I created a function with the same principle so I can change the first and the last character of the range:

function uni_char(first, last) {
  var char = String.fromCharCode(first + Math.random() * (last - first + 1))
  return char;
}

For example, if I run the function with 0x0000 and 0x007F as parameters, it works as expected. What I'm trying to do is to read a JSON file with multiple character ranges to use them with the function. This is the JSON:

[
  {
    "first": "0x0000",
    "last": "0x007F"
  },
  {
    "first": "0x0080",
    "last": "0x00FF"
  }
]

I populate an array with the JSON file data using the answer from this question:

var blocks = [];

$.ajax({
  async: false,
  url: '/scripts/data/blocks.json',
  data: "",
  accepts:'application/json',
  dataType: 'json',
  success: function (data) {
    for (var i = 0; i < data.length; i++) {
      blocks.push(data[i]);
    }
  }
});

But when I use the values from the array with the function:

uni_char(blocks[0].first, blocks[0].last);

It just creates a � symbol. What am I missing? Maybe an encoding problem? Or a logic problem?

2
  • 1
    Your hex "numbers" are strings. You should parse them into integers first, eg parseInt(first, 16) Commented Mar 28, 2021 at 23:47
  • Thank you. That's it. Commented Mar 29, 2021 at 14:31

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.