如何转换为C#中固定长度的十六进制字符串整数? [英] How to convert an integer to fixed length hex string in C#?
本文介绍了如何转换为C#中固定长度的十六进制字符串整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有9999最大值整数变量
I have an integer variable with max value of 9999.
我可以转换为固定长度的字符串(4个字符):
I can convert to fixed length string (4-characters):
value.ToString("0000");
和我可以将其转换为十六进制:
and I can convert it to hex:
value.ToString("X");
我想它在左侧,如果转换为四个字符(填充一个十六进制字符串0值被转换为不足4位十六进制值)。我试过,没有工作以下
I want to convert it to a hex string of four characters (padded with 0 at the left if the value is converted to less than four digits hex value). I tried the following which didn't work.
value.ToString("0000:X");
OK,我可以检查十六进制字符串和垫的左侧用零长度。
OK, I can check the length of hex string and pad left with zeros.
但有没有简单的方法?
推荐答案
在X格式说明后使用一个数字指定左填充: value.ToString(X4)
Use a number after the X format specifier to specify the left padding : value.ToString("X4")
这篇关于如何转换为C#中固定长度的十六进制字符串整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文