UTF-8 to GB2312

Time:2021-1-13

The string of a Chinese character UTF-8 is three bytes, and it is two bytes when it is converted to GB2312, and four bytes when it is converted to GB2312.

English letters and numbers, no matter what the code is, are a byte.

When data transmission, generally, after transcoding, if the byte is not enough, it is usually supplemented with 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 = line one;
        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'};
            //A byte is 8 bits, which can be identified by two hexadecimal bits
                char[]buf = new char[bytes.length*2];
                int a = 0;
                int index = 0;
                For (byte B: bytes) {// convert by division and remainder
                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 {
        //Set the requested character set
        req.setCharacterEncoding("utf-8");
        //Sets the text type of the response
        resp.setContentType("text/html;charset=utf-8");
        //Get the user input through the request object
        String username = req.getParameter("username");
        String password = req.getParameter("userpwd");

        System.out.println(username+" "+password);
        //If the user name is ABC and the password is 123, it means that the registration is successful. Otherwise, the registration fails
        if("abc".equals(username)&&"123".equals(password)){
            //Use the response object to redirect to the success page
            //resp.sendRedirect("success.html");
            //Request forwarding
            req.getRequestDispatcher("success.html").forward(req, resp);;
        }else{
            //Use the response object to redirect to the registration page
            resp.sendRedirect("register.html");
        }
    }

Recommended Today

Summary of recent use of gin

Recently, a new project is developed by using gin. Some problems are encountered in the process. To sum up, as a note, I hope it can help you. Cross domain problems Middleware: func Cors() gin.HandlerFunc { return func(c *gin.Context) { //Here you can use * or the domain name you specify c.Header(“Access-Control-Allow-Origin”, “*”) //Allow header […]