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?

Bejuco
  • 133
  • 12

0 Answers0