简体   繁体   English

Base64将Javascript编码为C#

[英]Base64 Encoding Javascript to C#

I am trying to port some Javascript to C# and I'm having a bit of trouble. 我正在尝试将一些Javascript移植到C#,但遇到了一些麻烦。 The javascript I am porting calls this 我要移植的JavaScript将此称为

var binary = out.map(function (c) {
    return String.fromCharCode(c);
}).join("");
return btoa(binary);

out is an array of numbers. out是一个数字数组。 I understand that it is taking the numbers and using fromCharCode to add characters to a string. 我知道这是采用数字并使用fromCharCode将字符添加到字符串中。 At first I wasn't sure if my C# equivalent of btoa was working correctly, but the only characters I'm having issues with are the first 6 or 8. My encoded string outputs the same except for the first few characters. 最初,我不确定btoa的C#等价物是否正常工作,但是我遇到的唯一字符是前6个或8个。我的编码字符串输出的字符与前几个字符相同。

At first in C# I was doing this 起初,我在C#中是这样做的

String binary = "";
foreach(int val in output){
    binary += ((char)val);
}

And then I tried 然后我尝试

foreach(int val in output){
   System.Text.ASCIIEncoding convertor = new System.Text.ASCIIEncoding();
   char o = convertor.GetChars(new byte[] { (byte)val })[0];
   binary += o;
}

Both work fine on the later characters of the String but not the start. 两者都可以在String的后面的字符上正常工作,但不能在开始时工作。 I've researched but I don't know what I'm missing. 我已经研究过,但不知道自己缺少什么。

My array of numbers is as follows: { 10, 135, 3, 10, 182, ....} 我的数字数组如下:{10,135,3,10,182,....}

I know the 10s are newline characters, the 3 is end of text, the 182 is ¶, but what's confusing me is that the 135 should be the double dagger ‡. 我知道10是换行符,3是文本结尾,182是¶,但令我感到困惑的是135应该是双匕首‡。 The Javascript does not show it when I print the string. 当我打印字符串时,Javascript不显示它。

So what ends up happening is when the String is converted to Base64 my string looks like Cj8DCj8CRFF.... while the Javascript String looks like CocDCrYCRFF.... The rest of the strings are the same and the int arrays used are identical. 所以最终发生的是,当字符串转换为Base64时,我的字符串看起来像Cj8DCj8CRFF...。而Javascript字符串看起来像CocDCrYCRFF...。其余的字符串是相同的,并且使用的int数组是相同的。

Any ideas? 有任何想法吗?

It's important to understand that binary data does not always represent valid text in a given encoding, and that some encodings have variable numbers of bytes to represent different characters. 重要的是要理解二进制数据并不总是以给定的编码表示有效文本,并且某些编码具有可变数量的字节来表示不同的字符。 In short: binary data and text are not the same at all, and you can only convert between the two in some cases and by following clear, accurate rules. 简而言之:二进制数据和文本根本不一样,在某些情况下,并且要遵循明确,准确的规则,您只能在两者之间进行转换。 Treating them incorrectly will cause pain. 不正确地治疗它们会引起疼痛。

That said, if you have a list of int s, that are always within the range 0-255, that should become a base64 string, here is a way to do it: 就是说,如果您有一个int列表,它们始终在0-255范围内,则应该成为base64字符串,这是一种处理方法:

var output = new[] { 0, 1, 2, 68, 69, 70, 254, 255 };
var binary = new List<byte>();
foreach(int val in output){
    binary.Add((byte)val);
}
var result = Convert.ToBase64String(binary.ToArray());

If you have text that should be encoded as a base64 string...generally I'd recommend UTF8 encoding , unless you need it to match the JS's implementation. 如果您有应被编码为base64字符串的文本...通常,我建议您使用UTF8 编码 ,除非您需要它来匹配JS的实现。

var str = "Hello, world!";
var result = Convert.ToBase64String(Encoding.UTF8.GetBytes(str));

The encoding that JS uses appears to be the same as casting between byte and char ( char s > 255 are invalid), which isn't one of the standard Encoding s available. JS使用的编码似乎与在bytechar之间进行强制转换( char > 255无效)相同,这不是可用的标准Encoding之一。

Here's how you might combine raw numbers and strings, then convert that to base64. 这是将原始数字和字符串组合在一起,然后将其转换为base64的方法。

checked // ensures that values outside of byte's range do not fail silently
{
    var output = new int[] { 10, 135, 3, 10, 182 };
    var binary = output.Select(x => (byte)x)
           .Concat("Hello, world".Select(c => (byte)c)).ToArray();

    var result = Convert.ToBase64String(binary);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM