Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

Printing Hexadecimal Code

public class PrintHexCode {
 
   public static void main(String[] args) {
      int i = 12345;
      System.out.println("Decimal is " + i);                        // 12345
      System.out.println("Hex is " + Integer.toHexString(i));       // 3039
      System.out.println("Binary is " + Integer.toBinaryString(i)); // 11000000111001
      System.out.println("Octal is " + Integer.toOctalString(i));   // 30071
      System.out.printf("Hex is %x
", i);    // 3039
      System.out.printf("Octal is %o
", i);  // 30071
 
      char c = 'a';
      System.out.println("Character is " + c);        // a
      System.out.printf("Character is %c
", c);      // a
      System.out.printf("Hex is %x
", (short)c);     // 61
      System.out.printf("Decimal is %d
", (short)c); // 97
 
      float f = 3.5f;
      System.out.println("Decimal is " + f);    // 3.5
      System.out.println(Float.toHexString(f)); // 0x1.cp1 (Fraction=1.c, Exponent=1)
 
      f = -0.75f;
      System.out.println("Decimal is " + f);    // -0.75
      System.out.println(Float.toHexString(f)); // -0x1.8p-1 (F=-1.8, E=-1)
 
      double d = 11.22;
      System.out.println("Decimal is " + d);     // 11.22
      System.out.println(Double.toHexString(d)); // 0x1.670a3d70a3d71p3 (F=1.670a3d70a3d71 E=3)
   }
}
Source by www3.ntu.edu.sg #
 
PREVIOUS NEXT
Tagged: #Printing #Hexadecimal #Code
ADD COMMENT
Topic
Name
4+4 =