本文介绍了十六进制增量/循环直到FFF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含十六进制数字的字符串,我想递增该十六进制数字,直到达到最大数字(FFF).我该如何遍历,以便可以获取起始十六进制和FFF之间的每个数字?
I have an string which contains a hexadezimal number and i want to increment that hex number until i reach my max number (FFF). How can i loop through so i can get every number between my start hex and FFF?
我试图将字符串转换为字节数组,但此后被卡住了.
I tried to convert the string in a byte array but got stuck after that.
string stringHex = "7A";
string binaryval = "";
binaryval = Convert.ToString(Convert.ToInt32(stringHex, 16), 2);
int numOfBytes = binaryval.Length / 8;
byte[] bytes = new byte[numOfBytes];
for (int i = 0; i < numOfBytes; ++i)
{
bytes[i] = Convert.ToByte(binaryval.Substring(8 * i, 8), 2);
}
我需要用它来创建一个显示所有这些数字的表.
I need this to create a table which displays all those numbers.
解决方案:
string sHex = Convert.ToString(sIPv4.Split(':')[2]);
for( int intFromHex = int.Parse(sHex, System.Globalization.NumberStyles.HexNumber);intFromHex <= 4095; intFromHex++)//4095 - FFF
{
string hexValue = intFromHex.ToString("X");
//SQL INSERT
}
推荐答案
您可以将 String
转换为 Int
,将 Int
递增,然后进行转换它返回到 String
(Hex)
You can convert String
to Int
increment the Int
and then convert it back to String
(Hex)
string stringHex = "7A";
int intFromHex = int.Parse(stringHex , System.Globalization.NumberStyles.HexNumber) + 1;
string hexValue = intFromHex.ToString("X");
这篇关于十六进制增量/循环直到FFF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!