Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8651943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:11:30+00:00 2026-06-12T14:11:30+00:00

I’m trying to port my C++ code to Java but I’m having a hard

  • 0

I’m trying to port my C++ code to Java but I’m having a hard time. The Java part isn’t working but the C++ part is.

I get:

Exception in thread “main” java.lang.StringIndexOutOfBoundsException:
String index out of range: 6 at
java.lang.AbstractStringBuilder.substring(AbstractStringBuilder.java:870)
at java.lang.StringBuilder.substring(StringBuilder.java:72) at
Foo.Encryption.EncodeB64(Encryption.java:57) at
Foo.Main.main(Main.java:9) Java Result: 1

That line points to: System.out.println(Base64Chars.charAt(BinToDecStr(Binaries.substring(0, 6))));

C++ Code (Works 100% of the time):

#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;
}

Now I tried to translate this to java but it doesn’t work :S.
This is the java code:

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;
    }
}

Any idea what I’m doing wrong in Java?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T14:11:31+00:00Added an answer on June 12, 2026 at 2:11 pm

    substr(0,n) in std::string will return less than n characters if n is bigger than the length of the string. In Java, substring in such a situation will raise an exception. You need to make sure n isn’t longer than the length of the string (something like str.substring(0, Math.min(6, str.length())).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.