Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 701
Evgeniy Dorofeev Avatar asked Dec 13 '12 01:12

Evgeniy Dorofeev


People also ask

How do you write hexadecimal strings?

You can just escape literal hex values in a C string like this: const char * s = "\xab\x64\x0e\x17\x45\xb2\xc7\x8a"; or you can just use hex values directly like this: const unsigned char s[8] = { 0xab, 0x64, 0x0e, 0x17, 0x45, 0xb2, 0xc7, 0x8a };

What is hex string format?

The format for coding a hexadecimal string mask is: X'yy...yy' The value yy represents any pair of hexadecimal digits that constitute a byte (8 bits). Each bit must be 1 (test bit) or 0 (ignore bit). You can specify up to 256 pairs of hexadecimal digits.

How do you display hexadecimal numbers?

In XML and XHTML, characters can be expressed as hexadecimal numeric character references using the notation &#xcode; , for instance ’ represents the character U+2019 (the right single quotation mark). If there is no x the number is decimal (thus ’ is the same character).

How do you make a hex number in Python?

hex() function is one of the built-in functions in Python3, which is used to convert an integer number into it's corresponding hexadecimal form. Syntax : hex(x) Parameters : x - an integer number (int object) Returns : Returns hexadecimal string.


1 Answers

Try this

System.out.println(String.format("0x%08X", 1)); 
like image 74
Drogba Avatar answered Sep 23 '22 02:09

Drogba