cbor / cbor.github.io

cbor.io web site
74 stars 33 forks source link

Hex in CBOR #89

Open CodeOn-ArK opened 2 years ago

CodeOn-ArK commented 2 years ago

Hi there! How to encode a JSON format as following:

{
"val1" : p,
"val2" : h'1234abcd'
}

Where the field val2 is associated with a hex data. This doesn't seems to be a correct JSON format, however the web interface is able to convert it to the required CBOR format. I am unable to use any of the python/JS libs to convert this to CBOR, however the web is able to do so. What is the correct representation of this format where I want to send a hex value Regards

cabo commented 2 years ago

The cbor.me website uses CBOR Diagnostic Notation (Section 8 of RFC 8949), which is an extended form of JSON to represent CBOR data items in a readable form. Another popular text form of JSON data is YAML.

It is not necessary to generate such a form to generate CBOR. In Python, you typically use Python notation to build a data item in memory and then convert it using one of the Python libraries, e.g., cbor2.

CodeOn-ArK commented 2 years ago

ok, but how do I encode something in hex? "val2" : h'1234abcd' this kind of thing, in any language

cabo commented 2 years ago

That is a question about your programming language. In Python, there are byte strings in the form of "bytes objects", https://docs.python.org/3/library/stdtypes.html#typebytes. bytes.fromhex('1234abcd') should give you what you need. (I'm not an active Python programmer, I hope I got this right.)

CodeOn-ArK commented 2 years ago

Any possible implementation for node js. Also, is the source for the website is available somewhere?

cabo commented 2 years ago

I'm not fluid in nodejs. The https://cbor.me website is based on the cbor-diag gem, which you can find at https://github.com/cabo/cbor-diag.

AtigPurohit commented 2 years ago

can we use cbor diagnostic notations with that library to encode hex If yes how and any supporting materials for that

cabo commented 2 years ago

As the README says:

No documentation; use the source, for now (the above command line utilities should show the basic usage).