title: Base64 Decode Pure Javascript
body: <p>I am trying to base64 decode a value (16-bit signed big endian) from a Kafka &lt;&gt; Debezium Payload using a User Defined Function in Snowflake. Unfortunately, I am only able to use Javascript and Snowflake uses Javascript Standard Library. I found a working example for NodeJS which uses the Buffer module but that is not usable in Javascript.</p>
  <pre><code> var kafkaDecodeNumber = function(base64EncodedNumber) {
       var byteArray = Buffer.from(base64EncodedNumber, 'base64');
       var value = 0;
       for (var i = 0; i &lt; byteArray.length; i++) {
           value = (value * 256) + byteArray[i];
       }
       return value;
   };
  
  var float = parseFloat(kafkaDecodeNumber( price )).toFixed(38) / 100
  </code></pre>
  <p>Here are some working code examples I am using elsewhere</p>
  <p>Python</p>
  <pre><code>    ctx = decimal.Context()
      ctx.prec = 38
      result = ctx.create_decimal(
          int.from_bytes(base64.b64decode(byte_value), byteorder='big')
          ) / 100
      return result
  </code></pre>
  <p>Ruby</p>
  <p><code>event.get('price').unpack1('m*').unpack1('B*').to_i(2) / 100.0)</code></p>
  <p>Any assistance here would be great!</p>