[英]Is there an equivalent for C# Enumerable for converting string to byte array in NodeJs?
I'm Sending Hex string to a function and return it as Byte array using c# , but now the requirement is to do this in NodeJs . 我正在将十六进制字符串发送到函数,并使用c#将其作为字节数组返回,但是现在的要求是在NodeJs中执行此操作。
i searched too much about this but non solutions gave me the same result 我对此进行了过多搜索,但非解决方案给了我相同的结果
here is my c# code with this hex string 这是我的十六进制字符串的C#代码
`8001000501335688003300020002000200`
public static byte[] StringToByteArray(string hex)
{
var byteArray = Enumerable.Range(0, hex.Length)
.Where(x => x % 2 == 0)
.Select(x =>Convert.ToByte(hex.Substring(x,2),16))
.ToArray();
return byteArray;
}
i tried this code in NodeJs but i didn't get the same result 我在NodeJs中尝试了此代码,但没有得到相同的结果
function StringToByteArray(hex) {
var rangebytes = range(0, hex.length).filter(x => x % 2 == 0)
var filteredHex = rangebytes.match(x =>
Buffer.from(hex.substring(x, 2), "utf8"));
return filteredHex;
}
function range(start, count) {
return Array.apply(0, Array(count))
.map(function (element, index) {
return index + start;
});
}
And here is the result from c# code http://prntscr.com/m7xnzg 这是C#代码http://prntscr.com/m7xnzg的结果
This function will convert a hex string to a byte array in Node.js: 此函数将十六进制字符串转换为Node.js中的字节数组:
function hexStringToByteArray(hexStr) { let a = []; for(let c = 0; c < hexStr.length; c += 2) { a.push(parseInt(hexStr.substr(c, 2), 16)); } return a; } console.log("Result: ", hexStringToByteArray("8001000501335688003300020002000200"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.