user2968369
user2968369

Reputation: 275

simple int to hex conversion not working

I have a very simple code where I get a value from numeric updown which I have then to convert to hex. (for numericupdown Hexadecimal property is set true)

I select FF from nud located in winForm. then in code

string str = nudID.Value.ToString("X");

But this is not working and I am getting format exception

Upvotes: 0

Views: 265

Answers (4)

Marco
Marco

Reputation: 23935

this should work:

string str = ((int)nudID.Value).ToString("X");

You need to parse the decimal value to int. You can make it a bit safer by using TryParse if you want to.

Upvotes: 0

chwarr
chwarr

Reputation: 7202

NumericUpDown.Value returns Decimal. Decimal.ToString(string) does not support "X":

The format parameter can be any valid standard numeric format specifier except for D, R, and X

Adapting some code from this solution, try this if you are on .NET 4.0 or later:

string str = new System.Numerics.BigInteger(nudID.Value).ToString("X");

Upvotes: 3

jiten
jiten

Reputation: 5264

Try this

string str = nudID.Value.ToString("X2");

Upvotes: 1

RRM
RRM

Reputation: 3451

NumericUpDown value isn't Int type, it's Decimal. Maybe here is the problem?

Upvotes: 4

Related Questions