3

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
        }
MaxW
  • 421
  • 1
  • 7
  • 22

1 Answers1

4

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");
Slasko
  • 407
  • 2
  • 8