### 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.

**d**, represents the nth digit.

**b**, represents the base.

**n**, represents the number of digits

**V**) of number with base b in the decimal system will be:

**V**

_{10}= (ฮฃd*b^{(n-1)})_{10}Let us take example number **7B** in hexadecimal(**base 16**) and use the above formula to convert to decimal.

V_{10}= (7*16(2-1) + 11*(1-1))_{10}V_{10}= (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")

**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:

import fromBinary from dw::core::Numbers

output application/json

---

fromBinary("110110")

**Output**

54

## 3. fromRadixNumber()

**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:

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:

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:

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**

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 ๐๐๐

Good to remember school days is Office work

ReplyDelete