Search code examples
javascriptkendo-uitelerikqr-codetyped-arrays

How can I use javascript to create a string consisting of every ISO/IEC 8859-1 character possible?


I want to create a string consisting of every character possible and see if any of the popular QR readers are able to read each and every char from this QR Barcode.

My problem is that I simply don't know how to create an object as a byte so that it appears as a IEC 8859 character. I've tried the Typed Arrays, but unable to achieve printing out each character and assigning it to the Telerik / Kendo control

How do I use JS to create a string of ISO/IEC 8859-1 characters, and assign it to the control linked above?


Solution

  • I wrote this function that takes a JavaScript number and determines if it's in the ISO/IEC 8859-1 codespace. Using this which String.fromCharCode allows you to construct the string you're looking for.

    function inIsoIec8859_1(code) {
      if (typeof code !== "number" || code % 1 !== 0) {
        throw Error("code supplied is not a integer type.")
      }
    
      if (code > 255 || code < 32 || (code > 126 && code < 160)) {
        return false;
      }
      return true; 
    }
    
    var isoLatinCodespace = "";
    
    var code = 0;
    for (; code < 255; code += 1) {
      if (inIsoIec8859_1(code)) {
        var current = String.fromCharCode(code);
        isoLatinCodespace = isoLatinCodespace + current;
      }
    }
    

    http://dojo.telerik.com/IGoT/12

    Because you accepted my original answer, it is above this line, unedited. I realized something after I posted that could be important.

    If you plan on getting this value in a loop, this will serve your needs much better.

    var MyApp = { 
      isoLatinString : (function() {
        var isoLatinCodespace = "";
    
        return function () {
          if (!isoLatinCodespace) {
            function inIsoIec8859_1(code) {
              if (typeof code !== "number" || code % 1 !== 0) {
                throw Error("code supplied is not a integer type.")
              }
    
              if (code > 255 || code < 32 || (code > 126 && code < 160)) {
                return false;
              }
              return true;
            }
    
            var code = 0;
            for (; code < 255; code += 1) {
              if (inIsoIec8859_1(code)) {
                var current = String.fromCharCode(code);
                isoLatinCodespace = isoLatinCodespace + current;
              }
            }
          }
          return isoLatinCodespace; 
        }
      }())
    }
    

    Then later using MyApp.isoLatinString(). This new code only generates the string once, even if called multiple times. MyApp can be any object you're using to contain your application.