@Sree, Welcome to Microsoft Q&A, you could try the following code to convert int to hex string successfully as you wanted.
First, we could use val.ToCharArray()[0]
to get the first char of string.
Second, we could use Char.IsDigit(firstchar)
to check if the char is number.
Third, we could use v.ToString("X4")
to get the hex string.
static void Main(string[] args)
{
var str = ConvertHex("7fph7vfxbvkl");
}
public static string ConvertHex(string val)
{
string retval = "";
char firstchar = val.ToCharArray()[0];
if (Char.IsDigit(firstchar))
{
int v = Convert.ToInt32(firstchar);
retval = string.Format("_x{0}_{1}", v.ToString("X4"),val.Remove(0,1));
}
return retval;
}
Result:
Hope my code could help you.
Best Regards,
Jack
If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.