一個漢字utf-8的字串3個位元組,轉成GB2312是2個位元組,轉成GB2312的字串是4個位元組,
英文字母和數字不管編碼是什么編碼,都是一個位元組,
資料傳輸的時候一般,轉碼后,位元組不夠的話,一般在后面補0
package servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class CustomerServlet extends HttpServlet {
public static void main(String[] args) throws Exception {
String a="行一";
byte[] b=a.getBytes("GB2312");
System.out.println( bytesToHexFun1(b).toUpperCase());
int c= 0 >> 8;
System.out.println(c);
}
public static String bytesToHexFun1(byte[] bytes) {
char[] HEX_CHAR = {'0','1','2','3','4','5',
'6','7','8','9','a','b','c','d','e','f'};
// 一個byte為8位,可用兩個十六進制位標識
char[]buf = new char[bytes.length*2];
int a = 0;
int index = 0;
for(byte b : bytes){// 使用除與取余進行轉換
if(b<0){
a=256+b;
}else{
a=b;
}
buf[index++]=HEX_CHAR[a/16];
buf[index++]=HEX_CHAR[a%16];
}
return new String(buf);
}
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//設定請求的字符集
req.setCharacterEncoding("utf-8");
//設定回應的文本型別
resp.setContentType("text/html;charset=utf-8");
//通過請求物件獲取用戶輸入的內容
String username = req.getParameter("username");
String password = req.getParameter("userpwd");
System.out.println(username+" "+password);
//如果輸入的用戶名是abc,密碼是123,則表示注冊成功,反之注冊失敗
if("abc".equals(username)&&"123".equals(password)){
//使用回應物件,重定向到成功頁面
//resp.sendRedirect("success.html");
//請求轉發
req.getRequestDispatcher("success.html").forward(req, resp);;
}else{
//使用回應物件,重定向到注冊頁面
resp.sendRedirect("register.html");
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/199663.html
標籤:Java
