Hex String Formatting

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

    #1

    Hex String Formatting

    byte b = 10;

    Console.WriteLi ne("{0} = 0x{0,2:X}", b);

    // This example displays "10 = 0x A". Note the space between 0x and A

    // I want it to display as: "10 = 0x0A" Note the zero between 0x and A

    // How can I change the format string

    // to pad it to 2 digits using a leading 0 if necessary

    // instead of a space in the hex representation?

    // Dave


  • Ray Hsieh (Ray Djajadinata)

    #2
    Re: Hex String Formatting

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Hi David,

    Use X2 instead of just X.

    David Hall wrote:
    | byte b = 10;
    |
    | Console.WriteLi ne("{0} = 0x{0,2:X}", b);
    |
    | // This example displays "10 = 0x A". Note the space between 0x and A
    |
    | // I want it to display as: "10 = 0x0A" Note the zero between 0x and A
    |
    | // How can I change the format string
    |
    | // to pad it to 2 digits using a leading 0 if necessary
    |
    | // instead of a space in the hex representation?
    |
    | // Dave
    |
    |


    - --
    Ray Hsieh (Ray Djajadinata) [SCJP, SCWCD]
    ray underscore usenet at yahoo dot com
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.3 (MingW32)
    Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

    iD8DBQE/nyu0wEwccQ4rWPg RAigrAJ0TcDPfLD YBHHY9+3xbLolJU X5aZQCdFlMv
    DC5EfUzPvbvQc8z lN8Ym3S0=
    =cokt
    -----END PGP SIGNATURE-----

    Comment

    • David Hall

      #3
      Re: Hex String Formatting

      Thanks, The following changes both work correctly, and messing with the
      numbers gives me a little more insight as to how the format specifers work
      in practice...

      byte b = 10;

      Console.WriteLi ne("{0} = 0x{0,0:X2}", b);

      Console.WriteLi ne("{0} = 0x{0,2:X2}", b);

      Just for my own curiosity, can anyone show me where to find this info on
      MSDN? I'm glad it works, but I still can't find any docs that say it
      should...

      Thanks again,

      Dave



      "Ray Hsieh (Ray Djajadinata)" <check@my.signa ture.com> wrote in message
      news:O3nlFRcnDH A.2772@TK2MSFTN GP12.phx.gbl...[color=blue]
      > -----BEGIN PGP SIGNED MESSAGE-----
      > Hash: SHA1
      >
      > Hi David,
      >
      > Use X2 instead of just X.
      >
      > David Hall wrote:
      > | byte b = 10;
      > |
      > | Console.WriteLi ne("{0} = 0x{0,2:X}", b);
      > |
      > | // This example displays "10 = 0x A". Note the space between 0x and A
      > |
      > | // I want it to display as: "10 = 0x0A" Note the zero between 0x and A
      > |
      > | // How can I change the format string
      > |
      > | // to pad it to 2 digits using a leading 0 if necessary
      > |
      > | // instead of a space in the hex representation?
      > |
      > | // Dave
      > |
      > |
      >
      >
      > - --
      > Ray Hsieh (Ray Djajadinata) [SCJP, SCWCD]
      > ray underscore usenet at yahoo dot com
      > -----BEGIN PGP SIGNATURE-----
      > Version: GnuPG v1.2.3 (MingW32)
      > Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org
      >
      > iD8DBQE/nyu0wEwccQ4rWPg RAigrAJ0TcDPfLD YBHHY9+3xbLolJU X5aZQCdFlMv
      > DC5EfUzPvbvQc8z lN8Ym3S0=
      > =cokt
      > -----END PGP SIGNATURE-----
      >[/color]


      Comment

      • Ray Hsieh (Ray Djajadinata)

        #4
        Re: Hex String Formatting

        -----BEGIN PGP SIGNED MESSAGE-----
        Hash: SHA1

        Hi David,

        I don't have the exact page with me right now, but searching for "format
        string composite" should give you what you want.

        David Hall wrote:

        | Thanks, The following changes both work correctly, and messing with the
        | numbers gives me a little more insight as to how the format specifers work
        | in practice...
        |
        | byte b = 10;
        |
        | Console.WriteLi ne("{0} = 0x{0,0:X2}", b);
        |
        | Console.WriteLi ne("{0} = 0x{0,2:X2}", b);
        |
        | Just for my own curiosity, can anyone show me where to find this info on
        | MSDN? I'm glad it works, but I still can't find any docs that say it
        | should...
        |
        | Thanks again,
        |
        | Dave

        - --
        Ray Hsieh (Ray Djajadinata) [SCJP, SCWCD]
        ray underscore usenet at yahoo dot com
        -----BEGIN PGP SIGNATURE-----
        Version: GnuPG v1.2.3 (MingW32)
        Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

        iD8DBQE/nz/GwEwccQ4rWPgRAp hcAJ4xYjn0nErsN fZZcnLIA0MG1kVp jACeJ7jb
        2SBKgmaQmTZFL45 K/zarQuM=
        =WO/6
        -----END PGP SIGNATURE-----

        Comment

        • David Hall

          #5
          Re: Hex String Formatting

          Thanks again Ray.

          Your search criteria led me to
          http://msdn.microsoft.com/library/de...matstrings.asp.
          The first paragraph describes the format specification, and the examples at
          http://msdn.microsoft.com/library/de...putexample.asp
          explain it pretty well. These two pages are definitely going to be
          bookmarked.

          Dave


          "Ray Hsieh (Ray Djajadinata)" <check@my.signa ture.com> wrote in message
          news:OEUV7AdnDH A.372@TK2MSFTNG P11.phx.gbl...[color=blue]
          > -----BEGIN PGP SIGNED MESSAGE-----
          > Hash: SHA1
          >
          > Hi David,
          >
          > I don't have the exact page with me right now, but searching for "format
          > string composite" should give you what you want.
          >
          > David Hall wrote:
          >
          > | Thanks, The following changes both work correctly, and messing with the
          > | numbers gives me a little more insight as to how the format specifers[/color]
          work[color=blue]
          > | in practice...
          > |
          > | byte b = 10;
          > |
          > | Console.WriteLi ne("{0} = 0x{0,0:X2}", b);
          > |
          > | Console.WriteLi ne("{0} = 0x{0,2:X2}", b);
          > |
          > | Just for my own curiosity, can anyone show me where to find this info on
          > | MSDN? I'm glad it works, but I still can't find any docs that say it
          > | should...
          > |
          > | Thanks again,
          > |
          > | Dave
          >[/color]


          Comment

          Working...