William Matsuoka
  • Introduction
  • Topics
  • Stata
  • Teaching
    • ECON 641L
    • ECON 640L >
      • Econ-Data
  • Blog
  • About

W=M/Stata

Welcome

Encode/Decode/Nintendo 64

1/1/2016

5 Comments

 
Why Care Block:
Base64 encoding is extremely common when using API services, such as the Fitbit API, but that’s about all I know.  It’s necessary to get the job done and isn’t readily available in Stata.
Happy New Year!  We're starting 2016 with a bang, including this three part series on acquiring Fitbit data through Fitbit’s API services.  2015 was a great year and marked the start of this website, and I can guarantee that there will be much more content to come.  See the new "Why Care Block"?  It’s inspired by a Holiday conversation with family and gives a brief synopsis of why we’re even doing this in the first place.  With that, let's talk about the topic of the day: Base64 encoding.

Base64 encoding exists for some privacy purpose.  That's about all I want to say about Base64 encoding.  Let's just look at an example, they’re a lot cooler.

Say for instance we have the phrase: “This is kinda boring…”

By converting every character to ASCII bytes (and by potentially adding padding), converting each byte to base 2, taking the 8-bit representation of the binary character and convert it to a six bits, converting that back to base 10, and using those numbers (they fall between 0 and 63) to look up the corresponding character of the alphabet plus two other characters, we finally arrive at the following encoded string:

"VGhpcyBpcyBraW5kYSBib3JpbmcuLi4="

While they mean the same thing, I think the first phrase was a little more accurate.  We can encode this second phrase a second time and it gives us:

"VkdocGN5QnBjeUJyYVc1a1lTQmliM0pwYm1jdUxpND0="

Now you can easily send encoded messages to your friend (not plural – I can't imagine that many people would tolerate this type of behavior).  I’ve put together a Stata package containing these routines; however, no help file is available upon the first release.  Here is a brief overview of the new commands, encode64 and decode64 at work.
Picture
We first called encode64 on the string "client_id:123456789" to encode this value in Base64.  The result shows under the command, but is also callable through the macro r(base64).  We create a local macro that contains this encoded value ("Y2xpZW50X2lkOjEyMzQ1Njc4OQ==") and decode this message to get back to our original string.  It's a lot less fun than my old Nintendo 64, but trust me, the results will be extremely rewarding!  Feel free to download the ado file below.
encode64.ado
File Size: 2 kb
File Type: ado
Download File

Part 2: Getting Heart Rate Data with Stata, Fitbit's API, and cURL
5 Comments
Quinton Baker link
2/22/2019 04:14:49 pm

This was extremely helpful to get our team to decode base64 images to png. However, Stata didn't really like how long our strings were, and it is more practical in our use case to just use the shell.
!certutil -decode [infile] [outfile]
Works great for us, the only extra step is writing the strings to file first, but since we need pngs we are thinking about what we want to call them anyway. Thanks for this tool!

Reply
RW
11/17/2021 12:32:49 pm

Was an ado file for decode64 supposed to be somewhere on this page as well? I downloaded encode64 and it works great, but can't seem to find the decode version. Thanks!

Reply
Will
11/17/2021 01:08:37 pm

Hi RW

Both encode and decode are in the attached ado-file - you can either split out the functions into their respective ado-files or you can simply make a copy of encode64.ado and name it decode64.ado.

If you have any further issues, feel free to e-mail me and I'll send you a copy of the split versions.

Reply
RW
11/17/2021 01:16:59 pm

Oh nice, I didn't realize it was in there--I split it out into its own ado and it's working great. Thanks for putting this together!

Katrina Robbins link
7/8/2022 03:30:59 pm

Thaank you for sharing this

Reply



Leave a Reply.

    Author

    Will Matsuoka is the creator of W=M/Stata - he likes creativity and simplicity, taking pictures of food, competition, and anything that can be analyzed.

    For more information about this site, check out the teaser above!

    Archives

    July 2016
    June 2016
    March 2016
    February 2016
    January 2016
    December 2015
    November 2015
    October 2015
    September 2015

    Categories

    All
    3ds Max
    Adobe
    API
    Base16
    Base2
    Base64
    Binary
    Bitmap
    Color
    Crawldir
    Email
    Encryption
    Excel
    Exif
    File
    Fileread
    Filewrite
    Fitbit
    Formulas
    Gcmap
    GIMP
    GIS
    Google
    History
    JavaScript
    Location
    Maps
    Mata
    Music
    NFL
    Numtobase26
    Parsing
    Pictures
    Plugins
    Privacy
    Putexcel
    Summary
    Taylor Swift
    Twitter
    Vbscript
    Work
    Xlsx
    XML

    RSS Feed

Proudly powered by Weebly
  • Introduction
  • Topics
  • Stata
  • Teaching
    • ECON 641L
    • ECON 640L >
      • Econ-Data
  • Blog
  • About