Search code examples
muledataweavemule4

Base64 Conversion to Number in DataWeave Returns incorrect value


I have the following in Python that decodes Yag= and returns the bytes b'a\xa8' and converts to an integer of 25000

import base64

num = base64.b64decode("Yag=")
converted = int.from_bytes(num, byteorder='big')

When I use the following DataWeave script it returns a\ufffd and 97

%dw 2.0
output application/json
import * from dw::core::Binaries
var num = fromBase64("Yag=")
var converted = num as Number
---
{
    num: num,
    converted: converted
}

The Python block returns the expected result. Am I missing something about DataWeave and how it decodes Base64?


Solution

  • DataWeave returns a correct result. The error comes from the assumption that the binary representation of a number is the same between DataWeave and Python. In general you should not make that assumption between any two different languages.

    To avoid that issue you can use the Binaries module function toHex() to get an hexadecimal representation of the binary, then use the function fromHex() from the Numbers module to convert the hexadecimal string to a Number.

    %dw 2.0
    output application/json
    import * from dw::core::Binaries
    var num = fromBase64("Yag=")
    var converted = num as Number
    ---
    {
        num: num,
        converted: converted,
        toHex: toHex(num),
        toHexToNumber: dw::core::Numbers::fromHex(toHex(num))
    }
    

    Output:

    {
      "num": "a\ufffd",
      "converted": 97,
      "toHex": "61A8",
      "toHexToNumber": 25000
    }