none
How to convert float/double/ulong into binary/otcal/hex string??

    Question

  • Hi, all.

    As I known, i can conver int into binary/otcal/hex string by using
    Convert.ToString( int_var,   2/8/16);

    But how can i convert float/double/ulong into binary/otcal/hex string????






    Thursday, May 15, 2008 7:19 AM

Answers

  • You should first convert the value to a byte array and then construct the string from that array:

    string s = String.Empty;

    // value is the value you want to convert to a string (double, long, int, ...)

    foreach (byte b in BitConverter.GetBytes(value))
    {
       s += Convert.ToString(b,16).PadLeft(2,'0'); // for hex. For binary, use 2 and 8. For octal, use 8 and 3
    }


    Thursday, May 15, 2008 8:04 AM

All replies

  • You should first convert the value to a byte array and then construct the string from that array:

    string s = String.Empty;

    // value is the value you want to convert to a string (double, long, int, ...)

    foreach (byte b in BitConverter.GetBytes(value))
    {
       s += Convert.ToString(b,16).PadLeft(2,'0'); // for hex. For binary, use 2 and 8. For octal, use 8 and 3
    }


    Thursday, May 15, 2008 8:04 AM
  • Thank you, It works.....

    Thursday, May 15, 2008 8:23 AM
  • You should first convert the value to a byte array and then construct the string from that array:

    string s = String.Empty;

    // value is the value you want to convert to a string (double, long, int, ...)

    foreach (byte b in BitConverter.GetBytes(value))
    {
       s += Convert.ToString(b,16).PadLeft(2,'0'); // for hex. For binary, use 2 and 8. For octal, use 8 and 3
    }


    支持 ulong 吗? 
    Wednesday, February 22, 2012 12:16 PM