Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Binary to hex Conversion ?

Binary to hex Conversion ?

Scheduled Pinned Locked Moved C#
csharpdata-structuresquestion
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    deadlyabbas
    wrote on last edited by
    #1

    Ho to convert binary to hex in C#. I have a byte array and I want to convert it into hex.

    K D L 3 Replies Last reply
    0
    • D deadlyabbas

      Ho to convert binary to hex in C#. I have a byte array and I want to convert it into hex.

      K Offline
      K Offline
      Khaniya
      wrote on last edited by
      #2

      you can do it on this way

      byte b = 15;
      Console.WriteLine(b.ToString("X"));

      Life's Like a mirror. Smile at it & it smiles back at you.- P Pilgrim So Smile Please

      D 1 Reply Last reply
      0
      • D deadlyabbas

        Ho to convert binary to hex in C#. I have a byte array and I want to convert it into hex.

        D Offline
        D Offline
        DaveyM69
        wrote on last edited by
        #3

        The convert class can also be used for this:

        Convert.ToString(number, base)

        where base can be 2 (binary), 10 (decimal) or 16 (hexadecimal).

        Dave
        Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

        D 1 Reply Last reply
        0
        • D DaveyM69

          The convert class can also be used for this:

          Convert.ToString(number, base)

          where base can be 2 (binary), 10 (decimal) or 16 (hexadecimal).

          Dave
          Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

          D Offline
          D Offline
          deadlyabbas
          wrote on last edited by
          #4

          Thanks for your reply. If you just convert it as you have shown it Eliminates the "0". Say for example you have to convert 0000111111111000 into Hex.So it will show FF8 instead 0FF8 Well I was able to over come that by doing following:

          string MyBinary = "0000111111111000";
          string MyHex = String.Format("{0:X4}", Convert.ToInt32(MyBinary, 2));

          D 1 Reply Last reply
          0
          • K Khaniya

            you can do it on this way

            byte b = 15;
            Console.WriteLine(b.ToString("X"));

            Life's Like a mirror. Smile at it & it smiles back at you.- P Pilgrim So Smile Please

            D Offline
            D Offline
            deadlyabbas
            wrote on last edited by
            #5

            Thanks .....!!!!!!!

            1 Reply Last reply
            0
            • D deadlyabbas

              Thanks for your reply. If you just convert it as you have shown it Eliminates the "0". Say for example you have to convert 0000111111111000 into Hex.So it will show FF8 instead 0FF8 Well I was able to over come that by doing following:

              string MyBinary = "0000111111111000";
              string MyHex = String.Format("{0:X4}", Convert.ToInt32(MyBinary, 2));

              D Offline
              D Offline
              DaveyM69
              wrote on last edited by
              #6

              When I need padding I use the string.PadLeft method. A couple of functions like this are all that's needed.

              public static string ToHexString(int value)
              {
              return ToHexString(value, 0);
              }
              public static string ToHexString(int value, int bytes)
              {
              string result = Convert.ToString(value, 16);
              if (bytes > 0)
              result = result.PadLeft(bytes * 2, '0');
              return result;
              }

              Dave
              Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
              BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

              1 Reply Last reply
              0
              • D deadlyabbas

                Ho to convert binary to hex in C#. I have a byte array and I want to convert it into hex.

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                int b = 15;
                string s=b.ToString("X8");

                I advice to always specify the width you want; padding zeroes will be prefixed when necessary. :)

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups