简体   繁体   English

将int转换为十六进制但不是字符串

[英]Converting int to hex but not string

Now I know that converting a int to hex is simple but I have an issue here. 现在我知道将int转换为十六进制很简单,但这里有一个问题。 I have an int that I want to convert it to hex and then add another hex to it. 我有一个int,我想将其转换为十六进制,然后添加另一个十六进制。 The simple solution is int.Tostring("X") but after my int is turned to hex, it is also turned to string so I can't add anything to it until it is turned back to int again. 简单的解决方案是int.Tostring("X")但是在我的int转换为十六进制之后,它也被转换为字符串,因此我不能向它添加任何内容,直到它再次返回到int。

So my question is; 所以我的问题是; is there a way to turn a int to hex and avoid having turned it to string as well. 有没有办法将int转换为十六进制,并避免将其转换为字符串。 I mean a quick way such as int.Tostring("X") but without the int being turned to string. 我的意思是快速的方式,如int.Tostring("X")但没有将int转换为字符串。

I mean a quick way such as int.Tostring("X") but without the int being turned to string. 我的意思是快速的方式,如int.Tostring("X")但没有将int转换为字符串。

No. 没有。

Look at this way. 看这边。 What is the difference between those? 那些有什么区别?

var i = 10;
var i = 0xA;

As a value, they are exactly same. 作为一个价值,它们完全相同 As a representation , first one is decimal notation and the second one is hexadecimal notation . 作为表示 ,第一个是十进制表示法 ,第二个是十六进制表示法 The X you use hexadecimal format specifier which generates hexadecimal notation of that numeric value. X使用十六进制格式说明符 ,它生成数值的十六进制表示法。

Be aware, you can parse this hexadecimal notation string to integer anytime you want. 请注意,您可以随时将此十六进制表示法字符串解析为整数。

There is no need to convert. 没有必要转换。 Number ten is ten, write it in binary or hex, yes their representation will differ depending in which base you write them but value is same. 十号是10,用二进制或十六进制写,是的,它们的表示会有所不同,这取决于你写它们的基数但值是相同的。 So just add another integer to your integer - and convert the final result to hex string when you need it. 所以只需在整数中添加另一个整数 - 并在需要时将最终结果转换为十六进制字符串。


Take example. 举个例子。 Assume you have 假设你有

int x = 10 + 10; // answer is 20 or 0x14 in Hex.

Now, if you added 现在,如果你添加了

int x = 0x0A + 0x0A; // x == 0x14

Result would still be 0x14 . 结果仍然是0x14 See? 看到?

Numeric 10 and 0x0A have same value just they are written in different base. 数字100x0A具有相同的值,只是它们写在不同的基数中。

Hexadecimal string although is a different beast. 十六进制字符串虽然是一个不同的野兽。 In above case that could be "0x14". 在上面的情况下,可能是“0x14”。 For computer this would be stored as: '0', 'x', '1', '4' - four separate characters (or bytes representing these characters in some encoding). 对于计算机,这将存储为: '0', 'x', '1', '4' - 四个单独的字符(或在某些编码中表示这些字符的字节)。 While in case with integers, it is stored as single integer (encoded in binary form). 在整数的情况下,它存储为单个整数(以二进制形式编码)。

I guess you missing the point what is HEX and what is INT. 我猜你错过了什么是HEX和什么是INT。 They both represent an numbers. 他们代表一个数字。 1, 2, 3, 4, etc.. numbers. 1,2,3,4等数字。 There's a way to look at numbers: as natural numbers: INT and hexadecimal - but at the end those are same numbers. 有一种方法可以查看数字:作为自然数:INT和十六进制 - 但最后这些是相同的数字。 For example if you have number: 5 + 5 = 10 (int) and A (as hex) but it the same number. 例如,如果您有数字:5 + 5 = 10(int)和A(作为十六进制)但它的数字相同。 Just view on them are different 只是看他们是不同的

Hex is just a way to represent number. 十六进制只是表示数字的一种方式。 The same statment is true for decimal number system and binary although with exception of some custom made numbers (BigNums etd) everything will be stored as binary as long as its integer (by integer i mean not floating point number). 对于十进制数字系统和二进制数字也是如此,尽管除了一些自定义数字(BigNums etd)之外,只要其整数(通过整数表示不是浮点数),所有内容都将存储为二进制。 What would you really like to do is probably performing calculations on integers and then printing them as a Hex which have been already described in this topic C# convert integer to hex and back again 你真正想做的是对整数进行计算然后将它们打印成十六进制已经在本主题中描述了C#将整数转换为十六进制并再次返回

The short answer: no, and there is no need. 简短的回答:不,没有必要。

The integer One Hundred and seventy nine (179) is B3 in hex, 179 in base-10, 10110011 in base-2 and 20122 in base-3. 整数一百七十九(179)是十六进制中的B3,基数-10中的179,基-2中的10110011和碱-3中的20122。 The base of the number doesn't change the value of it. 数字的基数不会改变它的值。 B3, 17, 10110011, and 20122 are all the same number, they are just represented different. B3,17,10110011和20122都是相同的数字,它们只是代表不同。 So it doesn't matter what base they are in as long as you do you mathematical operations on numbers in the same base it doesn't matter what the base is. 因此,只要你对同一基数中的数字进行数学运算,无论基数是什么,它们的基数都无关紧要。

So in your case with Hex numbers, they can contain characters such as 'A','B', 'C', and so on. 因此,在您使用十六进制数字的情况下,它们可以包含诸如“A”,“B”,“C”等字符。 So when you get a value in hex if it is a number that will contain a letter in its hex representation it will have to be a string as letters are not ints. 因此,当你得到一个十六进制的值时,如果它是一个在十六进制表示中包含一个字母的数字,那么它必须是一个字符串,因为字母不是整数。 To do what you want, it would be best to convert both numbers to regular ints and then do math and convert to Hex after. 要做你想做的事情,最好将两个数字转换为常规的int然后再做数学并转换为Hex。 The reason for this is that if you want to be able to add (or whatever operation) with them looking like hex you are going to to need to change the behavior of the desired operator on string which is a hassle. 这样做的原因是,如果您希望能够添加(或任何操作)它们看起来像十六进制,您将需要在字符串上更改所需操作符的行为,这是一个麻烦。

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

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