[Node.js] Node.js Buffers
>> 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的更多相关文章
- 在Sublime Text 3 中安装SublimeLinter,Node.js进行JS&CSS代码校验
转载自:http://www.wiibil.com/website/sublimelinter-jshint-csslint.html 在Sublime Text中安装SublimeLinter,No ...
- JS, Node.js, npm简介
序 听过JS,听过Node,也听过Node.js,还听过npm,然而并不是很清楚的知道都代表什么,这两天调接口,然后前端同学很忙,就自己把前端代码拿过来跑了,也趁机了解一下这几个概念,下边做个小的总结 ...
- node-sass 安装卡在 node scripts/install.js 解决办法
转自:https://segmentfault.com/a/1190000005921721 ======== 7月12日更新 ======== 总的来说就是两种方法解决: SASS_BINARY_S ...
- [Node.js] Node + Redis 实现分布式Session方案
原文地址: http://www.moye.me/?p=565 Session是什么? Session 是面向连接的状态信息,是对 Http 无状态协议的补充. Session 怎么工作? Sessi ...
- 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的环境, 采坑无数,特 ...
- 一统江湖的大前端(2)—— Mock.js + Node.js 如何与后端潇洒分手
<一统江湖的大前端>系列是自己的前端学习笔记,旨在介绍javascript在非网页开发领域的应用案例和发现各类好玩的js库,不定期更新.如果你对前端的理解还是写写页面绑绑事件,那你真的是有 ...
- node.js(node.js+mongoose小案例)_实现简单的注册登录退出
一.前言 通过node.js基本知识对node.js基本知识的一个简单应用 1.注册 2.登录 3.退出 二.基本内容 1.项目结构搭建如图所示 2.这个小案列中用到了art-template子模板以 ...
- node scripts/install.js 停顿解决办法
参考:node-sass 安装卡在 node scripts/install.js 解决办法 在安装hexo的时候,运行: npm install hexo-cli -g 卡死在了 node scri ...
- node启动appium.js
node启动appium.js,appium.js目录中不能有空格或者(x86)等字样
- 构建工具是如何用 node 操作 html/js/css/md 文件的
构建工具是如何用 node 操作 html/js/css/md 文件的 从本质上来说,html/js/css/md ... 源代码文件都是文本文件,文本文件的内容都是字符串,对文本文件的操作其实就是对 ...
随机推荐
- SQL Express几个版本的区别
对于这三个文件:SQLEXPR32_x86_CHS.exe.SQLEXPR_x86_CHS.exe. SQLEXPR_x64_CHS.exe,大家一看就知道是sqlserver的express版本,但 ...
- Latex 常用知识点存档
前言: 本篇仅作为自己的知识存档. $Latex$是什么,就不用介绍了,网上好多教程和知识点,当忘记的时候搜一下就可以了. 本科做美赛和毕设的时候用的$Latex$排版,最近开始在博客园写点东东,发现 ...
- 【简译】jQuery对象的奥秘:基础介绍
本文翻译自此文章 你有没有遇到过类似$(".cta").click(function(){})这样的JavaScript代码并且在想“$('#x')是什么”?如果这些对你想天书一样 ...
- Android 删除指定文件代码
package com.tware.pdfdrop; import java.io.File; import android.app.Activity; import android.graphics ...
- \u202e
document.write('\u202e1\u202e2\u202e3')
- 机器更换登录密码重启,然后SQL Server登录不了
解决方法:
- Tag file directory /struts-tags does not start with "/WEB-INF/tags"
使用自定义标签,记得引用路径 <%@taglib prefix="s" uri="/struts-tags" %>
- HDU-1518 Square(DFS)
Square Time Limit : 10000/5000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submi ...
- TFS2010中文版安装
VS2010的中文版出来一段时间了,对TFS2010的了解,也有一段时间了,只不过中文版还是首次见到.于是把第一次安装的图片分享出来,公供参数. TFS2010安装环境是操作系统为Windows Se ...
- ARM学习笔记13——LED驱动程序设计
首先我们要根据开发板原理图得到控制LED灯的引脚是哪个,我们现在以LED1为例,我们已经知道LED1由S5PV210的GPC1_3控制,因此我们按如下步骤进行: 第一步是配制S5PV210的GPC1_ ...