简体   繁体   中英

How to format numbers to a hex strings?

I want to format int numbers as hex strings. System.out.println(Integer.toHexString(1)); prints 1 but I want it as 0x00000001 . How do I do that?

尝试这个

System.out.println(String.format("0x%08X", 1));

You can use the String.format to format an integer as a hex string.

   System.out.println(String.format("0x%08X", 1));

That is, pad with zeros, and make the total width 8. The 1 is converted to hex for you. The above line gives: 0x00000001 and

   System.out.println(String.format("0x%08X", 234));

gives: 0x000000EA

From formatting syntax documented on Java's Formatter class:

Integer intObject = Integer.valueOf(1);
String s = String.format("0x%08x", intObject);
System.out.println(s);

我不太了解 Java,但是必须有一种方法可以将 toHexString 函数的输出用“0”填充到长度为 8。如果“0x”总是在开头,只需加上那个字符串到开头。

不那么冗长:

System.out.printf("0x%08x", 1); //"Use %0X for upper case letters

Java 17+

There is a new immutable class dedicated to conversion into and formatting hexadecimal numbers. The easiest way to go is using HexFormat::toHexDigits which includes leading zeroes:

String hex = "0x" + HexFormat.of().toHexDigits(1);
// 0x00000001

Beware , one has to concatenate with the "0x" prefix as such method ignores defined prefixes and suffixes, so the following snippet doesn't work as expected (only HexFormat::formatHex methods work with them):

String hex = HexFormat.of().withPrefix("0x").toHexDigits(1);
// 00000001

Returns the eight hexadecimal characters for the int value. Each nibble (4 bits) from most significant to least significant of the value is formatted as if by toLowHexDigit(nibble) . The delimiter, prefix and suffix are not used.

Alternatively use the advantage of HexFormat::formatHex formatting to two hexadecimal characters, and a StringBuilder as an Appendable prefix containing "0x" :

Each byte value is formatted as the prefix, two hexadecimal characters selected from uppercase or lowercase digits, and the suffix.

StringBuilder hex = HexFormat.of()
    .formatHex(new StringBuilder("0x"), new byte[] {0, 0, 0, 1});
// 0x00000001
StringBuilder hex = HexFormat.of()
    .formatHex(new StringBuilder("0x"), ByteBuffer.allocate(4).putInt(1).array());
// 0x00000001

您可以在 PrintStream 上使用java.util.Formatterprintf 方法

This is String extension for Kotlin

//if lengthOfResultTextNeeded = 3 and input String is "AC", the result is = "0AC"
//if lengthOfResultTextNeeded = 4 and input String is "AC", the result is = "00AC"

fun String.unSignedHex(lengthOfResultTextNeeded: Int): String {
    val count =
        lengthOfResultTextNeeded - this.length 
    val buildHex4DigitString = StringBuilder()
    var i = 1
    while (i <= count) {
        buildHex4DigitString.append("0")
        ++i
    }
    return buildHex4DigitString.toString() + this
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM