我有一个用Delphi编写的程序将11转换为 0xB ,将28转换为 0x1c 。我试图在c#中将11(十进制到十六进制)转换为: -
var deciValue01 = 11;
var deciValue02 = 28;
var deciValue03 = 13;
System.Diagnostics.Debug.WriteLine(string.Format("11 = {0:x}", deciValue01));
System.Diagnostics.Debug.WriteLine(string.Format("28 = {0:x}", deciValue02));
System.Diagnostics.Debug.WriteLine(string.Format("13 = {0:x}", deciValue03));
但我得到的结果是: -
想知道如何将11转换为'0xB',将28转换为'0x1c',将13转换为'0xD'?是不是我需要从十进制更改为十六进制?
答案 0 :(得分:2)
您只需使用X
将其设为大写十六进制数字而不是小写,然后自己添加0x
:
// Add using System.Diagnostics; at the top of the file... no need to
// explicitly qualify all your type names
Debug.WriteLine(string.Format("11 = 0x{0:X}", deciValue01));
Debug.WriteLine(string.Format("28 = 0x{0:X}", deciValue02));
Debug.WriteLine(string.Format("13 = 0x{0:X}", deciValue03));
请注意,deciValue01
值本身既不是“十进制”也不是“十六进制”。他们只是数字。当你谈论文本表示时,“十进制”或“十六进制”的概念才有意义,至少对于整数而言。 (这对浮点很重要,其中可表示类型的集合取决于所使用的基数。)
答案 1 :(得分:1)
试试这个
int value = Convert.ToInt32(/*"HexValue"*/);
String hexRepresentation = Convert.ToString(value, 16);
答案 2 :(得分:0)
听起来你想要这个......
var deciValue01 = 11;
var deciValue02 = 28;
var deciValue03 = 13;
System.Diagnostics.Debug.WriteLine(string.Format("11 = 0x{0:x}", deciValue01));
System.Diagnostics.Debug.WriteLine(string.Format("28 = 0x{0:x}", deciValue02));
System.Diagnostics.Debug.WriteLine(string.Format("13 = 0x{0:x}", deciValue03));