Print HEX values from a 8 bit byte?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David

    #1

    Print HEX values from a 8 bit byte?

    Is there an internal function in C# to translate a byte, or a byte
    array, into a HEX string, Ex. "F1C1005E"? If it is int the doc, I am
    missing it.


  • William Stacey

    #2
    Re: Print HEX values from a 8 bit byte?

    Here is one way:

    byte b = 10;
    string s = Convert.ToStrin g(b, 16).PadLeft(2, '0');
    Console.WriteLi ne("byte as Hex:"+s);

    --
    William Stacey, MVP

    "David" <dbmartin5@comc ast.net> wrote in message
    news:l6mvtv0cgm h8ib89ik36rlmbq 17t3ah0ev@4ax.c om...[color=blue]
    > Is there an internal function in C# to translate a byte, or a byte
    > array, into a HEX string, Ex. "F1C1005E"? If it is int the doc, I am
    > missing it.
    >
    >[/color]


    Comment

    • Rob Teixeira [MVP]

      #3
      Re: Print HEX values from a 8 bit byte?

      byte b = <some number>;
      string hexValue = b.ToString("x") ;

      You can also use the override of ToString with the NumberStyles enum.

      -Rob Teixeira [MVP]

      "David" <dbmartin5@comc ast.net> wrote in message
      news:l6mvtv0cgm h8ib89ik36rlmbq 17t3ah0ev@4ax.c om...[color=blue]
      > Is there an internal function in C# to translate a byte, or a byte
      > array, into a HEX string, Ex. "F1C1005E"? If it is int the doc, I am
      > missing it.
      >
      >[/color]


      Comment

      • Scott Gaitskell

        #4
        Re: Print HEX values from a 8 bit byte?

        David <dbmartin5@comc ast.net> wrote in
        news:l6mvtv0cgm h8ib89ik36rlmbq 17t3ah0ev@4ax.c om:
        [color=blue]
        > Is there an internal function in C# to translate a byte, or a byte
        > array, into a HEX string, Ex. "F1C1005E"? If it is int the doc, I am
        > missing it.
        >
        >[/color]


        David,

        The other replies are about single bytes, but if you are using Byte
        arrays, then the BitConverter class may be what your after.

        BitConverter.To String(new Byte[2] {72, 72});

        returns: "48-48"



        Scott


        Comment

        • Austin Ehlers

          #5
          Re: Print HEX values from a 8 bit byte?

          byte[] bytes;
          /.../
          string hexed="";
          foreach(byte b in bytes)
          hexed+=b.ToStri ng("x2");


          On Wed, 17 Dec 2003 04:20:01 GMT, David <dbmartin5@comc ast.net> wrote:
          [color=blue]
          >Is there an internal function in C# to translate a byte, or a byte
          >array, into a HEX string, Ex. "F1C1005E"? If it is int the doc, I am
          >missing it.
          >[/color]

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Print HEX values from a 8 bit byte?

            Austin Ehlers <the*remove*bor ed*me*guy16@hot mail.com> wrote:[color=blue]
            > byte[] bytes;
            > /.../
            > string hexed="";
            > foreach(byte b in bytes)
            > hexed+=b.ToStri ng("x2");[/color]

            When you're going through a loop appending text and you don't know at
            compile-time that the loop won't be repeated more than a few times, you
            should always use a StringBuilder. In this case, you even know the
            capacity the StringBuilder should use:

            byte[] bytes;
            /*...*/

            StringBuilder sb = new StringBuilder(b ytes.Length*2);
            foreach (byte b in bytes)
            {
            sb.AppendFormat ("{0:x2}", b);
            }
            string result = sb.ToString();

            For a 40K array, this makes the difference between taking over 6
            seconds on my box, and under a 20th of a second.

            --
            Jon Skeet - <skeet@pobox.co m>
            Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

            If replying to the group, please do not mail me too

            Comment

            • Austin Ehlers

              #7
              Re: Print HEX values from a 8 bit byte?

              On Thu, 18 Dec 2003 04:59:32 -0000, Jon Skeet [C# MVP]
              <skeet@pobox.co m> wrote:
              [color=blue]
              >Austin Ehlers <the*remove*bor ed*me*guy16@hot mail.com> wrote:[color=green]
              >> byte[] bytes;
              >> /.../
              >> string hexed="";
              >> foreach(byte b in bytes)
              >> hexed+=b.ToStri ng("x2");[/color]
              >
              >When you're going through a loop appending text and you don't know at
              >compile-time that the loop won't be repeated more than a few times, you
              >should always use a StringBuilder. In this case, you even know the
              >capacity the StringBuilder should use:
              >
              >byte[] bytes;
              >/*...*/
              >
              >StringBuilde r sb = new StringBuilder(b ytes.Length*2);
              >foreach (byte b in bytes)
              >{
              > sb.AppendFormat ("{0:x2}", b);
              >}
              >string result = sb.ToString();
              >
              >For a 40K array, this makes the difference between taking over 6
              >seconds on my box, and under a 20th of a second.[/color]

              Yeah, I thought about making it a StringBuilder, but I didn't want to
              make the code more complicated. And you're right, the StringBuilder
              version is much faster, even if you don't specify a default capacity
              in the constructor.


              Comment

              Working...