>> node
>>fs.readFile('finnish.txt', function(err,data){
console.log(data);
});
// Output string is not what we want >>fs.readFile('finnish.txt', function(err, data){
console.log(data.toString());
});
// Ouptu is ok /* Encoding */
>>fs.readFile('finnish.txt', 'utf8', function(err,data){
console.log(data);
});
// Ouptu is ok >>str = '\u0048\u0065\u006c\u006f \u0057\u006f\u0072\u006c\u0064\u0021';
'Hello World' str.length
12 Buffer.byteLength(str, 'utf8')
12 But the Buffer.byteLength and str.length are not equal!! ------ >>str = '\u00bd + \u00bc = \u00be';
'1/2 + 1/4 = 3/4'; str.length
9 Buffer.byteLength(str, 'utf8')
12 ----- >>buf = new Buffer(5)
>>buf.write('hello wolrd');
>>buf.toString()
'hello' >>buf.write('hello', 2);
hehel >>buf.write('xxxx', 2, 1);
>>buf.toString()
hexel >>buf.write('xxxx', 2, 2);
>>buf.toString()
hexxel >>buf.write('yyyy', 2, 1, 'utf8')
>>buf.toString()
heyel ----- buf1 = new Buffer('1234')
buf2 = new Buffer('0123')
buf3 = new Buffer('1234') buf1.compare(buf2)
1 //not equal
buf1.compare(buf3)
0 //equal buf1.equals(buf2)
false >>var arr = [buf1, buf2]
>>arr.sort(Buffer.compare)
[ <Buffer 30 31 32 33>, <Buffer 31 32 33 34> ] >>buf.toJSON()
{type: 'Buffer', data: [104, 101, 121, 101, 108]} >> buf = new Buffer('Hello World!');
>> buf2 0 buf.slice(0,3) buf2.toString();
'Hel'

In this lesson, we cover the Node.js Buffer object in detail. Not only will you learn that the buffer object is a reference to a memory space outside of the V8 engine, but you will learn practical methods to access it, modify it, and convert it to standard Javascript objects that can be used by your code. Examples and discussion are provided for using the toString() method, determining the byte length of the buffer, writing to a buffer, how to avoid truncating data, comparing buffers for equality using both compare() and equals(), and copying buffers using slice().

[Node.js] Node.js Buffers的更多相关文章

  1. 在Sublime Text 3 中安装SublimeLinter,Node.js进行JS&CSS代码校验

    转载自:http://www.wiibil.com/website/sublimelinter-jshint-csslint.html 在Sublime Text中安装SublimeLinter,No ...

  2. JS, Node.js, npm简介

    序 听过JS,听过Node,也听过Node.js,还听过npm,然而并不是很清楚的知道都代表什么,这两天调接口,然后前端同学很忙,就自己把前端代码拿过来跑了,也趁机了解一下这几个概念,下边做个小的总结 ...

  3. node-sass 安装卡在 node scripts/install.js 解决办法

    转自:https://segmentfault.com/a/1190000005921721 ======== 7月12日更新 ======== 总的来说就是两种方法解决: SASS_BINARY_S ...

  4. [Node.js] Node + Redis 实现分布式Session方案

    原文地址: http://www.moye.me/?p=565 Session是什么? Session 是面向连接的状态信息,是对 Http 无状态协议的补充. Session 怎么工作? Sessi ...

  5. soket.io.js + angular.js + express.js(node.js)

    soket.io.js + angular.js + express.js(node.js) 今天搭建个soket.io.js + angular.js + express.js的环境, 采坑无数,特 ...

  6. 一统江湖的大前端(2)—— Mock.js + Node.js 如何与后端潇洒分手

    <一统江湖的大前端>系列是自己的前端学习笔记,旨在介绍javascript在非网页开发领域的应用案例和发现各类好玩的js库,不定期更新.如果你对前端的理解还是写写页面绑绑事件,那你真的是有 ...

  7. node.js(node.js+mongoose小案例)_实现简单的注册登录退出

    一.前言 通过node.js基本知识对node.js基本知识的一个简单应用 1.注册 2.登录 3.退出 二.基本内容 1.项目结构搭建如图所示 2.这个小案列中用到了art-template子模板以 ...

  8. node scripts/install.js 停顿解决办法

    参考:node-sass 安装卡在 node scripts/install.js 解决办法 在安装hexo的时候,运行: npm install hexo-cli -g 卡死在了 node scri ...

  9. node启动appium.js

    node启动appium.js,appium.js目录中不能有空格或者(x86)等字样

  10. 构建工具是如何用 node 操作 html/js/css/md 文件的

    构建工具是如何用 node 操作 html/js/css/md 文件的 从本质上来说,html/js/css/md ... 源代码文件都是文本文件,文本文件的内容都是字符串,对文本文件的操作其实就是对 ...

随机推荐

  1. jsp EL表达式 字符串的比较

    jsp EL表达式 字符串的比较 跟JavaScript一样,直接使用两个等于号即可:== 代码如下: <c:if test="${highLight == 'visa'}" ...

  2. Freemarker 对null值报错的处理

    忽略null值 假设前提:user.name为null ${user.name},异常 ${user.name!},显示空白 ${user.name!'vakin'},若user.name不为空则显示 ...

  3. SPRING IN ACTION 第4版笔记-第一章-003-AOP介绍

    一.目标 要在BraveKnight调用embarkOnQuest()前后各做一些处理(调用Minstrel的方法) 二. 1.minstrel.xml <?xml version=" ...

  4. SQLite 对中文路径的支持(用到了StringToWideChar和Utf8Encode在D7的System单元中自带)

    最近用SQLITE作为数据库,发现,如果直接传递带中文路径或文件名的数据库,会导致无法打开数据库的情况.看了一下SQLITE的源码,才发现,原来SQLITE中是用UTF8编码进行文件打开操作的. 所以 ...

  5. 最新ps cs6序列号 永久免费可用

    一. 序列号 除非是从官方购买,从其它任何途径得到的序列号(包括网上流传的注册机生成的)都是不能通过联网验证的,必须使用破解补丁,或是通过修改hosts文件的方式来激活.因此,除了正版,不存在所谓的“ ...

  6. [LeetCode] 303. Range Sum Query - Immutable (Easy)

    303. Range Sum Query - Immutable class NumArray { private: vector<int> v; public: NumArray(vec ...

  7. perl use base 继承

    centos6.5:/root/podinns/lib#cat First.pm package First; use base qw(Second); sub new { my $self = {} ...

  8. git tag的使用

    查看所有的标签git tag 删除某一个标签git tag -d tagName 创建带注释的标签 git tag -a tagName -m "annotate" 轻量级标签 g ...

  9. Spark大数据处理技术

    全球首部全面介绍Spark及Spark生态圈相关技术的技术书籍 俯览未来大局,不失精细剖析,呈现一个现代大数据框架的架构原理和实现细节 透彻讲解Spark原理和架构,以及部署模式.调度框架.存储管理及 ...

  10. MySQL数据库的优化-运维架构师必会高薪技能,笔者近六年来一线城市工作实战经验

    原文地址:http://liangweilinux.blog.51cto.com/8340258/1728131 首先在此感谢下我的老师年一线实战经验,我当然不能和我的老师平起平坐,得到老师三分之一的 ...