Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用c#将十进制转换为十六进制?_C#_Ascii - Fatal编程技术网

如何使用c#将十进制转换为十六进制?

如何使用c#将十进制转换为十六进制?,c#,ascii,C#,Ascii,我有一个用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(

我有一个用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=b
  • 28=1c
  • 13=d

想知道如何将11转换为“0xB”,将28转换为“0x1c”,将13转换为“0xD”?我不是需要从十进制改为十六进制吗?

您只需要使用
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
值本身既不是“十进制”也不是“十六进制”。它们只是数字。“十进制”或“十六进制”的概念只有在讨论文本表示时才有意义,至少对于整数是如此。(这对于浮点很重要,其中可表示类型的集合取决于所使用的基。)

您只需使用
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
值本身既不是“十进制”也不是“十六进制”。它们只是数字。“十进制”或“十六进制”的概念只有在讨论文本表示时才有意义,至少对于整数是如此。(这对于浮点很重要,其中可表示类型的集合取决于所使用的基。)

听起来您想要这个

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));

听起来你想要这个

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));
试试这个

int value = Convert.ToInt32(/*"HexValue"*/);
String hexRepresentation = Convert.ToString(value, 16);
试试这个

int value = Convert.ToInt32(/*"HexValue"*/);
String hexRepresentation = Convert.ToString(value, 16);

那么您想添加一个“0x”前缀?为什么不简单地将其添加为文字<代码>格式化(“0x{0:X}”)并使用“`0x{0:X}”将十六进制字符改为大写而不是小写,因为它是您想要的。您得到的完全正确?只需将
Ox
添加到您生成的字符串中?那么您想添加一个“0x”前缀吗?为什么不简单地将其添加为文字<代码>格式化(“0x{0:X}”)并使用“`0x{0:X}”将十六进制字符改为大写而不是小写,因为它是您想要的。您得到的完全正确?只需在生成的字符串中添加
Ox