number.toChar() alternative in 1.2

Is there any way to get a char from an ascii value in CIQ 1.2? In the Ant+ Tracker profile, it provides the asset name as a series of numbers instead of characters. I need to get them into a string. number.format() also doesn't support the %c option.

Or am I going to have to create my own ascii conversion table?
  • The Lang.Char type didn't even exist prior to 1.3, so the best you can do is convert a Lang.Number to a Lang.String of length 1. But yes, you will have to write a function to do the conversion. This might help.

    // requires you to add a build exclusion for one of connectiq_1_2 or not_connectiq_1_2
    (:connectiq_1_2) const _ascii = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~";

    (:connectiq_1_2) function toChar(number)
    {
    if (number < 32 || 126 < number) {
    return "?";
    }

    return _ascii[number - 32];
    }

    (:not_connectiq_1_2) function toChar(number) {
    return number.toChar();
    }
  • Thanks. This code looks a bit cleaner than I would have done and keeps me from figuring out what the valid charsets are.

    As for build exclusions, I hadn't realized these were available. The (:debug) will be really helpful. But maybe I'm just being a bit slow here. Is there anyway to automatically add the connectiq_1_2 definition through resources? So add the connectiq_1_2 for epix and not_connectiq_1_2 for everything else?

    Thanks also for the utf-8 formatting hint to get the degree symbol. I think I'd originally tried inserting the symbol but not changing the file format. In this case I wasn't even using the DegreeSymbol constant in the app. Just left over from cut and paste of common constants/functions from other apps.

    Trying to support 1.2 has been educational. I'd tried compiling a couple things for my Epix but always ran into the Symbol Not Found error at compile time and it wasn't that important as I use my 630 for most things.
  • Here is the specifics on how to implement what Travis was talking about:

    in your resources\resources.xml file,
    <resources>
    <build>
    <exclude annotation="connectiq_1_2"/>
    </build>
    </resources>

    in resources\resources-epix\resources.xml file,
    <resources>
    <build>
    <exclude annotation="not_connectiq_1_2"/>
    </build>
    </resources>

    Then, I made a couple fixes to the code. "" -> "\", string[] -> string.substring(..)

    // requires you to add a build exclusion for one of connectiq_1_2 or not_connectiq_1_2
    (:connectiq_1_2) const _ascii = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";

    (:connectiq_1_2) function toChar(number)
    {
    if (number < 32 || 126 < number) {
    return "?";
    }

    return _ascii.substring(number - 32,number-31);
    }

    (:not_connectiq_1_2) function toChar(number) {
    return number.toChar();
    }
  • I'm glad that worked out for you. Sorry the code wasn't tested... It was getting late and I figured you'd be able to manage.

    Travis
  • Couldn't you use

    if(Number has :toChar) {

    }else {

    }

    and skip the whole exclude thing? Granted you'd use a small bit of extra space in all builds, but might simplify things over all.

    Or use Sys.getDeviceSettings().monkeyVersion and see if you're on 1.2.x?

    You would waste the space of the string in 1.3 or greater though..
  • You certainly could. You'd be trading a compile-time check for a run-time check (using has or fetching the device settings and comparing the version) and throwing away about 100 bytes of memory. Plus, it is an excuse to use the build exclusions system... :)

    Travis
  • Yes, using exclusions has come in really handy for me at times!
  • Couldn't you use

    if(Number has :toChar) {

    }else {

    }

    and skip the whole exclude thing? Granted you'd use a small bit of extra space in all builds, but might simplify things over all.

    Or use Sys.getDeviceSettings().monkeyVersion and see if you're on 1.2.x?

    You would waste the space of the string in 1.3 or greater though..


    You could, except the Epix (hardware, not in the sim) crashes from (Number has :toChar)! This was one of the first things I tried.

    Until I found out about the exclusion option, I was checking the Device Settings Monkey version. Exclusion is definitely a better solution.
  • adapting for the the Epix is definitely a PITA! But I'm learning all sorts of things through the process.

    However, anybody know why no debug info gets written to the log file? Doing the exact same thing as for my 630 and VAHR.
  • Interesting! I've never seen a "has" that caused a crash on any device. I wonder why. The Epix is a bit odd I guess.