Comments (5)
Yes. From the API:
await cbor.diagnose(cbor.encode(Buffer.from('foo')))
// "h'666f6f'\n"
From the CLI:
> cbor2diag -x 43666f6f
h'666f6f'
from node-cbor.
There are lots of examples in the tests, e.g. https://github.com/hildjj/node-cbor/blob/main/packages/cbor/test/diagnose.ava.js
Here is a more full-featured example for your data above:
const cbor = require('cbor');
const input = {
val1: "p",
val2: Buffer.from("1234abcd", "hex") // Put any binary data in a Buffer or Uint8Array
}
const encoded = cbor.encode(input); // `encoded` is now a Buffer containing the binary CBOR-encoded version of `input`
console.log(encoded.toString('hex')); // a26476616c3161706476616c32441234abcd
cbor.diagnose(encoded).then(result => {
// `diagnose` returns a Promise. If you're in an async function, you can
// use `await` instead of the `then` syntax.
// `result` is now a string that
// contains the diag formatted version of `input`
console.log(result); // {"val1": "p", "val2": h'1234abcd'}
});
Documentation for the diagnose
function is here.
from node-cbor.
Hi
It would be very helpful if you can show the way to encode this using APIs:
{
"val1" : "p",
"val2" : h'1234abcd'
}
Also can you guide to some examples
Regards
from node-cbor.
Anyway how to use this line of code
await cbor.diagnose(cbor.encode(Buffer.from('foo')))
suppose I want to send some binary data instead of foo
how to achieve that.
from node-cbor.
Thanks a lot for saving the day!!
from node-cbor.
Related Issues (20)
- cbor-web dist is broken HOT 4
- Update RFC link in readme HOT 1
- Nativescript angular HOT 9
- Question: How does one use stream based approach? HOT 8
- The number -2^53 should be encoded as a float, not an integer HOT 3
- MAXINT64: BigInt('0xffffffffffffffff') crashes on React-Native HOT 12
- Error: Unexpected data: 0x44 HOT 32
- react-native-cli HOT 5
- Webpack minimize causes cbor.encode to generate a different value HOT 5
- Fix Buffer converter matcher being mangled in minimised builds HOT 1
- ReferenceError: Can't find variable: TextDecoder HOT 20
- Behavior in Nativescript HOT 19
- Encode error when source is too large HOT 4
- switch to using Uint8Array instead of using buffer HOT 3
- Type array tags CBOR HOT 2
- Support `ArrayBufferView` as `BufferLike` HOT 1
- Move to ESM HOT 4
- cbor tagged object not same after decoding and again encoding HOT 8
- cbor-web: Unexpected token on rollup build, is `for(;;)` supposed to be here? HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from node-cbor.