将我的C ++ Base64代码翻译为Java无法正常工作

时间:2012-10-10 03:49:40

标签: java c++

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

我明白了:

  

线程“main”中的异常java.lang.StringIndexOutOfBoundsException:   字符串索引超出范围:6 at   java.lang.AbstractStringBuilder.substring(AbstractStringBuilder.java:870)     在java.lang.StringBuilder.substring(StringBuilder.java:72)at   Foo.Encryption.EncodeB64(Encryption.java:57)at   Foo.Main.main(Main.java:9)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中做错了吗?

1 个答案:

答案 0 :(得分:1)

如果substr(0,n)大于字符串的长度,std::string中的

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