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

  • SEARCH
  • Home
  • 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 8349455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:00:10+00:00 2026-06-09T08:00:10+00:00

I am writing a method where I am converting int values into binary strings

  • 0

I am writing a method where I am converting int values into binary strings and storing them. I am using the method Integer.toBinaryString to do so, and it’s working correctly, but the problem is that I need the method to return exactly 4 bits in the string instead of less (it will never be more because the numbers are not large enough). Here is an example of my code and where the problem is occurring:

int value5 = 3;
String strValue5 = Integer.toBinaryString(value5);
for(int index = 0; index < 4; index++){
     sBoxPostPass[4][index] = strVal5.charAt(index);
}

Clearly, this will throw an ArrayOutOfBoundsException because strValue5 == 11 and not 0011, like it needs to be. I hope this is sufficiently clear. Thanks in advance for the help.

  • 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-09T08:00:14+00:00Added an answer on June 9, 2026 at 8:00 am

    If your value will always have exactly 4 bits then that’s small enough to use a look-up table for the 16 values in question. Correcting any errors in my Java is left as an exercise for the reader.

    static String binary4[16] = {"0000", /* another exercise for the reader */, "1111"};
    static String toBinary4(int value) {
        return binary4[value & 0xF];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using StreamWriter to write log file. And this is the writing log method,
I am using on the client C# where I am converting double values to
I've been working on converting a C++ crypting method to C#. The problem is,
I have an async method I am converting to a sync method using a
I have a doubt about orkut's new URL Writing method. Earlier the home page
I'm writing a method which approximates the Kolmogorov complexity of a String by following
I'm writing a method that will resize the columns in a datagridview so that
I'm writing a method to wrap another method as part of a class. It
Validating Primitive Arguments and Complex Data Validating Arguments When writing a method, arguments should
I wonder if there is a way of writing a method or a class

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.