简体   繁体   English

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

[英]How to convert decimal to hex using c#?

I have a program written in Delphi is converting 11 to 0xB and 28 to 0x1c . 我有一个用Delphi编写的程序将11转换为0xB并将28转换为0x1c I tried to convert 11 (decimal to Hex) in c# using this:- 我试图使用以下方法在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));

but the results I am getting is:- 但是我得到的结果是:

  • 11 = b 11 = b
  • 28 = 1c 28 = 1分
  • 13 = d 13 =天

Wondering how to convert 11 to '0xB' and 28 to '0x1c' and 13 to '0xD'? 想知道如何将11转换为“ 0xB”,将28转换为“ 0x1c”,将13转换为“ 0xD”? Isn't it I need to change from Decimal to Hex? 我需要从十进制更改为十六进制吗?

You just need to use X to make it capital hex digits instead of lower case, and add the 0x yourself: 您只需要使用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));

Note that the deciValue01 values are neither "decimal" nor "hex" themselves. 请注意, deciValue01值本身既不是“十进制”也不是“十六进制”。 They're just numbers. 他们只是数字。 The concept of "decimal" or "hex" only makes sense when you're talking about a textual representation, at least for integers. “十进制”或“十六进制”的概念仅在谈论文本表示形式时才有意义,至少对于整数而言。 (It matters for floating point, where the set of representable types depends on the base used.) (对于浮点很重要,其中可表示类型的集合取决于所使用的基数。)

Try This 尝试这个

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

It sounds like you want this... 听起来你想要这个...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM