-
Notifications
You must be signed in to change notification settings - Fork 57
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 hexa-decimal integers #80
Comments
I'll second this. I stumbled across hjson while looking for a format to use for a machine-readable binary language definition. Things like opcode numbers are much better represented in hex notation. DSF/unquoted strings don't help in my case because they must be delimited by newline which would be seriously detrimental to the readability since the data is arranged in a kind of tabular style: Example snippet:
For now I'll probably go with 'FF' for 0xFF. On a related note, it would be nice to be able to disable the unquoted strings, as they obscure the error messages. In my case, they're completely useless anyway and I expect similar use-cases exist. |
Sorry, this project is EOL, see https://hjson.org |
One possible complication is that when you see hexadecimal literals, you think that it is the literal binary representation of the number. However, in JavaScript, numbers are usually (always?) floating-point. So, if you have an Hjson file with |
You are right, JavaScript's numbers are always double-precision floating points, and so a giant number literal will always be converted to it's closest floating-point representation. JavaScript already has a function const digits = {
0: 0,
1: 1,
2: 2,
3: 3,
4: 4,
5: 5,
6: 6,
7: 7,
8: 8,
9: 9,
a: 10,
b: 11,
c: 12,
d: 13,
e: 14,
f: 15,
};
function parseHex(input) {
let output = 0;
if (typeof input !== 'string') {
throw new TypeError('expected a string');
}
for (const char of input) {
if (!(char in digits)) {
throw new TypeError('"' + char + '" is not a valid hexadecimal digit');
}
output = output * 32 + digits[char];
}
} |
In some cases, hexa-decimal is handier than decimal, for example RGB values.
In my case, network switch ids, which are 64-bit unsigned integers, need to be in a kind of
a configuration file, and they might have look better, at least shorter, in hexa-decimal representation.
The text was updated successfully, but these errors were encountered: