nodejs和js里的gbk与utf8编码转换

JavaScript实现utf8和gbk字符串互相转换(附完整源码) 阅读详情
js 与 nodejs 都有 TextEncoder TextDecoder ,可以进行编码之间的转换

nodejs 版本需要 >=8.3.0 才支持 util.TextEncoder,util.TextDecoder

gbk 转 utf8

new TextDecoder('gbk').decode(new Uint8Array([ 47, 63, 214, 208, 206, 196, 61, 56, 56 ]).buffer)
// out:  "/?中文=88"
nodejs 建议通过 iconv or iconv-lite第三方库进行编码之间的转换。因为使用原生 TextEncoder需要完整的 ICU Data 具体看这里 util_class_util_textdecoder

utf8 gbk 互转

let iconv = require('iconv-lite');
// 这里是utf8
let str = '/?中文=88';
// 转换成gbk
let encoded = iconv.encode(str, 'gbk');
console.log(encoded,encoded.toJSON())

str = iconv.decode(Buffer.from([ 47, 63, 214, 208, 206, 196, 61, 56, 56 ]), 'gbk');
console.log('utf8 str:',str)

// out: Buffer <2F, 3F, D6, D0, CE, C4, 3D, 38, 38>
// Object {data: [47, 63, 214, 208, 206, 196, 61, 56, 56], type: "Buffer"}
// utf8 str: "/?中文=88"

感谢您的阅读,本文由 smallwhite’s Blog 版权所有。如若转载,请注明出处:smallwhite’s Blog(https://smallwhite.ml/pub/nodejs/nodejs-js-gbk-utf8-encode.html)

JavaScript实现字符编码转换utf-8/gbk(附完整源码) 阅读详情

相关推荐

js实现GBK编码,避免默认utf-8编码

前端页面通过ajax发送请求,参数都是默认使用utf-8编码格式对参数进行url编码,这样的后果就是,当后台页面如果使用gbk编码的话,会出现乱码的情况,然后网上找到一个大神写的gbk.js. 但是里面没有做出详细介绍,这里简单介绍: 页面引入js,通过$.ajax传递参数的时候,对参数使用\$URL.encode(param)方法,对参数进行gbk编码;后台接受参数之后,再使用encoder.encode(param,”gbk”);就可以解析参数了. 大家还有什么问题都可以留言,一起沟通解决. 使用方法:https://blog.csdn.net/shawemou/article/details/81172733

JS中utf8和GBK的字符编码转换

资料链接:http://igogogo9.iteye.com/blog/105669 1、PHP中的 json_encode 函数只限编码UTF-8的数据,当转换GBK或者GB2312等编码的数据时,会将汉字转为NULL。 2、JavaScript 中json 的使用: ① 将对象转为json字符串: JSON.stringify(obj) ② 将json转为对象

dmtnewtons的专栏 5万+

Bioconductor核心包(GO.db等)安装失败排查指南:从版本冲突到网络优化

本文详细解析了Bioconductor核心包(如GO.db)安装失败的常见原因及解决方案,包括版本冲突、网络问题和依赖关系。通过版本检查、镜像配置和依赖管理等方法,帮助用户高效解决安装难题,提升R语言生物信息学分析的工作效率。

weixin_29066781的博客 429

用JS如何把utf8转换成GBK的字符编码

function Utf8ToUnicode(strUtf8) {         var bstr = "";         var nTotalChars = strUtf8.length;        // total chars to be processed.         var nOffset = 0;                                      ...

igogogo9的专栏 8205

antdv-<a-form>的使用

是 Ant Design Vue 中用于创建表单的组件,提供了一种结构化的方式来收集用户输入。它支持多种布局、验证、数据绑定等功能,非常适合用于处理用户输入和提交数据。

2302_81312344的博客 2625

Ansys Motor-CAD:概述(EMag、THERM、LAB、MECH)

在这篇博客中,我概述了如何使用 Ansys Motor-CAD 模型、模拟、分析和后处理结果来评估电机性能,并帮助您为您的应用选择优化的电机,并通过电机设计选择实现成本效益和效率。我介绍了各种可用的电机类型、可供选择的物理模块和可配置的多物理选项。E-Mag 物理模块有许多选项卡,涵盖几何创建、材料选择、电磁设置、条件、求解器类型、解决方案模型、耦合设置等的所有设置。热物理模块有许多选项卡,涵盖几何创建、材料选择、热设置、条件、求解器类型、解决方案模型、耦合设置等的所有设置。分析结果的速度相结合。

jkh920184196的博客 1773

Nodejs编码转化问题【node.js不支持GBK,转码中文要第三方包】

Nodejs编码转化问题 目前Node.js仅支持hex、utf8、ascii、binary、base64、ucs2几种编码的转换。对于GBK,GB2312等编码,Nodejs自带的toString()方法不支持,因此中文转化的时候需要加载第三方库,主要有两个编码转换库iconv-lite和encoding,个人推荐使用encoding。 iconv-lite:是 iconv 的纯 js...

qq_24920947的博客 6727

创建XCP的CanApe工程

创建XCP的CanApe工程

weixin_44173919的博客 2898

Node.js环境中使用GBK编码

NodeJS环境中使用GBK编码Node默认支持的编码格式Node中的Buffer对象支持以下编码(以6.9.1版本为例):参考API[https://npm.taobao.org/mirrors/node/latest/docs/api/buffer.html#buffer_buffers_and_character_encodings] * ascii :7-bit ASCII data,舍

躁动de气球 1万+

Abaqus小白必看:用Micromechanics Plugin轻松搞定RVE周期性边界条件(附避坑指南)

本文为Abaqus初学者提供了使用官方Micromechanics Plugin插件高效实现复合材料代表性体积单元分析的完整指南。文章详细讲解了从插件安装、模型准备、周期性边界条件自动设置到结果验证的全流程,并重点分享了处理复杂模型和避免常见错误的实用技巧,帮助用户大幅提升RVE分析效率与准确性。

weixin_28363123的博客 817

用「OpenClaw」打造新媒体运营神器:小红书/公众号全自动选题→创作→排版→发布全流程

对于新媒体运营的从业者来说,AI不是要取代运营人员,而是把他们从繁琐的重复劳动中解放出来,专注于更核心的内容策略、用户运营、品牌建设,这才是AI在传统行业落地的真正价值。这是最终输出可发布内容的核心智能体,我们通过「OpenClaw」的Prompt工程,结合不同平台的排版规范,实现了一键生成符合要求的排版,完全替代人工重复劳动。这套系统的落地,不是技术的炫技,而是真正用AI解决了新媒体运营的核心痛点:用自动化替代重复的人工劳动,用数据决策替代经验主义,用全闭环执行替代纸上谈兵的建议。

专注于Python爬虫开发,分享爬虫技巧、项目实战与反爬经验,使用Scrapy、BeautifulSoup等工具,解决数据抓取难题。 201

node.js实现编码转GBK转UTF8,中文乱码方案!!!

在做这些之前遇见了一个需求,则是用node去做一个姓名的模糊查询。其实这是一个简单再也不能简单的事情,可是却在这个问题上卡了两天。 因为业务需要,给新功能做一个模糊查询。 首先,我得搭建了一个node的http服务,然后我配置了mysql。建立了ORM模型后,准备开始编写sql语句的时候遇见了一个奇怪的事情,由于我的node是utf8的编码。但是我们公司的数据库采用的是GBK的编码,因此导致我在进...

我的代码没有Bug的博客 1万+

大数据分析案例-基于决策树算法构建信用卡违约预测模型

本项目旨在使用机器学习等算法构建信用卡违约预测模型,主要应用在金融相关领域,根据用户以往的行为数据来预测是否会违约,有利于商业银行防范和化解信用卡风险,完善信用卡违约风险管理工作。本次实验通过探索性分析以及使用决策树构建信用卡违约模型,得出以下结论:1.可透支金额主要集中在0~200000之间。2.性别对于可透支金额的影响较小。3.受教育程度和婚姻状况对可透支金额的影响较大。4.决策树模型的准确率为0.88,模型效果还有待提高。

m0_64336780的博客 7579

nodejs应用之中文乱码问题(1)- 使用iconv-lite类库将gbk文件转为utf8

专注js栈技术(web前端、chrome扩展、nodejs、mongodb、快应用、微信小程序),整合多种编程语言(python、bat等),秉承工匠精神,不求做大做强,始终致力于用技术改善日常工作和生活。 一、本文目标 提供简洁可靠的方案,将gbk编码的文本转换为utf8编码,解决可能会出现的中文乱码问题。 二、适合的读者 1.具备基本的nodejs知识,知道如何使用npm下载类库 2.有解...

huazai2019wzqj的博客 2372

【乐企-工具篇】生成发票左上角二维码

【代码】【乐企-业务篇】生成发票左上角二维码。

九七的博客 712

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

nodejs关于十六进制转化为UTF8字符串与GBK字符串方法

variconv=require('iconv-lite'); /**十六进制转UTF8字符串**/ letdata01='48656c6c6f20576f726c64'//十六进制 letnewdata=Buffer.from(data01,'hex');//先把数据存在buf里面 console.log("newdata",newdata); console.log(newdata.toString("utf-8"));//使用toString函数就能转换成字符串...

xiaoqiang137549的专栏 3243

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

nodejs 使用iconv-lite转码GBK

2019独角兽企业重金招聘Python工程师标准>>> ...

weixin_33714884的博客 2722

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

nodejs操作gbk和utf8乱码文件

const fs = require('fs') const iconv = require("iconv-lite") const data = fs.readFileSync("./gbk.html") // console.log(data.toString()); // 默认是乱码 const data2 = iconv.decode(data, "gbk") console.log(data2); // 正常显示 // 再次写入到utf8 fs.writeFileSync("./utf8..

AdleyTales的技术博客 1864

node mysql gbk_nodejs 利用net模块发送gbk编码的内容

nodejs是只支持utf8,utf16,base64等屈指可数的几种编码格式,如果要使用gbk,gb2312等格式的编码需要调用外部的API。我项目中的硬件(语音芯片)只支持gbk,gb2312格式的汉字,所以我只能给硬件发送这种编码格式的字符串。我调用的是iconv-lite第三方API,代码中先require:var iconv = require("iconv-lite");之后从数据库中...

weixin_42356331的博客 263

nodejs 读取文件中文乱码 utf8转gbk解决办法

首先安装iconv-lite模块 npm install iconv-lite // 加载File System读写模块 var fs = require('fs'); // 加载编码转换模块 var iconv = require('iconv-lite'); var file = "F:\\nodeJS\\app\\file\\input.txt"; r...

pergoods的博客 3641

在nodejs中将GBK转UTF

在nodejs语言中通过iconv-lite将GBK数据格式转为UTF格式

半本说明书 1619

Node.js转化GBK编码 - iconv-lite

node当使用node获取GBK编码的数据时,nodejs只支持utf-8,node没有提供转换编码的原生支持,有倒是有一个模块iconv能干这个事,但需要本地方法,VC++库的支持。国外有个大牛写了一个纯粹用Javascript解码的模块:iconv-lite,可以实现编码转换,使用方法如下:var http = require("http"); var iconv = require("icon

zgljl2012的专栏 1万+

Nodejs编码转化问题

目前Node.js仅支持hex、utf8、ascii、binary、base64、ucs2几种编码的转换。对于GBK,GB2312等编码,Nodejs自带的toString()方法不支持,因此中文转化的时候需要加载第三方库,主要有两个编码转换库iconv-lite和encoding,个人推荐使用encoding。 iconv-lite:是 iconv 的纯 js 实现,支持的编码包括 no...

weixin_30642029的博客 983

Nodejs抓取非utf8字符编码的页面

Nodejs抓取非utf8字符编码的页面 Nodejs抓取非utf8字符编码的页面 - MK2 - 博客园Nodejs抓取非utf8字符编码的页面问题: nodejs目前无法处理非utf8编码以外的字符在cnodejs用户组,有同学遇到抓取百度页面出现编码问题由于...

135

php文件改写nodejs,nodejs批量修改文件编码格式_node.js

摘要:最近在制作手册的时候遇到了一个问题'文档乱码',查看文件之后发现文件编码不对,总共100多个文件,如果用编辑器另存为utf8,那就悲催了。所以自己就写了个程序,批量修改文件编码格式。代码:/*** 修改文件编码格式,例如:GBK转UTF8* 支持多级目录* @param {String} [root_path] [需要进行转码的文件路径]* @param {Array} [file_typ...

weixin_28963585的博客 186

Atitit node.js问题解决总结t99 目录 1.1. 找不到node程序 1 1.2. GBK编码问题 1 2. 按行读取gbk编码问题 2 2.1. 先写入txt utf8 encode

Atitit node.js问题解决总结t99 目录 1.1. 找不到node程序 1 1.2. GBK编码问题 1 2. 按行读取gbk编码问题 2 2.1. 先写入txt utf8 encode..then use path mode 2 2.2. NodeJS笔记:处理非utf8编码 2 找不到node程序 { //UseInte...

attilax的专栏 279
上一篇: 用 goproxy.io 自建 go modules 代理,部署到 heroku
下一篇: docker 突然不能被外网访问
whitehack
博客等级 码龄19年 67粉丝 123原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址