I had chatGPT port this to dataweave:
```
%dw 2.0
output application/json
fun TBCD_special_chars(input) =
if (input == "*") "1010"
else if (input == "#") "1011"
else if (input == "a") "1100"
else if (input == "b") "1101"
else if (input == "c") "1100"
else do {
// log message
"input " ++ input ++ " is not a special char, converting to bin "
(input as Number) as String {format: "0b"} // converting input to binary
}
fun TBCD_encode(input) =
var output = ""
var offset = 0
var matches = ["*", "#", "a", "b", "c"]
while (offset < sizeOf(input)) do {
if (sizeOf(input) - offset >= 2) do {
var bit = upper(input[offset to offset + 1]) // Get two digits at a time
bit = reverse(bit) // Reverse them
if (any(bit[i] in matches for i in (0 to 1))) do {
var new_bit = TBCD_special_chars(bit[0]) ++ TBCD_special_chars(bit[1])
bit = (new_bit as Number) as String {format: "0b"}
}
output = output ++ bit
offset = offset + 2
} else {
output = output ++ "f" ++ input[offset to offset + 1]
break
}
}
output
fun TBCD_decode(input) =
var output = ""
var offset = 0
while (offset < sizeOf(input)) do {
if (!contains(input[offset to offset + 1], "f")) do {
var bit = input[offset to offset + 1] // Get two digits at a time
bit = reverse(bit) // Reverse them
output = output ++ bit
offset = offset + 2
} else {
var bit = input[offset to offset + 1]
output = output ++ bit[1]
break
}
}
output
// Example usage
var encoded = TBCD_encode("12*34#")
var decoded = TBCD_decode(encoded)
{
"encoded": encoded,
"decoded": decoded
}
```