Unicode In C#?

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

    #1

    Unicode In C#?

    Hello,

    I was wondering if anybody knew much about Unicode strings in C#.

    I have a Unicode encoded string but how can I see the contents of this
    string so it doesn't display "?????????? ?"?

    I don't want to trans form it back into a ASCII string using Encoding.ASCII
    before I view the data its the Unicode data I wish to view.

    Thanks

    Mark Relly


  • Jon Skeet [C# MVP]

    #2
    Re: Unicode In C#?

    Mark Relly <mark@mslnet.co .uk> wrote:[color=blue]
    > I was wondering if anybody knew much about Unicode strings in C#.[/color]

    There's always more to learn, but I know a fair amount :)
    [color=blue]
    > I have a Unicode encoded string but how can I see the contents of this
    > string so it doesn't display "?????????? ?"?
    >
    > I don't want to trans form it back into a ASCII string using Encoding.ASCII
    > before I view the data its the Unicode data I wish to view.[/color]

    If you want to see the exact contents, I suggest you do something like:

    foreach (char x in myString)
    {
    Console.WriteLi ne ((int)x);
    }

    and then lookup what those characters are on http://www.unicode.org

    --
    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

    • Ray Hsieh (Ray Djajadinata)

      #3
      Re: Unicode In C#?

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

      Hi Mark,

      When you say "seeing the contents of this string", did you mean printing
      it using Console.WriteLi ne() or something? Because this works just fine
      for me (that is, it prints the PI symbol):

      ~ string Pi = "\u03a0";
      ~ Console.WriteLi ne(Pi);

      How did you display that string of yours?

      Mark Relly wrote:

      | Hello,
      |
      | I was wondering if anybody knew much about Unicode strings in C#.
      |
      | I have a Unicode encoded string but how can I see the contents of this
      | string so it doesn't display "?????????? ?"?
      |
      | I don't want to trans form it back into a ASCII string using
      Encoding.ASCII
      | before I view the data its the Unicode data I wish to view.
      |
      | Thanks
      |
      | Mark Relly
      |
      |


      - --
      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/oO5MwEwccQ4rWPg RAnriAJ9a6FjD/9v8gqMQyHJnRaeD MJfzkgCfbbJF
      AfgF/owf+LJiJpgO49rN ae0=
      =l3YE
      -----END PGP SIGNATURE-----

      Comment

      • Mark Relly

        #4
        Re: Unicode In C#?

        When I say seeing the contents of the string yes I do mean using
        System.WriteLin e.

        The code below
        challenge = Encoding.Unicod e.GetString(byt eArray,0,byteAr ray.Length);

        Console.WriteLi ne(challenge);

        Will give me the output "?????????????? ?????????"

        Which is unfortunate as I cannot "see the contents" in a usefull way

        Regards

        Mark


        Comment

        • Ray Hsieh (Ray Djajadinata)

          #5
          Re: Unicode In C#?

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

          But what is the content of the byteArray, exactly? I mean, when I did this:

          ~ char[] charArray = "Hello World!\u03a0".T oCharArray();
          ~ byte[] byteArray = new
          byte[Encoding.Unicod e.GetByteCount( charArray, 0, charArray.Lengt h)];

          ~ Encoding.Unicod e.GetBytes(char Array, 0, charArray.Lengt h,
          byteArray, 0);

          // Get string from the byte array
          ~ string challenge = Encoding.Unicod e.GetString(byt eArray, 0,
          byteArray.Lengt h);
          ~ Console.WriteLi ne(challenge);

          The challenge is printed just fine (Hello World! plus the PI symbol). So
          it really depends on what the initial content of your bytearray is. If
          you convert it to ascii, what is the content? Can you print it?

          Mark Relly wrote:

          | When I say seeing the contents of the string yes I do mean using
          | System.WriteLin e.
          |
          | The code below
          | challenge = Encoding.Unicod e.GetString(byt eArray,0,byteAr ray.Length);
          |
          | Console.WriteLi ne(challenge);
          |
          | Will give me the output "?????????????? ?????????"
          |
          | Which is unfortunate as I cannot "see the contents" in a usefull way
          |
          | Regards
          |
          | Mark

          - --
          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/oR5TwEwccQ4rWPg RApgmAJ9aRG8H5r iwm4ZEjjoiU4gO+ 1SstACeL0fw
          95ZqTQ5TFnpebIe b90DTUeU=
          =uWun
          -----END PGP SIGNATURE-----

          Comment

          • Mark Relly

            #6
            Re: Unicode In C#?

            An example of the initial content of the byte array is

            string challenge = "Please press the Sign
            ButtonSTARTOFRA NDOMTEXT6D8A4B2 6C98465D32070FA A0A18077F43FDB1 A3B"

            This is then converted to
            byte[] byteArray = Encoding.ASCII. GetBytes(challe nge);

            string stringchallenge = Encoding.Unicod e.GetString(byt eArray);

            This is were when written to Console the string is
            "?????????????? ??????????"

            I want to be able to view the contents of the "?????????? ?" without having
            to convert it back to ascii

            --
            Regards

            Mark



            Comment

            • Ray Hsieh (Ray Djajadinata)

              #7
              Re: Unicode In C#?

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

              Mark, the content of the byte array is ascii-encoded, not unicode
              encoded. It's no wonder you're getting "?"s :)

              Try changing this line:

              | byte[] byteArray = Encoding.ASCII. GetBytes(challe nge);

              to

              | byte[] byteArray = Encoding.Unicod e.GetBytes(chal lenge);

              Then you'll be able to view it just fine.

              Alternatively, you can insert a null between the characters of your
              string, i.e.:

              string challenge = "P\0l\0e\0a\0s\ 0e\0..."

              It will print just fine :)

              Mark Relly wrote:

              | An example of the initial content of the byte array is
              |
              | string challenge = "Please press the Sign
              | ButtonSTARTOFRA NDOMTEXT6D8A4B2 6C98465D32070FA A0A18077F43FDB1 A3B"
              |
              | This is then converted to
              | byte[] byteArray = Encoding.ASCII. GetBytes(challe nge);
              |
              | string stringchallenge = Encoding.Unicod e.GetString(byt eArray);
              |
              | This is were when written to Console the string is
              | "?????????????? ??????????"
              |
              | I want to be able to view the contents of the "?????????? ?" without
              having
              | to convert it back to ascii
              |


              - --
              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/oUlgwEwccQ4rWPg RAktFAJ0ZxSCciV IOZmDMOrel3RDdI tbNdACfachm
              4NYrS7q/kN+Q0fDG3giQkGE =
              =5Thw
              -----END PGP SIGNATURE-----

              Comment

              • Mark Relly

                #8
                Re: Unicode In C#?

                Thanks a lot Ray I'll try that.

                Cheers!

                Mark

                *** Sent via Developersdex http://www.developersdex.com ***
                Don't just participate in USENET...get rewarded for it!

                Comment

                • Ray Hsieh (Ray Djajadinata)

                  #9
                  Re: Unicode In C#?

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

                  Um, of course the "inserting nulls between characters" is just to prove
                  that the byte array is not unicode encoded since you did this:

                  byteArray = Encoding.ASCII. GetBytes()

                  :) in general you'd want to just make sure that that byte array's
                  content is unicode encoded though.


                  Mark Relly wrote:
                  | Thanks a lot Ray I'll try that.
                  |
                  | Cheers!
                  |
                  | Mark
                  |
                  | *** Sent via Developersdex http://www.developersdex.com ***
                  | Don't just participate in USENET...get rewarded for it!


                  - --
                  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/oa5RwEwccQ4rWPg RAu3qAJ0fKChECJ ivgSNbBqquoxj+A Nh6jgCffS1r
                  9hy+xcJO8iibeMO MCeVBcm0=
                  =/4oV
                  -----END PGP SIGNATURE-----

                  Comment

                  Working...