Home > Software engineering >  How to convert offset as a static string to a const int C#
How to convert offset as a static string to a const int C#

Time:09-04

So i have a function that scans a signature from a program and adds it to a string, i was wondering how i could add that string and turn it to a const int to use it in other functions

public static string RoundSkipOffset = "0x69B73F7";

and i want to get it in the format of

public const int RoundSkipOffset = 0x69B73F7;

ive tried parse but couldnt find a way, sorry if this is stupid i am very new to coding

CodePudding user response:

Well, the sequence is too long for int (which has 4 bytes only), long (which consist of 8 bytes); you can use BigInteger since you want 10 bytes to store:

using System.Numerics;

...

string RoundSkipSig = "8B 91 20 02 00 00 8B CA 83 E1";

...

readonly BigInteger result = BigInteger
  .Parse(RoundSkipSig.Replace(" ", ""), NumberStyles.HexNumber);

Note, that

  1. You should get rid of spaces before before parsing
  2. You can't declare result as const since you compute it in runtime

Edit: if you have small value which fit int range you can Convert

public static readonly int RoundSkipOffset = Convert.ToInt32("0x69B73F7", 16);

again, you can't use const but readonly

  • Related