Number System Conversion in MuleSoft(Mule 4) using Dataweave


 The number 11, we call it eleven in the decimal system. Still, it can be a representation of three in a binary system or it can be seventeen in a hexadecimal system. The actual value of a number depends on the base it is calculated. It is interesting changing the base changes the value of the representation. Let us assume in some integration flow we are getting values in binary number system but target system accepts values in a decimal system or hexadecimal system or vice versa.

Examples depicting values in various number systems:

# DECIMAL BINARY HEXADECIMAL OCTAL
BASE 10 BASE 2 BASE 16 BASE 8
I 5 101 5 5
II 14 1110 E 16
III 54 110110 36 66
IV 123 1111011 7B 173
V 240 11110000 F0 360

In our engineering days, we have studied a lot about number systems. Below is the mathematical representation of conversion logic.

Let us assume:
d, represents the nth digit.
b, represents the base.
n, represents the number of digits
Value(V) of number with base b in the decimal system will be: 
V10= (ฮฃd*b(n-1))10

Let us take example number 7B in hexadecimal(base 16) and use the above formula to convert to decimal.
V10= (7*16(2-1) + 11*(1-1))10
V10= (112 + 11)10 = (123)10

The above conversion was through pen and paper but how to do this in Mule?

Dataweave has built-in functions under the dw::core::Numbers module, which can help us to perform these conversions without writing complex logic. To convert to decimal values we have below 3 functions. Let's see usage one by one.

1. fromHex()

This function helps to convert hexadecimal value to decimal. This function takes a hexadecimal string as input and gives decimal value as output. Example:

%dw 2.0
import fromHex from dw::core::Numbers
output application/json
---
fromHex("7B")

Output:
123

2. fromBinary()

This function helps to convert the binary string value to decimal value. This function takes a binary string as input and gives decimal value as output. Example:

%dw 2.0
import fromBinary from dw::core::Numbers
output application/json
---
fromBinary("110110")

Output
54

3. fromRadixNumber()

This function helps to convert the numeric value in any base to decimal value. This function takes a number string and base as input and gives decimal value as output. Example:

%dw 2.0
import fromRadixNumber from dw::core::Numbers
output application/json
---
fromRadixNumber("360",8)

Here, 360 is the number in octal (base 8) and 8 represents the base.

Output
240

The above three ways were to convert numeric values from any base to decimal values. Dataweave also provides functions to do the reverse which is converting a decimal number to any base.

4. toHex()

This function helps to convert a decimal value to a hexadecimal value. This function takes a decimal number as input and gives a hexadecimal string value as output. Example:

%dw 2.0
import toHex from dw::core::Numbers
output application/json
---
toHex(54)

Output
"36"

5. toBinary()

This function helps to convert a decimal value to a binary string value. This function takes a decimal number as input and gives a binary string value as output. Example:

%dw 2.0
import toBinary from dw::core::Numbers
output application/json
---
toBinary(54)

Output
"110110"

6. toRadixNumber()

This function helps to convert a decimal value to a numeric string value as per the specified base. This function takes a decimal number and base as input and gives a string output of decimal value in the specified base. Example:

%dw 2.0
import toRadixNumber from dw::core::Numbers
output application/json
---
toRadixNumber(54,8)

Output
"66"

The above output is in base 8 or in octal.

Direct Conversion

What if we want to convert numeric value in base 4 directly to numeric value with base 3. In such a case, we can use a combination of fromRadixNumber and toRadixNumber.

(54)10 = (312)4 = (2000)3

Dataweave
%dw 2.0
import * from dw::core::Numbers
output application/json
---
toRadixNumber(fromRadixNumber(312,4),3)

Output
"2000"

References:

https://docs.mulesoft.com/dataweave/2.4/dw-numbers


Please share your valuable feedback ๐Ÿ˜Š๐Ÿ˜Š๐Ÿ˜Š



Comments

Post a Comment

Popular posts from this blog

DateTime formatting using xp20:format-dateTime ()

Create Delimited String from XML Nodes and Vice Versa in SOA 12c

Import and Export MDS artifacts in SOA 12c