UTF-8 to GB2312


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;

    public static String bytesToHexFun1(byte[] bytes) {
               char[] HEX_CHAR = {'0','1','2','3','4','5',
            //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
                return new String(buf);
    private static final long serialVersionUID = 1L;
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //Set the requested character set
        //Sets the text type of the response
        //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
            //Use the response object to redirect to the success page
            //Request forwarding
            req.getRequestDispatcher("success.html").forward(req, resp);;
            //Use the response object to redirect to the registration page

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 […]