Number System Conversion in MuleSoft(Mule 4) using Dataweave
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.
b, represents the base.
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:
import fromHex from dw::core::Numbers
output application/json
---
fromHex("7B")
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:
import fromBinary from dw::core::Numbers
output application/json
---
fromBinary("110110")
54
3. fromRadixNumber()
%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.
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:
import toHex from dw::core::Numbers
output application/json
---
toHex(54)
"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:
import toBinary from dw::core::Numbers
output application/json
---
toBinary(54)
"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:
import toRadixNumber from dw::core::Numbers
output application/json
---
toRadixNumber(54,8)
"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
import * from dw::core::Numbers
output application/json
---
toRadixNumber(fromRadixNumber(312,4),3)
"2000"
References:
https://docs.mulesoft.com/dataweave/2.4/dw-numbers
Please share your valuable feedback ๐๐๐
Good to remember school days is Office work
ReplyDelete