在保留字节的同时在JavaScript中连接十六进制字节和字符串 [英] Concatenating hex bytes and strings in JavaScript while preserving bytes

查看:103
本文介绍了在保留字节的同时在JavaScript中连接十六进制字节和字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  var hexValue = 0x89; 

我想用JavaScript连接一个十六进制值和一个字符串。
var png =PNG;

字符串PNG相当于 0x50 0x4E 0x47



连接 hexValue png 通过

  var concatHex = String.fromCharCode(0x89)+ String.fromCharCode(0x50)
+ String.fromCharCode(0x4E)+ String.fromCharCode(0x47);

...给出一个字节数为5的结果,因为第一个十六进制值需要控制字符:

  C2 89 50 4E 47 

我正在处理原始图像数据,其中包含 hexValue png 以及需要连接它们而不包含这个控制字符。


  1. 有没有办法修剪控制字符?

  2. 给定我有一个字节数组,有没有更好的方法来连接它们和一个字符串,同时保留字节?


解决方案

好的,我正在调查,我发现在JavaScript中实现这个eficienly JavaScript类型的数组被使用。



https: //developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays



http://msdn.microsoft.com/en-us/library/br212485(v = vs.94) .aspx



这里我写了一个代码(未测试)来执行你想要的操作:

  var png =PNG; 
var hexValue = 0x89;
var lenInBytes =(png.Length + 1)* 8; //留下一个额外的空间来连接hexValue

var buffer = new ArrayBuffer(lenInBytes); //创建大块内存,其字节全部预初始化为0
var int8View = new Int8Array(buffer); //把这个内存当作int8

来处理(int i = 0; i< png.Length; i ++)
int8View [i] = png [i] //这里转换png [i]到字节

//此时,我们将字符串png作为字节数组

int8View [png.Length] = hexValue //在这里执行并置

好希望它有帮助。


I would like to concatenate a hex value and a string using JavaScript.

var hexValue = 0x89;
var png = "PNG";

The string "PNG" is equivalent to the concatenation of 0x50, 0x4E, and 0x47.

Concatenating hexValue and png via

var concatHex = String.fromCharCode(0x89) + String.fromCharCode(0x50) 
              + String.fromCharCode(0x4E) + String.fromCharCode(0x47);

...give a result with a byte count of 5 because of the first hex value needing a control character:

C2 89 50 4E 47

I am working with raw image data where I have hexValue and png and need to concatenate them without this control character being included.

  1. Is there a way to trim off the control character?
  2. Given I have an array of bytes, is there a better way to concatenate them and a string while preserving the bytes?

解决方案

Well i was investigating and i found that in javascript to achieve this eficienly JavaScript typed arrays is used.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays

http://msdn.microsoft.com/en-us/library/br212485(v=vs.94).aspx

Here i wrote a code (not tested) to perform what you want:

var png = "PNG";
var hexValue = 0x89;
var lenInBytes = (png.Length + 1) * 8; //left an extra space to concat hexValue

var buffer = new ArrayBuffer(lenInBytes); //create chunk of memory whose bytes are all pre-initialized to 0
var int8View = new Int8Array(buffer); //treat this memory like int8

for(int i = 0; i < png.Length ; i++)
    int8View[i] = png[i]  //here convert the png[i] to bytes

//at this point we have the string png as array of bytes

    int8View[png.Length] = hexValue //here the concatenation is performed

Well hope it helps.

这篇关于在保留字节的同时在JavaScript中连接十六进制字节和字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆