Alternative for toChar in CIQ 1.2.0?

I have a Fenix 3 app that uses toChar to print ASCII characters that are assembled from 6-bit sequences. While building the app for the Epix I realized CIQ 1.2.0 has no toChar.
If anyone could share a way to print for example an 'A' from a byte that contains the decimal value 65, I'd greatly appreciate it!
  • This isn't pretty, but it works.

    var charNul = -65 + 'A';
    var x = 66 + charNul;
    System.println("66 to char: " + x); // "B"
    var y = 55 + charNul;
    System.println("55 to char: " + y); // "7"

    EDIT: as noted below, this doesn't work on a real Epix.

  • If anyone could share a way to print for example an 'A' from a byte that contains the decimal value 65, I'd greatly appreciate it!

    If you're just dealing with ASCII characters, you should be able to do a lookup table. You will get a Lang.String instead of a Lang.Char (because Lang.Char didn't exist in ConnectIQ 1.2.x).

    const ascii = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";

    function toChar(n) {
    if (n < 32 || 126 < n) {
    return null;
    }

    /* the function call below */
    return ascii.x(n-32,n-31);
    }


    This isn't pretty, but it works. Unforunately the forum hates parentheses, so just substitute round brackets where you see square brackets.

    That code may compile with recent compilers, but I have doubts it will work on devices with old firmware... the Lang.Char type didn't exist.
  • Note that there is no ascii.x function. You need to call ascii.substring, the forums seem to reject code blocks that include substring in them anywhere.
  • That code may compile with recent compilers, but I have doubts it will work on devices with old firmware... the Lang.Char type didn't exist.


    Sorry my mistake. I tried it in the simulator with the Epix, but I should know better than to trust the simulator. Would be nice if there was a less memory-intensive solution besides a lookup table.
  • Actually, the only device I'd worry about in the sim is the Epix. It's kind of an albatross these days (the only device still on CIQ 1.2.x), and I'm not sure the sim has been updated to check for what's supported in that version of CIQ, vs 1.4 or 2.x. Travis is correct - it slipped by in the compiler/sim, but I doubt you'd see that with other devices in the sim.

    They ONLY way I'd support anything on the Epix right now is if had one available to test on to see what else might be a bit "off".
  • Thanks for the ideas guys! The lookup table-approach is working on the actual Epix. I put single-character strings in the array. const ascii = [ "A", "B", "C", ... etc. The input "chars" are 6-bit codes anyway, so I can now skip adding decimal value 32 to them to make them ASCII values. :D
  • One thing to note about you're implementation...

    const ascii = [ "A", "B", ... ];


    There is a limit to the number of 'objects' that can be in use by your program at any time. Your implementation uses one object for every entry in the array; if it is a full ascii table with 256 elements, you will have used up 257 objects out of the total available (512?), effectively wasting half of your applications object limit on an ascii lookup table.

    Contrast that with the implementation that I provided which uses 2 objects.

    Travis
  • Agree your solution is way more efficient, but I could not get it to work on the Epix (that may well be error on my part, I'll have another look at it).
  • I could have an error in my code as well. I don't have an epix to test with.

    Travis