Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Hexadecimal to Binary WITH leading zeros???

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    16
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Hexadecimal to Binary WITH leading zeros???

    How do I convert a hexadecimal number to binary keeping the leading zeros? For the conversion itself I am using:
    String stringName = Integer.toBinaryString(int in 0x format);, however, this truncates the leading zeros rendering the conversion borderline useless. Using String formattedString = String.format("%032d", int in 0x format); works only on an int not on a string. I am sure there must be an obvious solution that I am overlooking as I would hate to write relatively complicated code for such a mundane procedure.

    Thanks in advance.


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: Hexadecimal to Binary WITH leading zeros???

    How do I convert a hexadecimal number to binary keeping the leading zeros? ... Using String formattedString = String.format("%032d", int in 0x format); works only on an int not on a string.
    When I try that I get a decimal string with width 32, not a binary one.

    public class Test {
        public static void main(String[] args) {
            String formattedString = String.format("%032d", 0xCAFE);
                // prints 00000000000000000000000000051966
            System.out.println(formattedString);
        }
    }

    The Formatter API docs reveal that there are octal, decimal and hex numeric formats offered (with zero as a padding character), but no binary (or other base). So it looks like toBinaryString() is the way to go.

    toBinaryString() returns a string and, alas, you get spaces inserted when the string is right aligned to a specified width, not zero (or any other character.)

    It looks like the most straight forward approach - using standard library formatting methods - is to use toBinaryString(), or one of the numerous toString() methods, format this to have the width you want with %32s for example, then replace the spaces with zeros.

  3. The Following User Says Thank You to pbrockway2 For This Useful Post:

    Deprogrammer (February 6th, 2012)

Similar Threads

  1. Decimal to Hexadecimal, Without Java Libraries.
    By ribmant in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 3rd, 2012, 06:28 AM
  2. Binary datafile and object creation according to binary tag ?
    By loicus in forum Object Oriented Programming
    Replies: 4
    Last Post: October 14th, 2011, 01:00 PM
  3. How to convert a String into an Hexadecimal ?
    By lumpy in forum Java Theory & Questions
    Replies: 2
    Last Post: February 16th, 2010, 04:01 PM
  4. Hexadecimal answer rather than decimal
    By Eric in forum Java Theory & Questions
    Replies: 1
    Last Post: June 22nd, 2009, 11:36 AM
  5. Program to convert Hexadecimal to its Character equivalent
    By nathanernest in forum Java Theory & Questions
    Replies: 2
    Last Post: April 8th, 2009, 03:12 AM