Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

Java String to ByteBuffer conversion

public static Charset charset = Charset.forName("UTF-8");
public static CharsetEncoder encoder = charset.newEncoder();
public static CharsetDecoder decoder = charset.newDecoder();

public static ByteBuffer str_to_bb(String msg){
  try{
    return encoder.encode(CharBuffer.wrap(msg));
  }catch(Exception e){e.printStackTrace();}
  return null;
}

public static String bb_to_str(ByteBuffer buffer){
  String data = "";
  try{
    int old_position = buffer.position();
    data = decoder.decode(buffer).toString();
    // reset buffer's position to its original so it is not altered:
    buffer.position(old_position);  
  }catch (Exception e){
    e.printStackTrace();
    return "";
  }
  return data;
}
Comment

convert bytebuffer to string

String s = StandardCharsets.UTF_8.decode(byteBuffer).toString();
Comment

PREVIOUS NEXT
Code Example
Java :: open file java 
Java :: fibonacci series in java using recursion 
Java :: java for each 
Java :: declare array with values java 
Java :: java xmx example 
Java :: javafx textarea how to make smaller 
Java :: android recyclerview show hide item with animation with default animation 
Java :: java random unique number 
Java :: how to input in java 
Java :: sorting the characters of a string in java 
Java :: why java platform independent 
Java :: This version of the Android Support plugin for IntelliJ IDEA (or Android Studio) cannot open this project, please retry with version 4.1 or newer. 
Java :: insert data into list java 
Java :: java argument main 
Java :: 2d arraylist in java 
Java :: parse string to int java 
Java :: initialize arraylist 
Java :: how to use input in java 
Java :: bootstrap 4 modal get event related target 
Java :: hide elements android 
Java :: math library java 
Java :: printing prime numbers in java 
Java :: array contains java 
Java :: instantiate optinal java 8 
Java :: how to get data from combobox in java 
Java :: how to use deque as stack or queue in java? 
Java :: method returns arraylist java 
Java :: get the max value from arrayList java and save it in int 
Java :: how to remove duplicate elements from char array in java 
Java :: java replaceAll ignore case 
ADD CONTENT
Topic
Content
Source link
Name
4+9 =