[Java Basics] Decimal to Hexadecimal

1. Convert the digital serial number from decimal to hexadecimal

/**
 * 10进制转16进制 长度为自定义,满足不同的需求, 0填充在左侧
 * @param serialNum 需要被转换的数字
 * @param length 需要转换成的长度
 * @return 左侧为0的自定义长度的16进制
 * @author hjm
 * @date 2023-05-16
 */
public static String toHex(int serialNum, int length) {
    
    

	// String.format("%016x", 1) 将10进制的1转成16进制,不足的以0补上,16位,结果00000000000000001 
	// String.format("%04x", 1) 将10进制的1转成16进制,不足的以0补上,4位,结果0001 
    return String.format("%0" + length + "x", serialNum);
}

2. Convert hexadecimal to decimal

/**
 * 16进制转10进制
 * @param serialNum 需要被转换的16进制
 * @return 10进制
 * @author hjm
 * @date 2023-05-16
 */
public static String hexToInteger(String serialNum) {
    
    
	// 这里是调用 hutool 中的16进制工具类转换,类似的转换还有很多,详情可查看hutool 文档或者 hutool 源码
    return HexUtil.hexToInt(serialNum);
}

3. Description of String.format()

3.1 Two overloaded methods of String.format()

  • format(String format, Object...args): The new string uses the locale, specifies the string format and parameters to generate a formatted new string.
  • format(Locale locale, String format, Object... args): Use the specified locale, specify the string format and parameters to generate a formatted string.

3.2 Placeholder Types

conversion character conversion character example description
%s string type “Worthy of praise”
%c character type ‘h’
%b Boolean type true
%d integer type (decimal) 88
%x integer type (hexadecimal) FF
%o integer type (octal) 77
%f floating point type 7.777
%a hexadecimal floating point type FF.35AE
%e index type 9.38e+5
%g Generic floating-point type (the shorter of f and e types) basically useless
%h hash code basically useless
%d% percentage type %(%special characters%% can be displayed%)
%n line break basically useless
%tx date and time type basically useless

The sample code is as follows:

public static void main(String[] args) {
    
    
        String str;
        // %s
        str = String.format("Hi,%s", "Jack");
        // hi,Jack
        System.out.println(str);
        // %c   %n
        str = String.format("字母d的大写是:%c %n", 'D');
        // 字母d的大写是:D
        System.out.println(str);
        // %b
        str = String.format("布尔结果是:%b", 3 > 1);
        // 布尔结果是:true
        System.out.println(str);
        // %d
        str = String.format("50的一半是:%d", 50/2);
        // 50的一半是:25
        System.out.println(str);
        // %x
        str = String.format("100的16进制数是:%x", 100);
        // 100的16进制数是:64
        System.out.println(str);
        // %o
        str = String.format("100的8进制数是:%o", 100);
        // 100的8进制数是:144
        System.out.println(str);
        // %f
        str = String.format("50元的书打6.5折是:%f 元", 50 * 0.65);
        // 50元的书打6.5折是:32.500000 元
        System.out.println(str);
        // %a
        str = String.format("上面打折后价格的16进制数是:%a", 50 * 0.65);
        // 上面打折后价格的16进制数是:0x1.04p5
        System.out.println(str);
        // %e
        str = String.format("上面打折后价格的指数表示是:%e", 50 * 0.65);
        // 上面打折后价格的指数表示是:3.250000e+01
        System.out.println(str);
        // %g
        str = String.format("上面价格的指数和浮点数结果的长度较短的是:%g", 50 * 0.65);
        // 上面价格的指数和浮点数结果的长度较短的是:32.5000
        System.out.println(str);
        // %d%
        str = String.format("上面的折扣是:%d%%", 85);
        // 上面的折扣是:65%
        System.out.println(str);
        // %h
        str = String.format("字母A的散列码是:%h",'A');
        // 字母A的散列码是:41
        System.out.println(str);
    }

3.3 Common logos

logo illustrate
+ Add sign to positive or negative numbers
0 Add 0 where there are not enough digits
space Fill in spaces where there are not enough digits
Group numbers, separated by three digits, can only be used in decimal
( Use parentheses to enclose negative numbers with minus signs removed
# Add OX to hexadecimal numbers and o to octal numbers; the use of auxiliary %x and %o is equivalent to a supplementary explanation prompt for digital bases
< Formats the arguments described by the previous converter lock
- Align to the left, fill in spaces where there are not enough digits

The sample code is as follows:

public static void main(String[] args) {
    
    
        // +
        String str;
        str = String.format("数字的正负表示:%+d %d %+d %d", 6, 6, -6, -6);
        // 数字的正负表示:+6 6 -6 -6
        System.out.println(str);
        // -
        str = String.format("左对齐:%-6d", 6);
        // 左对齐:6
        System.out.println(str);
        // 0
        str = String.format("缺位补零:%06d", 6);
        // 缺位补零:000006
        System.out.println(str);
        // 空格
        str = String.format("缺位补空格:% 6d", 6);
        // 缺位补空格:     6
        System.out.println(str);
        str = String.format("缺位补空格:% 6d", -6);
        // 缺位补空格:    -6
        System.out.println(str);
        // ,
        str = String.format("数字分组:%,d", 123456789);
        // 数字分组:123,456,789
        System.out.println(str);
        // (
        str = String.format("括号用法:%(d", -6666);
        // 括号用法:(6666)
        System.out.println(str);
        // 括号用法:6666
        str = String.format("括号用法:%(d", 6666);
        System.out.println(str);
        // #
        str = String.format("#号用法(十六进制):%#x", 12);
        // #号用法(十六进制):0xc
        System.out.println(str);
        str = String.format("#号用法(八进制):%#o", 12);
        // #号用法(八进制):014
        System.out.println(str);
        // <
        str = String.format("<括号用法:%f %<3.1f", 3.14, 3.2);
        // "%<3.1f"作用的对象是前一个"%f"所作用的对象
        // <括号用法:3.140000 3.1
        System.out.println(str);
    }

3.4 Date conversion character

the sign illustrate
c Include full date and time information
F "Year-Month-Day" format
D 'MM/DD/YYYY' format
r "HH:MM:SS PM" format (12-hour format)
T "HH:MM:SS" format (24 hour format)
R "HH:MM" format (24 hour format)

The sample code is as follows:

public static void main(String[] args) {
    
    
        String str;
        // c
        str = String.format("全部日期和时间信息:%tc", new Date());
        System.out.println(str); // 全部日期和时间信息:星期二 五月 16 13:07:05 CST 2023
        // F
        str = String.format("年-月-日格式:%tF", new Date());
        System.out.println(str); // 年-月-日格式:2023-05-16
        //D
        str = String.format("月/日/年格式:%tD", new Date());
        System.out.println(str); // 月/日/年格式:05/16/23
        //r
        str = String.format("HH:MM:SS PM格式(12时制):%tr", new Date());
        System.out.println(str); // HH:MM:SS PM格式(12时制):01:07:05 下午
        //T
        str = String.format("HH:MM:SS 格式(24时制):%tT", new Date());
        System.out.println(str); // HH:MM:SS 格式(24时制):13:07:05
        //R
        str = String.format("HH:MM格式(24时制):%tR", new Date());
        System.out.println(str); // HH:MM格式(24时制):13:07
        // %.2f保留两位小数
        str = String.format("3.14保留两位小数:%.2f", Math.PI);
        System.out.println(str); // 3.14保留两位小数:3.14
    }

The above content is some common functions of the String.format() method, which are often used in work scenarios. The purpose of organizing and summarizing is to facilitate subsequent study and reference. If there are other usages of related methods later, we will continue to supplement this article.

This article is over!

Guess you like

Origin blog.csdn.net/weixin_44299027/article/details/130701134