Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support hexadecimal, octal and binary numbers in $number() #305

Closed
sagIoTPower opened this issue Apr 20, 2024 · 2 comments
Closed

Support hexadecimal, octal and binary numbers in $number() #305

sagIoTPower opened this issue Apr 20, 2024 · 2 comments

Comments

@sagIoTPower
Copy link

Hi,

I would like to use the $number() function to decode some hexadecimal numbers.

The current implementation does not support this, e.g.
$number('0x575')
throws an exception:
com.api.jsonata4java.expressions.EvaluateRuntimeException: Unable to cast value to a number: "0x575"

The JSONata $number() function supports this and returns 1397.

Would it be possible to add some code like the following to the current implementation in NumberUtils.java:

public static int convertToInteger(String number) {
        if (number.startsWith("0x")) {
            // Hexadecimal number
            return Integer.parseInt(number.substring(2), 16);
        } else if (number.startsWith("0o")) {
            // Octal number
            return Integer.parseInt(number.substring(2), 8);
        } else if (number.startsWith("0b")) {
            // Binary number
            return Integer.parseInt(number.substring(2), 2);
        } else {
            // Decimal number or invalid format
            return Integer.parseInt(number);
        }
    }
wnm3 added a commit that referenced this issue Apr 21, 2024
@wnm3
Copy link
Member

wnm3 commented Apr 21, 2024

Thanks for the idea. I've pushed a new version to Maven Central 2.4.9 and it should be available in a few hours (it is 4/21 1:21pm ET now). If you agree the issue is resolved please close it. Thanks -- it gave me an opportunity to update the pom.xml for newer libraries.

@sagIoTPower
Copy link
Author

I tested the new version 2.4.9 and it works. So you can close the issue. Thank you.

@wnm3 wnm3 closed this as completed Apr 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants