0

我正在尝试将我的 C++ 代码移植到 Java,但我遇到了困难。Java 部分不起作用,但 C++ 部分起作用。

我得到:

线程“主”java.lang.StringIndexOutOfBoundsException 中的异常:字符串索引超出范围:6 在 java.lang.AbstractStringBuilder.substring(AbstractStringBuilder.java:870) 在 java.lang.StringBuilder.substring(StringBuilder.java:72) 在Foo.Main.main(Main.java:9) 处的 Foo.Encryption.EncodeB64(Encryption.java:57) Java 结果:1

那条线指向:System.out.println(Base64Chars.charAt(BinToDecStr(Binaries.substring(0, 6))));

C++ 代码(100% 有效):

#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <windows.h>
#include <cmath>

const std::string Base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

std::string DecToBinStr(int Num, int Padding)
{
    int Bin = 0, Pos = 1;
    std::stringstream SS;
    while (Num > 0)
    {
        Bin += (Num % 2) * Pos;
        Num /= 2;
        Pos *= 10;
    }
    SS.fill('0');
    SS.width(Padding);
    SS << Bin;
    return SS.str();
}

int BinToDecStr(std::string BinNumber)
{
    int Dec = 0;
    int Bin = strtol(BinNumber.c_str(), NULL, 10);

    for (int I = 0; Bin > 0; I++)
    {
        if(Bin % 10 == 1)
        {
            Dec += (1 << I);
        }
        Bin /= 10;
    }
    return Dec;
}

std::string EncodeB64X(std::string StringToEncode)
{
    std::string Binaries, Result;
    std::size_t STE_Size = StringToEncode.size();
    if(STE_Size)
    {
        for (std::size_t I = 0; I < STE_Size; I++)
            Binaries += DecToBinStr(int(StringToEncode[I]), 8);

        while(Binaries.size())
        {
            Result += Base64Chars[BinToDecStr(Binaries.substr(0, 6))];
            Binaries.erase(0, 6);
        }
    }
    return Result;
}

std::string DecodeB64X(std::string StringToEncode)
{
    std::string Binaries, Result;
    std::size_t STE_Size = StringToEncode.size();
    if(STE_Size)
    {
        for (std::size_t I = 0; I < STE_Size - 1; I++)
            Binaries += DecToBinStr(Base64Chars.find(StringToEncode[I]), 6);
        Binaries += DecToBinStr(Base64Chars.find(StringToEncode[STE_Size - 1]), 8 - ((STE_Size - 1) * 6) % 8);

        while(Binaries.size())
        {
            Result += char(BinToDecStr(Binaries.substr(0, 8)));
            Binaries.erase(0, 8);
        }
    }
    return Result;
}

int main()
{
    std::string F = EncodeB64X("Just Testing");
    std::cout<<F;
}

现在我试图把它翻译成java,但它不起作用:S。这是java代码:

public class BaseEncoder
{
    private static final String Base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

    boolean IsBase64(byte C) {
        return (Character.isDigit(C) || (C == '+') || (C == '/') || Character.isAlphabetic(C));
    }

    private String PadLeft(String s, int n) {
        StringBuilder SBuff = new StringBuilder();
        for (int I = n - s.length(); I > 0; --I) {
            SBuff.append('0');
        }
        SBuff.append(s);
        return SBuff.toString();
    }

    private int BinToDecStr(String BinNumber) {
        int Dec = 0;
        int Bin = Integer.parseInt(BinNumber);

        for (int I = 0; Bin > 0; ++I) {
            if(Bin % 10 == 1) {
                Dec += (1 << I);
            }
            Bin /= 10;
        }
        return Dec;
    }

    private String DecToBinStr(int Num, int Padding) {
        int Bin = 0, Pos = 1;
        String SS = new String();
        while (Num > 0) {
            Bin += (Num % 2) * Pos;
            Num /= 2;
            Pos *= 10;
        }
        SS = PadLeft(SS, Padding);
        SS += Bin;
        return SS;
    }

    String EncodeB64(String StringToEncode)
    {
        String Result = new String();
        StringBuilder Binaries = new StringBuilder();
        int STE_Size = StringToEncode.length();
        if (STE_Size > 0) {
            for (int I = 0; I < STE_Size; ++I) {
                Binaries.append(DecToBinStr(StringToEncode.charAt(I), 8));
            }

            while(Binaries.length() > 0) {
                System.out.println(Base64Chars.charAt(BinToDecStr(Binaries.substring(0, 6))));
                Result += Base64Chars.charAt(BinToDecStr(Binaries.substring(0, 6)));
                Binaries.delete(0, 6);
            }
        }
        return Result;
    }

    String DecodeB64(String StringToEncode)
    {
        String Result = new String();
        StringBuilder Binaries = new StringBuilder();
        int STE_Size = StringToEncode.length();
        if(STE_Size > 0) {
            for (int I = 0; I < STE_Size - 1; I++) {
                Binaries.append(DecToBinStr(Base64Chars.indexOf(StringToEncode.charAt(I)), 6));
            }
            Binaries.append(DecToBinStr(Base64Chars.indexOf(StringToEncode.charAt(STE_Size - 1)), 8 - ((STE_Size - 1) * 6) % 8));

            while(Binaries.length() > 0) {
                Result += (char)BinToDecStr(Binaries.substring(0, 8));
                Binaries.delete(0, 8);
            }
        }
        return Result;
    }
}

知道我在 Java 中做错了什么吗?

4

1 回答 1

1

substr(0,n)如果大于字符串的长度,instd::string将返回小于字符。在 Java 中,在这种情况下会引发异常。您需要确保不长于字符串的长度(类似于)。nnsubstringnstr.substring(0, Math.min(6, str.length())

于 2012-10-10T03:57:58.450 回答