Convert object type Buffer to string in javascript [closed]

-2

I have an object, which is part of a json that I bring from the database, with this structure:

data:[83,78,77,80,32,65,103,101,110,116,32,52,65,32,32,32,86,101,114,115,105,111,110,32,110,97,109,101,32,48,46,50,46,53,32,40,50,53,41] (39)
type: "Buffer"

I would like to know how to convert it to a text string (decoding the characters), I tried with toString('utf8') but still printing [object Object] , is there any other way to do it?

The resulting value must be SNMP Agent 4A Version name 0.2.5 (25)--SysDescr .

pdta: the database I'm working with is postgresql and a column of the table I use json saves.

    
asked by Kevin Puscan Ortiz 26.04.2018 в 15:39
source

2 answers

1

to convert it to string, provided you do not have methods you can use JSON.stringify () In order to count the objects in javascript are nothing more than associative array of type key value, where a value can be a function in itself.

let data = {
  data: [83, 78, 77, 80, 32, 65, 103, 101, 110, 116],
  type: "Buffer"
};

$("#resultado").html(JSON.stringify(data, null, '  '));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<pre id="resultado"></pre>
    
answered by 26.04.2018 в 15:48
1

If I do not understand wrong, what you need is to transform the numbers to characters:

let array = [83, 78, 77, 80, 32, 65, 103, 101, 110, 116, 32, 52, 65, 32,
    32, 32, 86, 101, 114, 115, 105, 111, 110, 32, 110, 97, 109, 101,
    32, 48, 46, 50 ,46 ,53 ,32 ,40 ,50 ,53 ,41 ];


console.log(String.fromCharCode(...array))

EDIT: Indeed, the array of numbers, when interpreted as character codes, gives the result you are looking for: SNMP Agent 4A Version name 0.2.5 (25)

    
answered by 27.04.2018 в 11:00