Robert
Robert

Reputation: 6226

Convert int string to hex string

How would I convert a string that represents an integer like "4322566" to a hex string?

Upvotes: 9

Views: 2871

Answers (5)

jball
jball

Reputation: 25024

int temp = 0;
string hexOut = string.Empty;
if(int.TryParse(yourIntString, out temp))
{
    hexOut = temp.ToString("X");
}

To handle larger numbers per your comment, written as a method

public static string ConvertToHexString(string intText)
{
    long temp = 0; 
    string hexOut = string.Empty; 
    if(long.TryParse(intText, out temp))
    { 
        hexOut = temp.ToString("X"); 
    } 
    return hexOut;
}

Upvotes: 2

Femaref
Femaref

Reputation: 61497

string s = int.Parse("4322566").ToString("X");

Upvotes: 10

dbasnett
dbasnett

Reputation: 11773

or .ToString("x") if you prefer lowercase hex.

Upvotes: 1

griegs
griegs

Reputation: 22770

Try

int otherVar= int.Parse(hexstring ,
System.Globalization.NumberStyles.HexNumber);

Upvotes: 0

Kevin L.
Kevin L.

Reputation: 4558

Try .ToString("X").

Upvotes: 1

Related Questions