gpt4 book ai didi

来自 VB6 的 C# 版本的 Base64 编码

转载 作者:行者123 更新时间:2023-12-02 05:19:22 25 4
gpt4 key购买 nike

我需要使用 Base64 对字符串进行编码。我有一个 VB6 算法,我需要准确地编写一个 C# 版本。我能够 99% 正确,但最后一个字符是错误的,我不确定到底出了什么问题。

这里是要编码的字符串:

¼9ÀPʨ!Œaøc4vøÝFãgïÒ_£¡˜è/[t•Ôý56wBpïcÃ.Cð`

VB6 编码给出以下字符串:

vDnAUMqoFiGMYfhjNHb43UbjZ+/SX6MBoZjoL1t0ldT9NTZ3QnDvYwMTww4uQ/Ag

C#编码给出以下字符串:

vDnAUMqoFiGMYfhjNHb43UbjZ+/SX6MBoZjoL1t0ldT9NTZ3QnDvYwMTww4uQ/Ax

如您所见,除最后一个字母外,其他所有内容均相同。

VB6 算法:

Const sBASE_64_CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"


Function Base64encode(ByVal asContents)
Dim lnPosition
Dim lsResult
Dim Char1
Dim Char2
Dim Char3
Dim Char4
Dim Byte1
Dim Byte2
Dim Byte3
Dim SaveBits1
Dim SaveBits2
Dim lsGroupBinary
Dim lsGroup64

If Len(asContents) Mod 3 > 0 Then asContents = asContents & String(3 - (Len(asContents) Mod 3), " ")
lsResult = ""

For lnPosition = 1 To Len(asContents) Step 3
lsGroup64 = ""
lsGroupBinary = Mid(asContents, lnPosition, 3)

Byte1 = Asc(Mid(lsGroupBinary, 1, 1)): SaveBits1 = Byte1 And 3
Byte2 = Asc(Mid(lsGroupBinary, 2, 1)): SaveBits2 = Byte2 And 15
Byte3 = Asc(Mid(lsGroupBinary, 3, 1))

Char1 = Mid(sBASE_64_CHARACTERS, ((Byte1 And 252) \ 4) + 1, 1)
Char2 = Mid(sBASE_64_CHARACTERS, (((Byte2 And 240) \ 16) Or (SaveBits1 * 16) And &HFF) + 1, 1)
Char3 = Mid(sBASE_64_CHARACTERS, (((Byte3 And 192) \ 64) Or (SaveBits2 * 4) And &HFF) + 1, 1)
Char4 = Mid(sBASE_64_CHARACTERS, (Byte3 And 63) + 1, 1)
lsGroup64 = Char1 & Char2 & Char3 & Char4

lsResult = lsResult + lsGroup64
Next

Base64encode = lsResult
End Function

这是 C# 的等价物:

public const string sBASE_64_CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
public static string Base64encode(string asContents)
{
int lnPosition;
string lsResult;
string Char1;
string Char2;
string Char3;
string Char4;
int Byte1;
int Byte2;
int Byte3;
int SaveBits1;
int SaveBits2;
string lsGroupBinary = null;
string lsGroup64 = null;

if (Microsoft.VisualBasic.Strings.Len(asContents) % 3 > 0)
asContents = asContents + (3 - (Microsoft.VisualBasic.Strings.Len(asContents) % 3));
lsResult = "";

for (lnPosition = 1; lnPosition <= Microsoft.VisualBasic.Strings.Len(asContents); lnPosition += 3)
{
lsGroup64 = "";
lsGroupBinary = Microsoft.VisualBasic.Strings.Mid(asContents, lnPosition, 3);

Byte1 = Microsoft.VisualBasic.Strings.Asc(Microsoft.VisualBasic.Strings.Mid(lsGroupBinary, 1, 1));
SaveBits1 = Byte1 & 3;
Byte2 = Microsoft.VisualBasic.Strings.Asc(Microsoft.VisualBasic.Strings.Mid(lsGroupBinary, 2, 1));
SaveBits2 = Byte2 & 15;
Byte3 = Microsoft.VisualBasic.Strings.Asc(Microsoft.VisualBasic.Strings.Mid(lsGroupBinary, 3, 1));

Char1 = Microsoft.VisualBasic.Strings.Mid(sBASE_64_CHARACTERS, ((Byte1 & 252) / 4) + 1, 1);
Char2 = Microsoft.VisualBasic.Strings.Mid(sBASE_64_CHARACTERS, (((Byte2 & 240) / 16) | (SaveBits1 * 16) & 0xff) + 1, 1);
Char3 = Microsoft.VisualBasic.Strings.Mid(sBASE_64_CHARACTERS, (((Byte3 & 192) / 64) | (SaveBits2 * 4) & 0xff) + 1, 1);
Char4 = Microsoft.VisualBasic.Strings.Mid(sBASE_64_CHARACTERS, (Byte3 & 63) + 1, 1);
lsGroup64 = Char1 + Char2 + Char3 + Char4;

lsResult = lsResult + lsGroup64;
}

return lsResult;
}
}

我做错了什么?

最佳答案

原始代码用空格填充字符串,使长度为 3 的整数倍。翻译后的代码无法正确执行此操作。此行未正确翻译:

If Len(asContents) Mod 3 > 0 Then asContents = asContents & String(3 - (Len(asContents) Mod 3), " ")

你有它作为

if (Microsoft.VisualBasic.Strings.Len(asContents) % 3 > 0)
asContents = asContents + (3 - (Microsoft.VisualBasic.Strings.Len(asContents) % 3));

但应该是

if (Microsoft.VisualBasic.Strings.Len(asContents) % 3 > 0)
asContents = asContents + Microsoft.VisualBasic.Strings.String(3 - (Microsoft.VisualBasic.Strings.Len(asContents) % 3), " ");

您的代码没有附加空格,而是附加了一个数字。

关于来自 VB6 的 C# 版本的 Base64 编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14127180/

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