function Chr()

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

    function Chr()

    Hello,

    The function Microsoft.Visua lBasic.Chr( ), is there a similar function in
    System.Text ? How does this work?

    Thank you,

    me.


  • Samuel R. Neff

    #2
    Re: function Chr()


    If you put a hard-coded value inside Chr() such as

    Chr(65)

    then the compiler will remove the Chr() call and replace it with a
    constant:

    "A"c

    If the argument is a variable, then it compiles it to a call to
    Strings.Chr() inside Microsoft.Visua lBasic.

    For character codes 0-127, then Chr() is the same as Convert.ToChar( ).

    Dim i as Integer = 65
    Dim c as Char = Chr(i)

    is the same as

    Dim i as Integer = 65
    Dim c as Char = Convert.ToChar( i)

    For higher characters, Chr() does some complicated stuff with unicode
    character resolution and the current code page.

    HTH,

    Sam


    On Wed, 15 Dec 2004 20:35:57 +0100, "Elephant"
    <elephant@micro soft.com> wrote:
    [color=blue]
    >Hello,
    >
    >The function Microsoft.Visua lBasic.Chr( ), is there a similar function in
    >System.Text ? How does this work?
    >
    >Thank you,
    >
    >me.
    >[/color]

    Comment

    • Herfried K. Wagner [MVP]

      #3
      Re: function Chr()

      "Samuel R. Neff" <blinex@newsgro up.nospam> schrieb:[color=blue]
      > For higher characters, Chr() does some complicated stuff with unicode
      > character resolution and the current code page.[/color]

      In addition to that, VB provides a 'ChrW' function too.

      --
      M S Herfried K. Wagner
      M V P <URL:http://dotnet.mvps.org/>
      V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

      Comment

      • Samuel R. Neff

        #4
        Re: function Chr()


        I was wondering about that.. what's the differene between passing a[color=blue]
        >127 unicode character to Chr() and ChrW()? Chr() does lots of extra[/color]
        processing and ChrW() just passes it along to Convert.ToChar( ).
        What's this extra processing do and when would one use Chr() over
        ChrW()?

        Thanks,

        Sam


        On Wed, 15 Dec 2004 21:14:32 +0100, "Herfried K. Wagner [MVP]"
        <hirf-spam-me-here@gmx.at> wrote:
        [color=blue]
        >"Samuel R. Neff" <blinex@newsgro up.nospam> schrieb:[color=green]
        >> For higher characters, Chr() does some complicated stuff with unicode
        >> character resolution and the current code page.[/color]
        >
        >In addition to that, VB provides a 'ChrW' function too.[/color]

        Comment

        • Jay B. Harlow [MVP - Outlook]

          #5
          Re: function Chr()

          Elephant,
          In addition to the other comments.

          As Samuel stated: Chr does Ansi encoding using the current code page, while
          ChrW does Unicode encoding.

          ChrW & Convert.ToChar do not need to use an Encoding object per se, as the
          integer code point for a character is simply the character. Chr would need
          an encoding object as Samuel stated characters 128 to 255 use the Ansi
          Encoding object for the current code page. I understand that both Chr & ChrW
          handle negative numbers in a consistent manner, where as Convert.ToChar will
          throw an exception.


          Encoding within .NET is handled by the System.Text.Enc oding class.

          Encoding.Defaul t offers an Encoding object that represents the current code
          page, while Encoding.Unicod e offers an Encoding object that represents the
          Unicode character set.

          Normally you use an Encoding object to convert an array of Bytes to & from a
          String. By string I mean both a String or an array of Chars.

          Dim bytes() As Byte
          Dim name As String = "Jay B. Harlow"
          Dim ansi As Encoding = Encoding.Defaul t
          bytes = ansi.GetBytes(n ame)
          name = ansi.GetString( bytes)

          For individual characters using Chr or ChrW is easier.

          Hope this helps
          Jay

          "Elephant" <elephant@micro soft.com> wrote in message
          news:bISdnXhCO7 XeDF3cRVnyjQ@ca sema.nl...[color=blue]
          > Hello,
          >
          > The function Microsoft.Visua lBasic.Chr( ), is there a similar function in
          > System.Text ? How does this work?
          >
          > Thank you,
          >
          > me.
          >
          >[/color]


          Comment

          Working...