Covert String or Object to Hex

Hello Everyone,

I have something like this ....

object var = property.Value;

I want to change var to hex, how can I do it....

In C or C++ we use to do something printf("%x, var)....does the trick....

I'm sure will be really simple, I'm not doing something correct.....

Thanks,

Harsimrat



Answer this question

Covert String or Object to Hex

  • MaGa820

    Sorry for that, the link you sent, there are so much stuff in ther...I just kinda got lost.....OOps
  • goofaholix

    All sorts of stuff :-)

    It looks like you want property.Value.ToString("X"); from the docs.  string.Format("{0}", property.Value); will do ToString, and you can obviously put other constant info in there, but I'm sure you can also format that way, but this part of the docs does not show how, neither does the string.Format docs.

     



  • Jagdeep Sihota

    int i = 6;

    i.ToString("X");

    this definately works. What were you doing You should also read the links I provided.



  • TEE_622

    Sorry, I'm really being annoyin here...

    When I do something like this,

    object var = string.Format("{0}", property.Value);

    I still get the string as you said, then how can I get the hex value out of it......


  • Michael Gaillez

    Hey Sorry what i'm doing wrong here...

    object var = property.Value;

    string val = var.ToString();

    string value = string.Format(val, 1);


  • PEng1

    In C#, it's string.Format, and items to format are specified as {0}, {1}, etc. There are also format specifiers, including ones for hex.

    http://msdn.microsoft.com/library/default.asp url=/library/en-us/cpguide/html/cpconformattingtypes.asp



  • xxxlazyxxx

    Cool - glad to help :-)

  • Mwendel

    Thats where It was, because if you see the property .ToString() it doesnt allow any overloaded functions, so ("X") doesn't work.....
  • Wolfram Menzel

    Like I said, I'm not sure, but the docs I linked you to say you should do this instead:

    property.Value.ToString("X");



  • dognardo

    Awesome, thanks...I was being stupid....

    this works...

    int var = Convert.ToInt32(property.Value);

    string value = var.ToString("X");


  • victor2007

    Oops - sorry *blush*

    Re-reading the example, you can't do this on an object, you need to convert it to an int or a double first.

    You can use int.TryParse to convert it and at the same time make sure it can be converted.



  • polease

    I tried this way...still can't have overloaded function..

    int var = Convert.ToInt32(property.Value.ToString());

    int var1 = int.Parse(property.Value.ToString());

    Doesn't work.....


  • Covert String or Object to Hex