Convert decimal number to ASCII

I'm thinking about the possibility using the println logger as a way to store Generic ANT data received by the watch for debugging purposes.

However, storing it as a text file is not an efficient use of the 5K storage limit. I'm thinking that I could encode the data as if it were a .FIT record (or a similar binary concept) then write some type of simple decoder to recover the data later when I download the log file.

My first thought on how to do this is to write each byte of data as its ASCII character so I would need someFunction like below to do this.

var num = 65; //num is between 0 and 255
var character = someFunction(num); //someFunction returns the string "A"
Sys.println(character); // prints an A into the log file


Does anyone know of a simple way to do this in MonkeyC?
  • I would expect that there is a possibility to cast it in a way, as this language is called 'monkey C' but there seems to be no possibility for that...

    A dictionary could be another possibility here, couldn't it?
  • A dictionary could be another possibility here, couldn't it?


    You can use a dictionary, but that will be a very expensive solution.

    LDSLARON's solution above is the cheapest way.... even if it only work for ASCII characters and not for the full Unicode character set.
  • Former Member
    Former Member over 10 years ago
    You won't be able to use the complete ASCII set, because not all characters are printable. You don't need to map byte value 1 to ascii value 1 (or 32 to 32 like in LDSLARON's solution), so if you can find 128 different printable characters you could write 1 byte as 2 characters.
    But..... this is not a very big win. A 4 byte integer could take up to 11 characters to write and now costs 8 characters encoded. Even in the most optimal situation you won't win more than 50% extra storage, but you'll probably only get up to 20-30%.
    I think if you want your encoding to really make a difference, you should look at ways to compress it before you encode. The type of compression that best suits your needs depends on the data you're expecting, but I'd look at things like run-length encoding or some way of keeping your numbers in a range between 0 and 127 (using a baseline value?).
  • Agreed; I was focusing on the "Convert decimal number to ASCII" problem in Monkey C, but that may not ultimately solve the bigger picture problem the OP is interested in solving.
  • It would be great to have a String. fromCharCode () function (similar to the Javascript one) to address this.
    Also having Number. format () accept the 'c' specifier to display a Character would be great, and would I imagine share code.
    This would go towards addressing the lack of an actual Character Class provided by the language.
    How is a feature request to add this raised?
  • It would be great to have a String. fromCharCode () function (similar to the Javascript one) to address this.
    Also having Number. format () accept the 'c' specifier to display a Character would be great, and would I imagine share code.
    This would go towards addressing the lack of an actual Character Class provided by the language.
    How is a feature request to add this raised?


    I've noted your requests and they'll be put into our queue. The best way to make feature requests is the way you just did. We take note of all the requests and do our best to accommodate them. If after a few work days you don't see a response from a ConnectIQ account, feel free to bump your request.

    That being said, we do have to juggle all the requests against our planned features and bug fixes, so it may take us longer than desired to implement these requests. If it doesn't make it into the next release it'll still be in our queue.
  • Hm, would need to get the ascii (unicode or whatever) code from a character but couldn't do that so far...

    function text(dc,text)
        {
            var i,c,l,t,x;
            
            l=text.length();
           
            for (i=0;i<l;i++)
            {
                c=text.substring(i,i+1).toNumber();

                      :
       

    In other words, I'd need a inverse function to toChar() - so 64.toChar().andBack() should return 64 again.

    Any idea how to handle this correctly?

  • var sa = text.toCharArray();

    for(i...)

      var ascii = sa[i].toNumber();

  • Hm, would need to get the ascii (unicode or whatever) code from a character but couldn't do that so far...

    In other words, I'd need a inverse function to toChar() - so 64.toChar().andBack() should return 64 again.

    Any idea how to handle this correctly?

    As this thread is over 7 years old, the information is a lil out of date and there actually is a way to do this now, with the Char class (which was introduced in CIQ 1.3.0.) The only device which didn't get CIQ 1.3 was the original Epix, IIRC.

    Like _psx_ said, you can convert a string to an array of chars using String.toCharArray().

    Then you can use the following functions to do the kinds of things you're talking about:

    Char.toNumber() ("The UTF-32 representation of the Char interpreted as a Number ")

    Number.toChar()

    Char.toString()