简体   繁体   English

C#中从十六进制到int的转换不正确

[英]Incorrect conversion from hex to int in C#

What can be the reason for the problem? 这个问题的原因是什么? My method returns incorrect int values. 我的方法返回不正确的int值。 When I give it hex value of AB or DC or something similar it returns int = 0 but when I give it a hex = 22 it returns me int = 22. (though int should be 34 in this case). 当我给它AB或DC的十六进制值或类似的东西它返回int = 0但是当我给它一个十六进制= 22它返回我int = 22.(虽然在这种情况下int应该是34)。

public int StatusBit(int Xx, int Rr)  {
        int Number;
        int.TryParse(GetX(Xx,Rr), out Number);
            return Number;
    }

I tried to use Number = Convert.ToInt32(GetX(Xx,Rr)); 我试图使用Number = Convert.ToInt32(GetX(Xx,Rr)); but it gives same result but null instead of 0 for anything that includes letters. 但是对于包含字母的任何内容,它会给出相同的结果但是null而不是0。

Use Convert.ToInt32(string, int) instead. 请改用Convert.ToInt32(string, int) That way you can give a base the number should be interpreted in. Eg 这样你就可以给出一个应该解释数字的基数。例如

return Convert.ToInt32(GetX(Xx, Rr), 16);

(You also don't check the return value of TryParse which would give a hint that the parse failed.) (您也不会检查TryParse的返回值,这会提示解析失败。)

If you expect both decimal and hexadecimal numbers you need to branch according to how the number looks and use either base 10 or base 16. Eg if your hexadeximal numbers always start with 0x you could use something along the following lines: 如果你期望十进制和十六进制数字,你需要根据数字看起来分支,并使用基数10或基数16.例如,如果您的十六进制数始终以0x您可以使用以下行中的内容:

string temp = GetX(Xx, Rr);
return Convert.ToInt32(temp, temp.StartsWith("0x") ? 16 : 10);

But that would depend on how (if at all) you would distinguish the two. 但这取决于你如何区分这两者(如果有的话)。 If everything is hexadecimal then there is no such need, of course. 如果一切都是十六进制的,那么当然没有这种需要。

Use NumberStyles.HexNumber : 使用NumberStyles.HexNumber

using System;
using System.Globalization;

class Test
{
    static void Main()
    {
        string text = "22";
        int value;
        int.TryParse(text, NumberStyles.HexNumber, 
                     CultureInfo.InvariantCulture, out value);
        Console.WriteLine(value); // Prints 34
    }
}

Do you really want to silently return 0 if the value can't be parsed, by the way? 顺便说一下,如果无法解析值,你真的想静默返回0吗? If not, use the return value of int.TryParse to determine whether the parsing succeeded or not. 如果不是,请使用int.TryParse的返回值来确定解析是否成功。 (That's the reason it's returning 0 for "AB" in your original code.) (这就是原始代码中“AB”返回0的原因。)

int.TryParse parses a base 10 integer. int.TryParse解析一个基数为10的整数。

Use Convert.ToUInt32(hex, 16) instead 请改用Convert.ToUInt32(hex, 16)

here is my solution; 这是我的解决方案;

kTemp = int.Parse(xcc, System.Globalization.NumberStyles.HexNumber);

above kTemp is an integer, and xcc is a string. 以上kTemp是一个整数, xcc是一个字符串。 xcc can be anything like; xcc可以是任何类似的; FE, 10BA, FE0912... that is to say; FE,10BA,FE0912 ......也就是说; xcc is a string of hex characters in any length. xcc是任意长度的十六进制字符串。 beware; 谨防; I dont get the 0x prefix with my hex strings. 我没有得到我的十六进制字符串的0x前缀。

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

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