Hex increment / loop till FFF

大兔子大兔子 提交于 2021-01-28 12:12:27

问题


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.

Solution:

        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
        }

回答1:


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


来源:https://stackoverflow.com/questions/33940269/hex-increment-loop-till-fff

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!