node緩沖區_Node.js緩沖區介紹

node緩沖區

什么是緩沖液? (What are Buffers?)

Binary is simply a set or a collection of 1 and 0. Each number in a binary, each 1 and 0 in a set are called a bit. Computer converts the data to this binary format to store and perform operations. For example, the following are five different binaries:

Binary只是10的集合或集合。 二進制中的每個數字,一組中的每個1和0稱為bit 。 計算機將數據轉換為該二進制格式以存儲和執行操作。 例如,以下是五個不同的二進制文件:

10, 01, 001, 1110, 00101011

10, 01, 001, 1110, 00101011

JavaScript does not have a byte type data in its core API. To handle binary data Node.js includes a binary buffer implementation with a global module called Buffer.

JavaScript的核心API中沒有字節類型數據。 為了處理二進制數據,Node.js包括一個二進制緩沖區實現,該實現帶有一個名為Buffer的全局模塊。

創建一個緩沖區 (Creating a Buffer)

There are different ways you can create a buffer in Node.js. You can create an empty buffer by with a size of 10 bytes.

您可以使用多種方法在Node.js中創建緩沖區。 您可以創建一個10字節大小的空緩沖區。

const buf1 = Buffer.alloc(10);

From UTF-8-encoded strings, the creation is like this:

通過UTF-8編碼的字符串,創建過程如下所示:

const buf2 = Buffer.from('Hello World!');

There are different accepted encoding when creating a Buffer:

創建緩沖區時,可以接受不同的編碼:

  • ascii

    ASCII
  • utf-8

    utf-8
  • base64:

    base64:
  • latin1

    拉丁語1
  • binary

    二元
  • hex

    十六進制

There are three separate functions allocated in the Buffer API to use and create new buffers. In above examples we have seen alloc() and from(). The third one is allocUnsafe().

在緩沖區API中分配了三個單獨的函數,以使用和創建新緩沖區。 在上面的示例中,我們看到了alloc()from() 。 第三個是allocUnsafe()

const buf3 = Buffer.allocUnsafe(10);

When returned, this function might contain old data that needs to be overwritten.

返回時,此函數可能包含需要覆蓋的舊數據。

與緩沖液的相互作用 (Interactions with Buffer)

There are different interactions that can be made with the Buffer API. We are going to cover most of them here. Let us start with converting a buffer to JSON.

Buffer API可以進行不同的交互。 我們將在這里介紹其中的大多數內容。 讓我們從將緩沖區轉換為JSON開始。

let bufferOne = Buffer.from('This is a buffer example.');
console.log(bufferOne);// Output: <Buffer 54 68 69 73 20 69 73 20 61 20 62 75 66 66 65 72 20 65 78 61 6d 70 6c 65 2e>let json = JSON.stringify(bufferOne);
console.log(json);// Output: {"type": "Buffer", "data": [84,104,105,115,32,105,115,32,97,32,98,117,102,102,101,114,32,101,120,97,109,112,108,101,46]}

The JSON specifies that the type of object being transformed is a Buffer, and its data. Converting an empty buffer to JSON will show us that it contains nothing but zeros.

JSON指定要轉換的對象的類型是Buffer及其數據。 將空緩沖區轉換為JSON將向我們顯示,它只包含零。

const emptyBuf = Buffer.alloc(10);emptyBuf.toJSON();// Output: { "type": "Buffer", "data": [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ] }

Do notice that, Buffer API also provides a direct function toJSON() to convert a buffer into a JSON object. To examine the size of a buffer, we can use length method.

請注意,Buffer API還提供了toJSON()的直接函數,可將緩沖區轉換為JSON對象。 要檢查緩沖區的大小,我們可以使用length方法。

emptyBuf.length;
// Output: 10

Now let us convert buffer to a readable string, in our case, the utf-8 encoded.

現在,讓我們將緩沖區轉換為可讀字符串,在本例中為utf-8編碼。

console.log(bufferOne.toString('utf8'));// Output: This is a buffer example.

.toString() by default converts a buffer to a utf-8 format string. This is how you decode a buffer. If you specify an encoding you can convert the buffer to another encoding

默認情況下, .toString()將緩沖區轉換為utf-8格式的字符串。 這就是解碼緩沖區的方式。 如果指定一種編碼,則可以將緩沖區轉換為另一種編碼

console.log(bufferOne.toString('base64'));

有關緩沖區的更多信息: (More info on Buffers:)

  • Need a better understanding of buffers in Node.js? Check this out.

    是否需要更好地了解Node.js中的緩沖區? 看一下這個。

翻譯自: https://www.freecodecamp.org/news/node-js-buffer-explained/

node緩沖區

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/390355.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390355.shtml
英文地址,請注明出處:http://en.pswp.cn/news/390355.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

專訪趙加雨:WebRTC在網易云信的落地

去年的這個時候&#xff0c;在市面上公開表示使用WebRTC的公司還沒幾家&#xff0c;但2018年以來&#xff0c;宣布采用或支持WebRTC的公司已經越來越多。實時音視頻提供商網易云信也在自研的NRTC中集成了WebRTC。在他們眼里&#xff0c;2017年是WebRTC的轉折之年&#xff0c;而…

html/css雜題

1、css選擇器&#xff1a;詳細&#xff08;http://www.ruanyifeng.com/blog/2009/03/css_selectors.html&#xff09; 派生選擇器&#xff1a;按標簽 類別選擇器&#xff1a;按class ID選擇器&#xff1a;按ID 通用選擇器&#xff1a;* 匹配所有 屬性選擇器&#xff1a;按屬性&…

黑客馬拉松 招募_我如何贏得第一次黑客馬拉松-研究,設計和編碼的2個狂野日子

黑客馬拉松 招募I had no coding or engineering background. I studied biology in college, with no clue about what to do with my degree. 我沒有編碼或工程背景。 我在大學學習生物學&#xff0c;但不知道如何處理我的學位。 My first jobs were making cold calls in s…

1、Linux命令隨筆

1 Linux命令總結2 3 man 命令幫助;4 help 命令的幫助&#xff08;bash的內置命令&#xff09;;5 ls list,查看目錄列表;6 -ld&#xff1a;查看目錄權限;7 -l:(long)長格式顯示屬性;8 -F:給不同的文件類型結尾加標識9 -p:給目錄加斜線10 …

1137. 第 N 個泰波那契數

泰波那契序列 Tn 定義如下&#xff1a; T0 0, T1 1, T2 1, 且在 n > 0 的條件下 Tn3 Tn Tn1 Tn2 給你整數 n&#xff0c;請返回第 n 個泰波那契數 Tn 的值。 示例 1&#xff1a; 輸入&#xff1a;n 4 輸出&#xff1a;4 解釋&#xff1a; T_3 0 1 1 2 T_4 1…

web圖像_Web圖像優化的基本介紹

web圖像Images are an essential ingredient of most websites. The visual quality of pictures has a direct impact on the brand image and the message those images convey. And the weight of images usually accounts for a 40-60% of the data transferred on the web…

ElasticSearch客戶端注解使用介紹

The best elasticsearch highlevel java rest api-----bboss 1.ElasticSearch客戶端bboss提供了一系列注解 ESId 用于標識實體對象中作為docid的屬性&#xff0c;該注解只有一個persistent 布爾值屬性&#xff0c;用于控制被本注解標注的字段屬性是否作為普通文檔屬性保存&am…

5827. 檢查操作是否合法

給你一個下標從 0 開始的 8 x 8 網格 board &#xff0c;其中 board[r][c] 表示游戲棋盤上的格子 (r, c) 。棋盤上空格用 ‘.’ 表示&#xff0c;白色格子用 ‘W’ 表示&#xff0c;黑色格子用 ‘B’ 表示。 游戲中每次操作步驟為&#xff1a;選擇一個空格子&#xff0c;將它變…

團隊的遠程管理_遠程團隊指南:如何管理您的遠程軟件開發團隊

團隊的遠程管理Guides to help you work remotely seem to have swept through the Internet these days. 這些天來&#xff0c;幫助您遠程工作的指南似乎席卷了Internet。 Do this, avoid that, stay productive, and all those run-of-the-mill tips we’ve already tried o…

JS 正則 錢

function ValidateIsDecial(sValue) {return (!sValue && !!!sValue && /^[0-9]{1,10}(\.[0-9]{0,2})?$/.test(sValue)); };驗證 decimal(12,2) 小數點前允許10位,小數點后允許2位 1234567890 true 12345678901 false 0123456789 true 01234567891 false 123.…

5193. 刪除字符使字符串變好

5193. 刪除字符使字符串變好 一個字符串如果沒有 三個連續 相同字符&#xff0c;那么它就是一個 好字符串 。 給你一個字符串 s &#xff0c;請你從 s 刪除 最少 的字符&#xff0c;使它變成一個 好字符串 。 請你返回刪除后的字符串。題目數據保證答案總是 唯一的 。 示例 …

2020計算機頂級大會_2020年頂級遠程調試工具

2020計算機頂級大會When it comes to debugging, the tool you use is extremely important and can determine how easy is is to fix problems within your code. 在調試方面&#xff0c;您使用的工具非常重要&#xff0c;可以確定在代碼中修復問題的難易程度。 In the earl…

BZOJ5292 洛谷4457 LOJ2513:[BJOI2018]治療之雨——題解

https://www.lydsy.com/JudgeOnline/problem.php?id5292 https://www.luogu.org/problemnew/show/P4457 https://loj.ac/problem/2513 你現在有m1個數&#xff1a;第一個為p&#xff0c;最小值為0&#xff0c;最大值為n&#xff1b;剩下m個都是無窮&#xff0c;沒有最小值或最…

PHP--------微信網頁開發實現微信掃碼功能

今天說說微商城項目中用到的掃一掃這個功能&#xff0c;分享一下&#xff0c;希望對各位有所幫助。 前提&#xff1a;要有公眾號&#xff0c;和通過微信認證&#xff0c;綁定域名&#xff0c;得到相應信息&#xff0c;appid&#xff0c;appsecret等。 微信開發文檔&#xff1a;…

313. 超級丑數

超級丑數 是一個正整數&#xff0c;并滿足其所有質因數都出現在質數數組 primes 中。 給你一個整數 n 和一個整數數組 primes &#xff0c;返回第 n 個 超級丑數 。 題目數據保證第 n 個 超級丑數 在 32-bit 帶符號整數范圍內。 示例 1&#xff1a; 輸入&#xff1a;n 12,…

初創公司股本結構_我如何向初創公司的開發團隊添加一些結構-以及從過程中學到的東西

初創公司股本結構Until recently, Id spent the last 4 years of my career at FinTech start-ups. Id always worked for smaller companies, and being at a start-up was the next logical step in looking for roles where I could make the biggest difference. 直到最近…

拿什么拯救你,我的面試之——從零打卡刷Leetcode(No.003)

寫在前邊&#xff1a;小詹一直覺得自己編程能力不強&#xff0c;想在網上刷題&#xff0c;又怕不能堅持。不知道有木有和小伙伴和小詹一樣想找個人一起刷題呢&#xff1f;歡迎和小詹一起定期刷leetcode&#xff0c;每周一周五更新一題&#xff0c;每一題都吃透&#xff0c;歡迎…

146. LRU 緩存機制

146. LRU 緩存機制 運用你所掌握的數據結構&#xff0c;設計和實現一個 LRU (最近最少使用) 緩存機制 。 實現 LRUCache 類&#xff1a; LRUCache(int capacity) 以正整數作為容量 capacity 初始化 LRU 緩存 int get(int key) 如果關鍵字 key 存在于緩存中&#xff0c;則返回…

[SQL] 請教一下 count里面有case when 一般情況下啥時候用

http://www.itpub.net/forum.php?modviewthread&tid1810967 問題: 比如 count(case when pday_id${deal_date} then 1 end) 我有點想不明白具體什么情況下count&#xff08;&#xff09; 這個小括號里面還要用case when 大家做BI統計的時候一般什么情況用啊 還有個…

路由器架設虛擬服務器讓外網訪問到本地網站

確定電腦與路由器正確連接&#xff0c;并且已連至互聯網。在地址欄中輸入192.168.0.1回車&#xff0c;輸入用戶名密碼&#xff0c;進入路由器主界面。 然后點擊左側菜單中的“虛擬服務器”&#xff0c;——“端口段映射”打開“端口段映射”界面。 由于網站用的是80端口&#x…