整数转换问题 [英] integer conversion problem
问题描述
大家好,
将字符串值转换为整数值时,结果以十六进制格式返回.
例如:int output = Convert.ToInt16("1");
当手表中的手表输出值显示为0x001时.
和int output1 = Convert.ToInt32("1");
output1值显示为0x00000001.
我没问题但是,此问题仅在我的系统中出现.
Hi all,
When converting a string value to integer value the result return as hexadecimal format.
Ex: int output=Convert.ToInt16("1");
when watch output value in a add watch it displayed as 0x001.
and int output1 = Convert.ToInt32("1");
output1 value displayed as 0x00000001.
I don''t the problem. But this problem occurs only in my system. Please reply me if u know the answer.
推荐答案
不,不是.
整数没有十六进制格式"或十进制格式"-它们是数字(实际上总是以二进制存储).
唯一使用十六进制格式的时间是从十六进制格式的字符串转换为整数或从整数转换为十六进制格式的字符串时.在第一种情况下,您将使用:
No, it doesn''t.
Integers do not have a "Hex format", or a "decimal format" - they are numbers (and are in fact always stored in binary).
The only time you use a hexadecimal format is when you are converting from a sting in hex format ro an integer, or from an integer to a string in hex format. In the first case, you would use:
int myInteger = Int32.Parse(hexString, System.Globalization.NumberStyles.HexNumber);
第二个:
and in the second:
string hexString = myInteger.ToString("X4");
输入字符串必须表示 ^ ]起作用.
其输出是整数.既没有十进制也没有十六进制格式.这只是正确的值.
您可以再次将其转换为字符串以将其显示给用户.使用 ToString()方法 [
The input string has to represent the number in decimal for Convert.ToInt16(String)[^] to work.
Its output is an integer. That has neither decimal nor hexadecimal format. It''s just the correct value.
You can convert it into a string again to display it to the user. Use the ToString() method[^] for that.
If you want the output string to be in hexadecimal format, use
Int16 number = 255;
string output = number.ToString("X");
为此.
如果要将表示十六进制数字的字符串转换为整数,请使用
for that.
If you want to convert a string that represents a number in hexadecimal form to an integer, use
Int16 number;
bool success = Int16.TryParse(
"0xFF",
NumberStyles.HexNumber,
CultureInfo.InvariantCulture,
out value
);
这篇关于整数转换问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!