Friday, October 9, 2009

How to convert an integer to a hex string

Use Int32.ToString(String) method:

int integer = 12345678;
String hexString = integer.ToString("X");

Result: BC614E

Optional precision specifier indicates the minimum number of digits desired in the resulting string. If required, the number is padded with zeros to its left to produce the number of digits given by the precision specifier:

String hexString = integer.ToString("X8");

Result: 00BC614E

Use lowercase 'x' to generate lowercase characters for the hexadecimal digits greater than 9:

String hexString = integer.ToString("x8");

Result: 00bc614e

No comments:

Post a Comment