「Java」getBytes()で文字列をバイト列に返すサンプル

2021年7月7日

書式
public byte[] getBytes(String charsetName)throws UnsupportedEncodingException
指定された文字セットを使用してこのStringをバイト・シーケンスにエンコードし、結果を新規バイト配列に格納します。
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException{
String ptVal = request.getParameter("username");
try {
byte[] byteData = ptVal.getBytes("UTF-8");
}catch(UnsupportedEncodingException e){
System.out.println(e);
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{ String ptVal = request.getParameter("username"); try { byte[] byteData = ptVal.getBytes("UTF-8"); }catch(UnsupportedEncodingException e){ System.out.println(e); } }
public void doGet(HttpServletRequest request, HttpServletResponse response)
  throws IOException, ServletException{

  String ptVal = request.getParameter("username");

  try {
    byte[] byteData = ptVal.getBytes("UTF-8");
  }catch(UnsupportedEncodingException e){
    System.out.println(e);
  }
}

 

Java

Posted by arkgame