1

This is my data:

"buffer": {
        "type": "Buffer",
        "data": [
            8,
            4,
            18,
            32,
            203,
            243,
            203,
            190,
            254,
            80,
            185,
            227,
            225,
            187,
            114,
            219,
            243,
            87,
            83,
            254,
            248,
            95,
            139,
            1,
            26,
            198,
            149,
            158,
            30,
            44,
            146,
            237,
            166,
            70,
            199,
            9,
            26,
            32,
            138,
            251,
            102,
            14,
            230,
            244,
            102,
            250,
            142,
            53,
            210,
            190,
            9,
            23,
            169,
            98,
            14,
            156,
            148,
            99,
            168,
            204,
            145,
            108,
            191,
            223,
            189,
            58,
            202,
            61,
            52,
            254
        ]
    },

I want to convert it to a string. So far tried below mentioned things:

  1. .toString()
  2. .new String(buffer)
  3. String.fromCharCode.apply(null, buffer)
  4. buffer.map(c => String.fromCharCode(c)).join('') etc

Btw I have no access to data since when I try to access data it throws undefined so I pass the complete object which in this case is buffer to parse which is not returning the proper result. It is returning "\b Ì jQßô\f:l{Ê­3DZz²ú_¿;t_ ¹ÐÇævËçÕ§MAjHríþ;!/Ýdãi",

Can someone help me with what it is that I am doing wrong?

10
  • You need to share the code where you get the buffer Commented Jul 29, 2019 at 9:33
  • which is not returning the proper result. So what is the proper result? Those numbers don't look like they'd result in readable ascii. Commented Jul 29, 2019 at 9:34
  • @TienDuong I get the buffer as part of the response from fabric node js sdk when I call of its function. Commented Jul 29, 2019 at 9:36
  • There's no specific code as such. Its just a function call. let response = await channel.queryInfo(peer, true); Here's the function call if this helps Commented Jul 29, 2019 at 9:37
  • @tkausl yeah, I did not consider that possibility. It could be returning whatever i am getting right now. Commented Jul 29, 2019 at 9:38

2 Answers 2

1

You should pass the encoding to the .toString() function like in thew following example:

foo.toString('utf8');
Sign up to request clarification or add additional context in comments.

Comments

0

Try this :

let data = {"buffer": {
    "type": "Buffer",
    "data": [
        8,
        4,
        18,
        32,
        203,
        243,
        203,
        190,
        254,
        80,
        185,
        227,
        225,
        187,
        114,
        219,
        243,
        87,
        83,
        254,
        248,
        95,
        139,
        1,
        26,
        198,
        149,
        158,
        30,
        44,
        146,
        237,
        166,
        70,
        199,
        9,
        26,
        32,
        138,
        251,
        102,
        14,
        230,
        244,
        102,
        250,
        142,
        53,
        210,
        190,
        9,
        23,
        169,
        98,
        14,
        156,
        148,
        99,
        168,
        204,
        145,
        108,
        191,
        223,
        189,
        58,
        202,
        61,
        52,
        254
    ]
}}
let newstr = JSON.stringify(data);
console.log(newstr)
console.log(JSON.parse(newstr).buffer.data)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.