Home » Core Java » lang » String (page 4)

String

Convert String to byte array ASCII encoding

This is an example of how to convert a String to byte array with ASCII encoding. The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class. Converting a String to byte array with ASCII encoding implies that you should: Create a String. Create a byte array with size ...

Read More »

Convert String to byte array UTF encoding

public static byte[] stringToBytesUTFCustom(String str) { byte[] b = new byte[str.length() << 1]; for(int i = 0; i < str.length(); i++) { char strChar = str.charAt(i); int bpos = i << 1; b[bpos] = (byte) ((strChar&0xFF00)>>8); b[bpos + 1] = (byte) (strChar&0x00FF); } return b; } public static String bytesToStringUTFCustom(byte[] bytes) { char[] buffer = new char[bytes.length >> 1]; for(int ...

Read More »