Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Base64Encode


  • Please log in to reply
8 replies to this topic

#1 Bendage

Bendage

    Advanced Member

  • Members
  • PipPipPip
  • 153 posts
  • LocationIrvine, CA

Posted 13 September 2012 - 03:57 AM

Hi everyone, Ive been working on a big Netduino project for over a year now. This project I believe will be highly beneficial for the Netduino community. I am on the final stages of demo'ing this code and releasing the source to the community. There is one final piece missing with my project... byte streaming to and from the board via serial. The problem is that I have deducted with the custom serial protocal I wrote, I must use Base64 Encoding. There is no other way around it. The good news is that there is a class in the Toolbox.NETMF.Tools framework located at http://netmftoolbox....box.NETMF.Tools called Base64Encode. But for the life of me I can't figure out why they did not write a decode routine. I desperately need to decode it on the Netduino. Does anyone know where I can get a Base64Decode routine? Your reply is greatly appreciated.

#2 Bendage

Bendage

    Advanced Member

  • Members
  • PipPipPip
  • 153 posts
  • LocationIrvine, CA

Posted 13 September 2012 - 05:43 AM

I guess I needed too fast... I just rewrote the Java one.

Someone should add this to the netduino helper classes. I believe this site maintains those classes.

        public static String Base64Decode(String s) 
        {
 
            // remove/ignore any characters not in the base64 characters list
            // or the pad character -- particularly newlines
            s = s.Replace("[^" + base64chars + "=]", "");
 
            // replace any incoming padding with a zero pad (the 'A' character is
            // zero)
            String p = (s[s.Length - 1] == '=' ? (s[s.Length - 2] == '=' ? "AA" : "A") : "");
            String r = "";
            s = s.Substring(0, s.Length - p.Length) + p;
 
            // increment over the length of this encrypted string, four characters
            // at a time
            for (int c = 0; c < s.Length; c += 4) {
 
                // each of these four characters represents a 6-bit index in the
                // base64 characters list which, when concatenated, will give the
                // 24-bit number for the original 3 characters
                int n = (base64chars.IndexOf(s[c]) << 18)
                        + (base64chars.IndexOf(s[c + 1]) << 12)
                        + (base64chars.IndexOf(s[c + 2]) << 6)
                        + base64chars.IndexOf(s[c + 3]);
 
                // split the 24-bit number into the original three 8-bit (ASCII)
                // characters
                r += "" + (char) ((n >> 16) & 0xFF) + (char) ((n >> 8) & 0xFF) + (char) (n & 0xFF);
            }
 
            // remove any zero pad that was added to make this a multiple of 24 bits
            return r.Substring(0, r.Length - p.Length);
        }



#3 Bendage

Bendage

    Advanced Member

  • Members
  • PipPipPip
  • 153 posts
  • LocationIrvine, CA

Posted 13 September 2012 - 05:54 AM

I guess I needed too fast... I just rewrote the Java one.

Someone should add this to the netduino helper classes. I believe this site maintains those classes.

        public static String Base64Decode(String s) 
        {
 
            // remove/ignore any characters not in the base64 characters list
            // or the pad character -- particularly newlines
            s = s.Replace("[^" + base64chars + "=]", "");
 
            // replace any incoming padding with a zero pad (the 'A' character is
            // zero)
            String p = (s[s.Length - 1] == '=' ? (s[s.Length - 2] == '=' ? "AA" : "A") : "");
            String r = "";
            s = s.Substring(0, s.Length - p.Length) + p;
 
            // increment over the length of this encrypted string, four characters
            // at a time
            for (int c = 0; c < s.Length; c += 4) {
 
                // each of these four characters represents a 6-bit index in the
                // base64 characters list which, when concatenated, will give the
                // 24-bit number for the original 3 characters
                **** int n = (base64chars.IndexOf(s[c]) << 18)
                        + (base64chars.IndexOf(s[c + 1]) << 12)
                        + (base64chars.IndexOf(s[c + 2]) << 6)
                        + base64chars.IndexOf(s[c + 3]); *****
 
                // split the 24-bit number into the original three 8-bit (ASCII)
                // characters
                r += "" + (char) ((n >> 16) & 0xFF) + (char) ((n >> 8) & 0xFF) + (char) (n & 0xFF);
            }
 
            // remove any zero pad that was added to make this a multiple of 24 bits
            return r.Substring(0, r.Length - p.Length);
        }



Further testing shows the the Decode can only take an unencoded text of 57 characters before thrown an index out of range exception on the "****" code above. Thanks java! It's no good.

#4 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 13 September 2012 - 06:30 AM

But for the life of me I can't figure out why they did not write a decode routine. I desperately need to decode it on the Netduino. Does anyone know where I can get a Base64Decode routine?

I didn't include one in the toolbox since I didn't need it yet. Same for url encoding, I don't have url decoding included in the toolbox as well. Every added feature will use code storage, so I try to stay keen on that.
So... "But for the life of me" hope that one's saved ;)

I could add it to the wishlist for the toolbox, but to be realistic, it wouldn't be implemented within a month, unless I got a high need for it.
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#5 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 13 September 2012 - 07:32 AM

There are plenty of good codecs, but I'd like to remember that those have to run in a very small RAM. Any intensive string-game would result in a heap overload, which leads to a very bad performing. It's also worthwhile to remember that a too intensive heap working could lead to a memory overflow. Unfortunately (I wouldn't follow certain practices) should be follow a pre-allocated array of bytes/chars (depends on decode/encode), and a loop filling it up. That won't touch the heap at all, and the performance remains high. Cheers
Biggest fault of Netduino? It runs by electricity.

#6 Bendage

Bendage

    Advanced Member

  • Members
  • PipPipPip
  • 153 posts
  • LocationIrvine, CA

Posted 13 September 2012 - 11:08 AM

I didn't include one in the toolbox since I didn't need it yet. Same for url encoding, I don't have url decoding included in the toolbox as well. Every added feature will use code storage, so I try to stay keen on that.
So... "But for the life of me" hope that one's saved ;)

I could add it to the wishlist for the toolbox, but to be realistic, it wouldn't be implemented within a month, unless I got a high need for it.



Ha! Funny guy Stefan

Actually.... duh! After further searching, Base64 is in the Micro Framework under the Convert class. So I guess my next question would be... why is it in the toolbox?

Mario,

I have successfully sent a 40k photo @ 19200 sending 120 bytes at a time. No memory loss when streaming to SD file. It does require a thread.sleep of 200 milliseconds between looping sends though or else the serial datarecieved gets a chunk of the message cut off. Still much testing to do but I am pretty stoked.

#7 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 13 September 2012 - 11:21 AM

Ha! Funny guy Stefan

Thanks, I suppose.

Actually.... duh! After further searching, Base64 is in the Micro Framework under the Convert class. So I guess my next question would be... why is it in the toolbox?

Could you point me to your findings?
I found http://msdn.microsof...ibrary/ee432112 before, but that class isn't available on Netduino as far as I experienced. So I wrote my own implementation. Glad you find it funny :)
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#8 Mario Vernari

Mario Vernari

    Advanced Member

  • Members
  • PipPipPip
  • 1768 posts
  • LocationVenezia, Italia

Posted 13 September 2012 - 11:39 AM

I have successfully sent a 40k photo @ 19200 sending 120 bytes at a time. No memory loss when streaming to SD file. It does require a thread.sleep of 200 milliseconds between looping sends though or else the serial datarecieved gets a chunk of the message cut off. Still much testing to do but I am pretty stoked.


Happy that you're satisfied.
BTW, in your case the time for the codec appears secondary, because the communication takes actually most of the time.


IMHO, I would consider seriously to put a Base64 codec at native level, because it's very useful. The problem is there's no more free rooms in the Netduino!
Cheers
Biggest fault of Netduino? It runs by electricity.

#9 Bendage

Bendage

    Advanced Member

  • Members
  • PipPipPip
  • 153 posts
  • LocationIrvine, CA

Posted 13 September 2012 - 04:11 PM

Stefan, The "duh" was on me... not you. It wasn't easy to find although it should have been :) Mario... it is. http://msdn.microsof...y/hh401577.aspx http://msdn.microsof...y/hh435051.aspx




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.