gpt4 book ai didi

Javascript fromCharCode 在 C# 中等效

转载 作者:行者123 更新时间:2023-11-30 20:55:51 26 4
gpt4 key购买 nike

我在 Javascript 中有一个函数,如下所示

// Use this to convert "0123DEF01234" (hex string) to
// binary data based on 0x01 0x23 0xDE 0xF0 0x12 0x34

hex = "0123456789ABCDEFFEDCBA98765432100123456789ABCDEF";

hex = hex.replace(/\s/g,""); // eliminate spaces

var keyar = hex.match(/../g); // break into array of doublets

var s=""; // holder for our return value

for(var i=0;i<keyar.length;i++)
s += String.fromCharCode( Number( "0x" + keyar[i] ) );

return s;

但我找不到这条线的等价物

        s += String.fromCharCode( Number( "0x" + keyar[i] ) );

这行的等价物是什么?

最佳答案

您可以尝试使用 Linq:

using System.Linq;

...

string hex = "0123456789ABCDEFFEDCBA98765432100123456789ABCDEF";

// Eliminating white spaces
hex = string.Concat(hex.Where(c => !char.IsWhiteSpace(c)));

// Let "binary data based on 0x01 0x23 0xDE..." be an array
byte[] result = Enumerable
.Range(0, hex.Length / 2) // we have hex.Length / 2 pairs
.Select(index => Convert.ToByte(hex.Substring(index * 2, 2), 16))
.ToArray();

// Test (let's print out the result array with items in the hex format)
Console.WriteLine(string.Join(" ", result.Select(b => $"0x{b:X2}")));

结果:

0x01 0x23 0x45 0x67 0x89 0xAB 0xCD 0xEF 0xFE 0xDC 0xBA 0x98 0x76 0x54 0x32 0x10 0x01 0x23 0x45 0x67 0x89 0xAB 0xCD 0xEF

关于Javascript fromCharCode 在 C# 中等效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47672527/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com