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 hexa-decimal integers #80

Open
wing9405 opened this issue Feb 20, 2018 · 4 comments
Open

Support hexa-decimal integers #80

wing9405 opened this issue Feb 20, 2018 · 4 comments

Comments

@wing9405
Copy link

wing9405 commented Feb 20, 2018

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.

@Orochimarufan
Copy link

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:

opcodes:
[
        {code: 0x02, name: "Jc",                            tags: ['START_BLOCK'],  handler: 'IF'},
        {code: 0x03, name: "Jump",          args: 'O',      tags: ['JUMP']},
        {code: 0x04, name: "Switch",                        tags: ['END_BLOCK'],    handler: 'SWITCH'},
        {code: 0x05, name: "Call",          args: 'CH'},
]

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.
[For more technical things that are hand-crafted yet consumed by machines (directly or through conversion) like this, some kind of unquoted tags/identifiers (i.e. single unquoted words) would be more useful IMO since tag-like things tend to be very short which makes the quotes significant typing overhead.]

@laktak
Copy link
Member

laktak commented Sep 14, 2018

Sorry, this project is EOL, see https://hjson.org

@sffc
Copy link

sffc commented Sep 16, 2018

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 0xABABABABABABABAB, does that mean the floating-point number with those binary bits, or does it mean the closest floating-point representation to the decimal version of that number?

@dqsully
Copy link
Member

dqsully commented Sep 16, 2018

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 parseInt that can parse numbers of any radix. But even if you rolled your own solution it would still be pretty simple:

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];
  }
}

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

5 participants