Convert long to binary, hex and octal
In this chapter you will learn:
Convert long value to binary, hex and octal format strings
A long
type value can have binary hexadecimal and octal format. To convert long
value in different format we can use the following methods.
static String toBinaryString(long i)
returns a string representation in base 2.static String toHexString(long i)
returns a string representation in base 16.static String toOctalString(long i)
returns a string representation in base 8.
It is simple to convert long value to its binary, hexadecimal and octal forms:
public class Main {
public static void main(String[] args) {
System.out.println(Long.toBinaryString(10));
System.out.println(Long.toHexString(15));
System.out.println(Long.toOctalString(10));
}/*j a v a 2 s .c o m*/
}
The output:
Next chapter...
What you will learn in the next chapter:
Home » Java Tutorial » Primitive Data Types