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 6 of 6

Thread: hex to binary - how do i flush off the leading 0

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Post hex to binary - how do i flush off the leading 0

    any idea how do i flush off the leading 0s from my results? I want to start with 1.


    import java.util.Scanner; 
     
    public class Hex2Bin {
    	public static void main(String[] args) {
    		String hexStr; 
    		int hexStrLen; 
     
    		String[] binStrs = {"0000", "0001", "0010", "0011", "0100", "0101", 
    							"0110", "0111", "1000", "1001", "1010", "1011", 
    							"1100", "1101", "1110", "1111"};
     
    		Scanner in = new Scanner(System.in); 
    		System.out.print("Enter a Hexadecimal string: "); 
    		hexStr = in.next(); 
    		hexStrLen = hexStr.length(); 
    		System.out.print("The binary equivalent is: "); 
     
    		for (int pos = 0; pos <hexStrLen; ++pos){ 
    			char hexChar = hexStr.charAt(pos); 
     
    			if (hexChar >= '0' && hexChar <='9') {
    				System.out.print(binStrs[hexChar-'0']); 
    			}else if (hexChar >= 'a' && hexChar <='f'){
    				System.out.print(binStrs[hexChar-'a'+10]);
    		    }else if (hexChar >= 'A' && hexChar <= 'F'){
    		    	System.out.print(binStrs[hexChar-'A'+10]);
    		    }else{ 
    		    	System.out.println("error: invalid hexadecimal number");
    		    	return; 
    			}
    		}
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: hex to binary - how do i flush off the leading 0

    If you have a String: "0001" you could use some String methods to find the first "1" and another String method to get the contents of the String from that location to the end of the String.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: hex to binary - how do i flush off the leading 0

    Quote Originally Posted by Norm View Post
    If you have a String: "0001" you could use some String methods to find the first "1" and another String method to get the contents of the String from that location to the end of the String.
    I understand that i need to use String's indexOf() and substring(). But just how am i gonna insert into my code

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: hex to binary - how do i flush off the leading 0

    Change the code to build a String (vs printing the parts)
    use those two methods to strip the leading "0"s off the String
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2014
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: hex to binary - how do i flush off the leading 0

    i think there is a way to do this without a string.

    look into Bitwise and Bit Shift Operators (The Java™ Tutorials > Learning the Java Language > Language Basics)

    bitshifting may resolve your problem. i dont remember how my prof did in the pass

  6. #6
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: hex to binary - how do i flush off the leading 0

    i think there is a way to do this without a string.
    I think that is only applicable numbers. How can you use Bit Shift in String?

    I understand that i need to use String's indexOf() and substring(). But just how am i gonna insert into my code
    just get the indexOf() to get the first index of 1 in your string.
    then use substring() to get what you want, get the string from indexOf(1) up to the original length of the string
    Last edited by dicdic; March 5th, 2014 at 07:58 PM.

  7. The Following User Says Thank You to dicdic For This Useful Post:

    kennylty (March 23rd, 2014)

Similar Threads

  1. Replies: 0
    Last Post: November 12th, 2013, 04:57 AM
  2. how to flush the keyboard buffer in java?
    By zara-t in forum Java Theory & Questions
    Replies: 3
    Last Post: June 28th, 2013, 02:45 AM
  3. Replies: 1
    Last Post: May 13th, 2013, 05:09 PM
  4. flush()
    By Purple01 in forum Java Theory & Questions
    Replies: 3
    Last Post: October 11th, 2012, 08:47 AM
  5. Hexadecimal to Binary WITH leading zeros???
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 6th, 2012, 09:27 PM