Base64Encode
#1
Posted 13 September 2012 - 03:57 AM
#2
Posted 13 September 2012 - 05:43 AM
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
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
Posted 13 September 2012 - 06:30 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.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?
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.
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs
#5
Posted 13 September 2012 - 07:32 AM
#6
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
Posted 13 September 2012 - 11:21 AM
Thanks, I suppose.Ha! Funny guy Stefan
Could you point me to your findings?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?
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
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs
#8
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
#9
Posted 13 September 2012 - 04:11 PM
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users