一。目录

➤ Understanding why you need buffers in Node

➤ Creating a buffer from a string

➤ Converting a buffer to a string

➤ Manipulating the bytes in a buffer

➤ Slicing and copying a buffer

二。Understanding why you need buffers in Node

 JavaScript is good at handling strings, but because it was initially designed to manipulate HTML documents, it is not very good at

handling binary data. JavaScript doesn’t have a byte type — it just has numbers — or structured types, or even byte arrays: It just

has strings.  

  Because Node is based on JavaScript, Node can handle text protocols like HTTP, but you can also use it to talk to databases,

manipulate images, and handle fi le uploads. As you can imagine, doing this using only strings is very difficult. In the early days,

Node handled binary data by encoding each byte inside a text character, but that proved to be wasteful, slow,unreliable, and hard

to manipulate.

  To make these types of binary-handling tasks easier, Node includes a binary buffer implementation, which is exposed as a JavaScript

API under the Buffer pseudo-class. A buffer length is specified in bytes, and you can randomly set and get bytes from a buffer.

  Another thing that is special about this buffer class is that the memory where the data sits is allocated outside of the JavaScript VM

memory heap. This means that these objects will not be moved around by the garbage-collection  algorithm: It will sit in this permanent

memory address without changing, which saves CPU cycles that would be wasted making memory copies of the buffer contents.

三。GETTING AND SETTING BYTES IN A BUFFER

  After you create or receive a buffer, you might want to inspect and change its contents. You can access the byte value on any

position of a buffer by using the [] operator like this:

var buf = new Buffer('my buffer content');
// accessing the 10th position of buf
console.log(buf[10]); // -> 99

  //You can also manipulate the content of any position:
  buf[20] = 125; // set the 21th byte to 125

 

四。注意 

NOTE When you create an initialized buffer, keep in mind that it will contain random bytes, not zeros.

var buf = new Buffer(1024);
console.log(buf[100]); // -> 5 (some random number)

  

NOTE In certain cases, some buffer operations will not yield an error. For instance:

➤ If you set any position of the buffer to a number greater than 255, that position will be assigned the 256 modulo value.

➤ If you assign the number 256 to a buffer position, you will actually be assigning the value 0.

➤ If you assign a fractional number like 100.7 to a buffer position, the buffer position will store the integer part — 100 in this case.

➤ If you try to assign a position that is out of bounds, the assignment will fail and the buffer will remain unaltered.

五。字节序

  又称端序,尾序(英语:ByteOrder, or Endianness)。现代的计算机系统一般采用字节( 8 bit Byte)作为逻辑寻址单位。当物理单位的长度大于

1个字节时,就要区分字节顺序。典型的情况是整数在内存中的存放方式和网络传输的传输顺序。目前在各种体系的计算机中通常采用的字节存储机制主要

有两种:big-edian和little-endian。

六。字节序示例

 var b = new Buffer('123456789');
console.log(b.length); //9
console.log(b);          //<Buffer 31 32 33 34 35 36 37 38 39> var b2 = new Buffer(4);
b2.writeInt32LE(123456789,0);
console.log(b2.length);      //4
console.log(b2);          //<Buffer b5 e5 39 07>

nodejs(三)Buffer module & Byte Order的更多相关文章

  1. 字节顺序标记——BOM,Byte Order Mark

    定义 BOM(Byte Order Mark),字节顺序标记,出现在文本文件头部,Unicode编码标准中用于标识文件是采用哪种格式的编码.     介绍 UTF-8 不需要 BOM,尽管 Unico ...

  2. LITTLE-ENDIAN(小字节序、低字节序) BOM——Byte Order Mark 字节序标记 数据在内存中的存放顺序

    总结: 1. endian 字节存放次序 字节序,顾名思义字节的顺序,再多说两句就是大于一个字节类型的数据在内存中的存放顺序(一个字节的数据当然就无需谈顺序的问题了). 2. LITTLE-ENDIA ...

  3. UTF-8文件的Unicode签名BOM(Byte Order Mark)问题记录(EF BB BF)

    背景 楼主测试的批量发送信息功能上线之后,后台发现存在少量的ERROR日志,日志内容为手机号码格式不正确. 此前测试过程中没有出现过此类问题,从运营人员拿到的发送列表的TXT,号码是符合规则的,且格式 ...

  4. StreamWriter结合UTF-8编码使用不当,会造成BOM(Byte Order Mark )问题生成乱码(转载)

    问: I was using HttpWebRequest to try a rest api in ASP.NET Core MVC.Here is my HttpWebRequest client ...

  5. nodejs里的module.exports和exports的关系

    关于node里面的module.exports和exports的异同,网上已经有很多的资料,很多的文章,很多的博客,看了很多,好像懂了,又好像不懂,过几天又不懂了...大致总结是这样的: //下面这种 ...

  6. nodejs: 理解Buffer

    学习nodejs中buffer这一章,有一段写到buffer的拼接,其中一段源码非常优美,特拿来与大家共享. var chunks = []; var size = 0; res.on('data', ...

  7. 字节序(byte order)和位序(bit order)

    字节序(byte order)和位序(bit order)  在网络编程中经常会提到网络字节序和主机序,也就是说当一个对象由多个字节组成的时候需要注意对象的多个字节在内存中的顺序.  以前我也基本只了 ...

  8. 编程-Byte order & Bit order

    https://mp.weixin.qq.com/s/B9rKps4YsLiDTBkRks8rmQ 看到比特序和字节序放在一起被提及,想必就已经填补了概念拼图里面缺失的那一块了,这一块正是比特序. 一 ...

  9. 大熊君大话NodeJS之------Buffer模块

    一,开篇分析 所谓缓冲区Buffer,就是 "临时存贮区" 的意思,是暂时存放输入输出数据的一段内存. JS语言自身只有字符串数据类型,没有二进制数据类型,因此NodeJS提供了一 ...

随机推荐

  1. Repeater的j简单使用

    嘿嘿,今天没有任务,所以突然想起来我之前记得笔 记说要把repeater的使用以及获取值的详细使用总结一下,所以这就闲来无聊总结一下,虽然现在不会使用这些小知识点的,但是我感觉自己的学习还是要 不断地 ...

  2. html中属于布尔类型的属性

    1.noshade,用来表示有无阴影,多用于在<hr />标签当中 2.ckecked,用来表示是否默认选中,多用于单选按钮<input type="radio" ...

  3. thrift安装及使用

    下载Thrift:http://thrift.apache.org/download ■ 将thrift-0.11.0.exe重命名为thrift.exe: ■ 解压thrift-0.11.0.tar ...

  4. spring配置文件中xsd引用问题

    XML的一些概念 首先来看下xml的一些概念: xml的schema里有namespace,可以给它起个别名.比如常见的spring的namespace:   xmlns:mvc="http ...

  5. php API接口入门

    1.简述: api接口开发,其实和平时开发逻辑差不多:但是也有略微差异: 平时使用mvc开发网站的思路一般是都 由控制器 去 调用模型,模型返回数据,再由控制器把数据放到视图中,展现给用户: api开 ...

  6. Python pyQt4/pyQt5 学习笔记1(空白窗口,按钮,控件事件,控件提示,窗体显示到屏幕中间,messagebox)

    PyQt4是用来编写有图形界面程序(GUI applications)的一个工具包.PyQt4作为一个Python模块来使用,它有440个类和超过6000种函数和方法.同时它也是一个可以在几乎所有主流 ...

  7. 动力学仿真引擎ODE的学习笔记,C#演示(一)

    ®版权声明:本文为博主原创文章,未经博主允许不得转载. 一.ODE介绍与平台搭建. 接触到动力学仿真引擎, 是因为笔者的一款PLC仿真软件需要3D仿真.我需要达到的效果是,以3D方式构建出工控行业中常 ...

  8. Android 线程与主线程

    网络连接需要时间.Web服务器可能需要1~2秒的时间来响应,文件下载则耗时更久.考虑到这个因素,Android禁止任何主线程网络连接行为.即使强行为之,Android也会抛出NetworkOnMain ...

  9. 你可能不知道的shell、bash二三事(Centos 7)

    个人.bashrc: ~/.bashrc: # .bashrc # User specific aliases and functions alias rm='rm -i' alias cp='cp ...

  10. vs 的git插件

    在vs2013上Tfs提供的git管理完全无法理解他的管理方式,还是 Git Source Control Provider 好用用. 下载地址: [http://gitscc.codeplex.co ...