How To Convert String To Byte Array In Java ?
The getBytes() method can be used to convert a String into byte array. An Example is given below:
String secret = "The Gold Is In Room 345";
byte[] secretInBytes=secret.getBytes();
How To Convert Byte array To String In Java?
To convert the string back to byte array use new String(byteArrayName)
byte[] arr ; ----> Suppose 'arr' is a byte array
String str=new String(a); -------> Converts the byte array 'arr' to String
Saturday, June 1, 2013
Java Program To Convert Image To Byte Array And Vice Versa
Java Program To Convert Image To Byte Array And Vice Versa
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import java.nio.file.Files;
class ByteToImage{
public static void main (String[] a)throws IOException{
// Convert Image To Byte Array
File fi = new File("rainforest.jpg");
byte[] imageInBytes = Files.readAllBytes(fi.toPath());
//Convert Byte Array To Image
InputStream in = new ByteArrayInputStream(imageInBytes);
BufferedImage b = ImageIO.read(in);
ImageIO.write(b, "jpg", new File("rainforest1.jpg"));
}
}
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
import java.nio.file.Files;
class ByteToImage{
public static void main (String[] a)throws IOException{
// Convert Image To Byte Array
File fi = new File("rainforest.jpg");
byte[] imageInBytes = Files.readAllBytes(fi.toPath());
//Convert Byte Array To Image
InputStream in = new ByteArrayInputStream(imageInBytes);
BufferedImage b = ImageIO.read(in);
ImageIO.write(b, "jpg", new File("rainforest1.jpg"));
}
}
Subscribe to:
Posts (Atom)