Showing posts with label conversion. Show all posts
Showing posts with label conversion. Show all posts

Tuesday, 14 June 2011

How to convert map to List in java?

Assuming map is your instance of Map :
map.values() will return a Collection containing all of the map's values
map.keys() will return a Set containing all of the map's keys
map.entrySet() will return a Set containing all map key value pairs.

Get list of keys

List<String> list = new ArrayList<String>(map.keySet());

Get list of values

List<String> list = new ArrayList<String>(map.values());

Thursday, 31 March 2011

Conversion from decimal to binary

You can convert a decimal to binary using toBinaryString() method of Integer wrapper class as follows.

int i = 42;
String binstr = Integer.toBinaryString(i);

Conversion from integer to hexadecimal and vice-versa

You can convert a hexadecimal to integer using two different methods as shown below:
int i = Integer.valueOf("B8DA3", 16).intValue();

   or

int i = Integer.parseInt("B8DA3", 16);  


You can convert a decimal to binary using three different methods as shown below:

int i = 42;
String hexstr = Integer.toString(i, 16);

or

String hexstr = Integer.toHexString(i);

or (with leading zeroes and uppercase)

public class Hex {
public static void main(String args[]){
int i = 42;
System.out.print
(Integer.toHexString( 0x10000 | i).substring(1).toUpperCase());
}
}

Sunday, 20 March 2011

Convert String to byte array

This example shows how to do it:

String stringToConvert = "This String is 76 characters long and will be converted to an array of bytes";

byte[] theByteArray = stringToConvert.getBytes();

Convert from byte array to string

This the function to convert byte array to string:

//eg. byte[] byteArray = new byte[] {87, 79, 87, 46, 46, 46};
public static String convertByteArrayToString(byte[] byteArray) {

String value = new String(byteArray);

return value;
}

Monday, 2 August 2010

Converting an integer to binary string - Binary.java


/*************************************************************************
* Compilation: javac Binary.java
* Execution: java Binary n
*
* Prints out n in binary.
*
* % java Binary 5
* 101
*
* % java Binary 106
* 1101010
*
* % java Binary 0
* 0
*
* % java Binary 16
* 10000
*
* Limitations
* -----------
* Does not handle negative integers.
*
* Remarks
* -------
* could use Integer.toBinaryString(N) instead.
*
*************************************************************************/

public class Binary {
public static void main(String[] args) {

// read in the command-line argument
int n = Integer.parseInt(args[0]);

// set v to the largest power of two that is <= n
int v = 1;
while (v <= n/2) {
v = v * 2;
}

// check for presence of powers of 2 in n, from largest to smallest
while (v > 0) {

// v is not present in n
if (n < v) {
System.out.print(0);
}

// v is present in n, so remove v from n
else {
System.out.print(1);
n = n - v;
}

// next smallest power of 2
v = v / 2;
}

System.out.println();

}

}

Though written a program, we can use inbuilt lib of java - See here.