Categories

JAVA DATEBASE
Technology Network Community
Oracle Database
Fusion Middleware
Development Tools
Java
Desktop
Server & Storage Systems
Enterprise Management
Berkeley DB Family
Cloud Computing
Big Data
Business Intelligence
Architecture
Migration and Modernization
E-Business Suite
Siebel
PeopleSoft Enterprise
JD Edwards World
Industries
JD Edwards EnterpriseOne
User Productivity Kit Pro (UPK) and Tutor
Governance, Risk & Compliance (GRC)
Master Data Management (MDM)
Oracle CRM On Demand
On Demand: SaaS and Managed Applications
AutoVue Enterprise Visualization
Primavera
ATG
Agile PLM
Endeca Experience Management
Fusion Applications
Archived Forums

 



Tags

New To Java


Integer.toBinaryString() I need Bloody Leading Zeros


Hello All,  I am doing the following:  public String stringToBinary(String input) {                      int intRepresentation = Integer.parseInt(input, 16);                      String binaryString = Integer.toBinaryString(intRepresentation);                      return(binaryString);  }   I need the leading zeros though for my project, they are crucial. For example for the input:  '03' I need the function to return '00000011' not '11' like it does currently.  Another example, if input was '36' then I need it to return '0011 0110'.  Thanks guys!

You'll have to add them yourself.

This becomes tough though:  How do I check if I have '1' in hex then I need to add 3 leading zeros to the output of the function.  But if I have '2' in hex then I need to add  2 leading zeros to the output of of the function.  Finally if I have for example 'FF' I dont need to add any.   You Can see there are many cases, what is the way to approach this?

If you want a string of length length from the number number:  - Get the binary string using the method you already use. - Get the length of that string, call it stringLength - Prepend (stringLength-length) '0'-Characters to the string.  Done.

public String stringToBinary(String input) {     int intRepresentation = Integer.parseInt(input, 16);     String binaryString = "0000000"+Integer.toBinaryString(intRepresentation);     return binaryString; // change this to only return the last 8 chars [substring]  } 

Ok, I like your solution tschodt, though it is kinda of a dirty hack, but it should work.  How do I refrence from the end of the string using substring? I know how to reference forward substring(0,5) would be characters 1 -5 but how do I refrence a strings last character and work my way back 8 characters?

Acuatually will this work?  int sizeOf = binaryString.length(); return(binaryString.subString(sizeOf - 8, sizeof);


Related Links

Java Game that is Perfect for new Java Students
listener for JFormattedTextField
some one please help i need this done as qwick as possible
Project does not produce all results (Arraylist)
is there a 1 unique listener for all actions on table?
Applet tag
How to kill a application
deploying in TOMCAT?
Quick enum question
Mutator Methods
Difference between 'new Integer(value)' and Integer.valueOf(value)
Sharing variable among different file
Running DOS commands in Java
Why compiler says illegal start of experssion?
error message
Whts the use of Wrapper classes