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 8310187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:22:19+00:00 2026-06-08T19:22:19+00:00

Hi everyone I have implemented a solution to convert a value from binary to

  • 0

Hi everyone I have implemented a solution to convert a value from binary to hexidecimal. So I was wondering if there could be more elegant solution(pretty sure there is) than mine. I have tested the program and it is working. Here is the code:

public class BinaryToHex
{
    public static void main(String[] args) 
    {
        String binary = "1110001101";
        binaryToHex(binary);
        //38d
    }

    public static void binaryToHex(String binaryValue)
    {
        StringBuilder sb = new StringBuilder(binaryValue);

        System.out.println("Original StringBuilder: " + sb);
        sb.reverse();

        System.out.println("reversed StringBuilder: " + sb);

        int convert = binaryValue.length();
        System.out.println("Legth of the binary: " +  convert);

        if(convert % 4 != 0)
        {
            while(convert % 4 != 0)
            {


                sb.append(0);
                convert ++;

                System.out.println("StringBuilder in loop: " + sb);
                System.out.println("Convert in loop: " + convert);
            }
        }
        sb.reverse();
        System.out.println("Ready StringBuilder for use? " + sb);
        String test = null;
//      String test = sb.toString();
        for(int i=0; i<sb.length(); i ++)
        {
            if(i % 4 == 0)
            {
                test = sb.substring(i, (i+4));
                System.out.print(getChar(test));
            }
        }
    }

    public static String getChar(String num)
    {
        String number = "";
        switch(num)
        {
            case "0000" : number = "0"; break;
            case "0001" : number = "1"; break;
            case "0010" : number = "2"; break;
            case "0011" : number = "3"; break;
            case "0100" : number = "4"; break;
            case "0101" : number = "5"; break;
            case "0110" : number = "6"; break;
            case "0111" : number = "7"; break;
            case "1000" : number = "8"; break;
            case "1001" : number = "9"; break;
            case "1010" : number = "A"; break;
            case "1011" : number = "B"; break;
            case "1100" : number = "C"; break;
            case "1101" : number = "D"; break;
            case "1110" : number = "E"; break;
            case "1111" : number = "F"; break;

        }
        return number;
    }

}
  • 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-08T19:22:20+00:00Added an answer on June 8, 2026 at 7:22 pm

    It was an funny exercise to do things we take for granted in a library implementation.
    You can improve you algo a lot by using counters to track where you are and prevent the reverse operations. Also the switch block is rather un-elegant.

    Here’s my go at it. Not claiming it’s highly performant, but I think it’s an improvement on your proposal.

    public class BinToHex {
    
        static char[] HEX = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
        static int[] POW_2 = {1,2,4,8};
        public static String toHex(String bin) {
            int len = bin.length();
            char[] result = new char[(int)Math.ceil(len*1D/4)];
            int pos = result.length-1;
            int hexval = 0;
            for (int i=0; i<len;i++ ){
                hexval += (bin.charAt(len-i-1)-HEX[0]) * POW_2[i%4];
                if ((i+1)%4==0) {
                    result[pos--] = HEX[hexval];
                    hexval = 0;
                }
            }
            if (pos==0) {
                result[0] = HEX[hexval];
            }
            return new String(result);
        }
    
        public static void main(String [] param) {
            System.out.println(BinToHex.toHex("1")); // border case 1 char => 0x1
            System.out.println(BinToHex.toHex("101")); // testcase less than 4 chars => 0x5
            System.out.println(BinToHex.toHex("1011")); // testcase eq 4 chars => 0xB
            System.out.println(BinToHex.toHex("101101011011")); // testcase lenght % 4  = 0
            System.out.println(BinToHex.toHex("11101101011011")); // testcase lenght % 4  != 0
            System.out.println(BinToHex.toHex("000101101011011")); // testcase leading 0
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There have been a lot of discussion around this and everyone tend to agree
Hello everyone I have a problem. I want to do a GridView with a
Have a nice day everyone, I have something to ask your hel, to better
I have a central git repository that everyone pushes to for testing and integration,
everyone! I have sprite moving by action, what have health bar (progress bar). When
Hi everyone my problem today is I have a couple of buttons that slide
Hello everyone Masters Of Web Delevopment :) I have a piece of PHP script
everyone. I have some labels that I draw them in the xib file, and
I have those code snippet that checks the fans table and gets everyone who
H everyone, I have the following: $html = '<ul class=pagination-all>'; $html .= '<li class=pagination-start><div

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.