The Buffer from method in nodejs can be used to create a new Buffer from a string, or array of numbers, or object in general. In many cases it might be one of th best ways to create a new Buffer, but there are other options as well of course. In any case when creating a nodejs project buffers do come up now and then, and the from method of the Buffer global comes in handy when there is a desire to quickly create a buffer with an initial value derives from a hex string for example. So lets take a quick look at some examples of the buffer from method in action in nodejs.
For starters the buffer from method can be used by just passing a string as the first argument when calling it. When doing so the default encoding observed is utf8, however and alternative encoding such as ascii can be given as the second argument.
When using the buffer from method this way the size and starting content of the resulting buffer depending on the length of the string and the encoding given if any. But there is must more to the buffer from method then just this so lets look at some more examples that have to do with strings, and event various kinds of objects.
So there is more to creating a buffer from a string that just giving the string to the buffer from method and being done with it. There is the deferences between ascii and utf8 encoding, and there is also the question of hex strings and other forms of data the are sored in string form. So in this section I will be taking a look at some more examples that have to do with creating new buffers with a string value in nodejs.
What is nice about ascii is that it is a simple and easy to understand encoding in which each character is one byte, but that it not the case with utf8. The eight in utf8 does not mean eight bits per character, it just means that single byte units of data are used to represent characters. Sure it it one byte per character when it comes to sticking to the ascii range of characters, but that is not the case when going into extended ascii and beyond with utf8 of course.
So on top of the diferences between utf8 and ascii there is also the nature of hex strings. If I have a string of hex values where there are two chars for each byte then I can use the hex encoding option to create a buffer from a hex string.
So if it is an array of numbers that are between 0 and 255 then just pass the array as the first argument. The length of the array will be equal to the number of elements, and the value for each byte in the array will correspond to the values for each element.
So the Array should be an array of numbers, if however you are dealing with an array of string values such as hex values. The the way to go about doing it will involve converting the array of string values to a single string, and then using that with the desired encoding as covered in the section in which I went over creating buffers from strings above.
So then there is creating buffers from objects in general. Any object that has a valueOf property can be passed as the first argument. In this case as the value that is the result of calling the valueOf property of the object is what will be used to create the starting size and content of the buffer.
In this example I am creating my one custom object with a valueOf property to show what happens when creating a buffer with an object.
Well there are some basic example of using the buffer from method in nodejs to create new buffers from strings, arrays, and objects in general. There are many other method of interest in the buffer global as well though. The buffer.alloc method is what can be used to create a new zero filled buffer, and then there is the buffer write method that can be used to put data into the buffer. So much more to write about just when it comes to buffers alone when it comes to nodejs.