java code for integer to binay

Download java code for integer to binay

If you can't read please download the document

Upload: v-yaswanth-kumar

Post on 22-Nov-2015

11 views

Category:

Documents


2 download

DESCRIPTION

conversions for int to binary ,hex and octal

TRANSCRIPT

int x = 100;System.out.println(Integer.toBinaryString(x));///The following example shows the usage of java.lang.Integer.toBinaryString() method.package com.tutorialspoint;import java.lang.*;public class IntegerDemo { public static void main(String[] args) { int i = 170; System.out.println("Number = " + i); /* returns the string representation of the unsigned integer value represented by the argument in binary (base 2) */ System.out.println("Binary is " + Integer.toBinaryString(i)); // returns the number of one-bits System.out.println("Number of one bits = " + Integer.bitCount(i)); } } /////public Integer calculateHash(String uuid) { try { MessageDigest digest = MessageDigest.getInstance("SHA1"); digest.update(uuid.getBytes()); byte[] output = digest.digest(); String hex = hexToString(output); Integer i = Integer.parseInt(hex,16); return i; } catch (NoSuchAlgorithmException e) { System.out.println("SHA1 not implemented in this system"); } return null;} private String hexToString(byte[] output) { char hexDigit[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; StringBuffer buf = new StringBuffer(); for (int j = 0; j < output.length; j++) { buf.append(hexDigit[(output[j] >> 4) & 0x0f]); buf.append(hexDigit[output[j] & 0x0f]); } return buf.toString();}//////public class MainClass{ public static void main(String[] arg){ System.out.println(Integer.toHexString(10)); System.out.println(Integer.toHexString(20)); System.out.println(Integer.toHexString(30)); System.out.println(Integer.toHexString(40)); }}////public class MainClass { public static void main(String[] arg) { System.out.println(Integer.toBinaryString(100)); }}////public class MainClass { public static void main(String[] arg) { System.out.println(Integer.toOctalString(10)); }}///import java.io.*;import java.lang.*;public class HexaToInteger{ public static void main(String[] args) throws IOException{ BufferedReader read = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the hexadecimal value:!"); String s = read.readLine(); int i = Integer.valueOf(s, 16).intValue(); System.out.println("Integer:=" + i); }}///