51

如何在XX XX XX XX没有循环的情况下将 int(4 字节)转换为十六进制(“”)?

例如:

i=13 hex="00 00 00 0D"

i.ToString("X")返回"D",但我需要一个 4 字节的十六进制值。

4

2 回答 2

85

You can specify the minimum number of digits by appending the number of hex digits you want to the X format string. Since two hex digits correspond to one byte, your example with 4 bytes needs 8 hex digits. i.e. use i.ToString("X8").

If you want lower case letters, use x instead of X. For example 13.ToString("x8") maps to 0000000d.

于 2013-04-10T07:53:15.527 回答
12

try this:

int innum = 123;
string Hex = innum .ToString("X");  // gives you hex "7B"
string Hex = innum .ToString("X8");  // gives you hex 8 digit "0000007B"
于 2013-04-10T07:53:47.700 回答