C# equivelant of VB.NET's Asc()

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

    C# equivelant of VB.NET's Asc()

    VB.NET has a function, Asc(), that gets the Ascii value of a character. I am
    attempting to find an equivelant function for C#. Can somebody help me here?
    --
    Nathan Sokalski
    njsokalski@hotm ail.com
    有声小说网为广大读者提供热门小说在线免费阅读,本站收集的网络文学小说情节跌宕起伏,有声小说网是值得书友们收藏的小说在线阅读网。



  • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

    #2
    Re: C# equivelant of VB.NET's Asc()

    Nathan Sokalski wrote:
    VB.NET has a function, Asc(), that gets the Ascii value of a character. I am
    attempting to find an equivelant function for C#. Can somebody help me here?
    (int)c

    Arne

    PS: I believe Asc is a VB6'ism.

    Comment

    • Mr. Arnold

      #3
      Re: C# equivelant of VB.NET's Asc()


      "Arne Vajhøj" <arne@vajhoej.d kwrote in message
      news:48659a67$0 $90274$14726298 @news.sunsite.d k...
      Nathan Sokalski wrote:
      >VB.NET has a function, Asc(), that gets the Ascii value of a character. I
      >am attempting to find an equivelant function for C#. Can somebody help me
      >here?
      >
      (int)c
      >
      Arne
      >
      PS: I believe Asc is a VB6'ism.
      Asc() can still be used VB.Net. It was in VB3-VB6 and QuickBasic too.

      Comment

      • Mr. Arnold

        #4
        Re: C# equivelant of VB.NET's Asc()


        "Fred" <foleide@free.f r.invalidwrote in message
        news:ePaA8NO2IH A.6096@TK2MSFTN GP06.phx.gbl...
        Dans : news:OSp7aGO2IH A.5512@TK2MSFTN GP06.phx.gbl,
        Mr. Arnold écrivait :
        >"Arne Vajhøj" <arne@vajhoej.d kwrote in message
        >news:48659a67$ 0$90274$1472629 8@news.sunsite. dk...
        >>Nathan Sokalski wrote:
        >>>VB.NET has a function, Asc(), that gets the Ascii value of a
        >>>character. I am attempting to find an equivelant function for C#.
        >>>Can somebody help me here?
        >>>
        >>(int)c
        >>>
        >>Arne
        >>>
        >>PS: I believe Asc is a VB6'ism.
        >>
        >Asc() can still be used VB.Net. It was in VB3-VB6 and QuickBasic too.
        >
        But it's not equivalent to Arne's solution as it returns windows default
        encoding character code (not Unicode value)
        >
        What does that have to do with anything? It's not a VB6'ism is all that was
        being pointed out here and nothing else.

        Comment

        • Mr. Arnold

          #5
          Re: C# equivelant of VB.NET's Asc()


          "Fred" <foleide@free.f r.invalidwrote in message
          news:%233qn7QO2 IHA.4772@TK2MSF TNGP03.phx.gbl. ..

          <snipped>

          Yeah -- yeah don't be going off the deep-end with this.

          Comment

          • Michel Posseth  [MCP]

            #6
            Re: C# equivelant of VB.NET's Asc()

            '"I believe Asc is a VB6'ism"

            It would have been if you needed to set a reference to the
            Microsoft.Visua lBasic.Compatib ility namespace
            (Microsoft.Visu alBasic.Compati blity.dll)
            please note that if you do not need to set this reference you are working
            with the Microsoft.Visua lBasic namespace wich is for current Visual Basic
            ..NET programs , it is a mamanged library that is part of the framework just
            as system.data for instance, It is even possible to set a reference to the
            VB namespace in C# and thus use all the VB shortcut methods in C#

            hth

            Michel Posseth
            ..



            "Arne Vajhøj" <arne@vajhoej.d kschreef in bericht
            news:48659a67$0 $90274$14726298 @news.sunsite.d k...
            Nathan Sokalski wrote:
            >VB.NET has a function, Asc(), that gets the Ascii value of a character. I
            >am attempting to find an equivelant function for C#. Can somebody help me
            >here?
            >
            (int)c
            >
            Arne
            >
            PS: I believe Asc is a VB6'ism.

            Comment

            • Cor Ligthert[MVP]

              #7
              Re: C# equivelant of VB.NET's Asc()

              Nathan-

              \\\
              int a = Convert.ToInt32 ('A');
              int b = 'A';
              ///

              -Cor

              "Nathan Sokalski" <njsokalski@hot mail.comschreef in bericht
              news:%23nV5IFM2 IHA.1236@TK2MSF TNGP02.phx.gbl. ..
              VB.NET has a function, Asc(), that gets the Ascii value of a character. I
              am attempting to find an equivelant function for C#. Can somebody help me
              here?
              --
              Nathan Sokalski
              njsokalski@hotm ail.com
              有声小说网为广大读者提供热门小说在线免费阅读,本站收集的网络文学小说情节跌宕起伏,有声小说网是值得书友们收藏的小说在线阅读网。

              >

              Comment

              • Joergen Bech

                #8
                Re: C# equivelant of VB.NET's Asc()

                On Fri, 27 Jun 2008 21:56:55 -0400, Arne Vajhøj <arne@vajhoej.d k>
                wrote:
                >Nathan Sokalski wrote:
                >VB.NET has a function, Asc(), that gets the Ascii value of a character. I am
                >attempting to find an equivelant function for C#. Can somebody help me here?
                >
                >(int)c
                >
                >Arne
                >
                >PS: I believe Asc is a VB6'ism.
                If you *know* that your character is in the 0x00-0x7f range,
                (int)c will do just fine.

                But as you can see from using .Net Reflector (below), the full
                Asc(char) function is a little more elaborate than that.

                Regards,

                Joergen Bech

                ---snip---

                public static int Asc(char String)
                {
                int num;
                int num2 = Convert.ToInt32 (String);
                if (num2 < 0x80)
                {
                return num2;
                }
                try
                {
                byte[] buffer;
                Encoding fileIOEncoding = Utils.GetFileIO Encoding();
                char[] chars = new char[] { String };
                if (fileIOEncoding .IsSingleByte)
                {
                buffer = new byte[1];
                int num3 = fileIOEncoding. GetBytes(chars, 0, 1, buffer,
                0);
                return buffer[0];
                }
                buffer = new byte[2];
                if (fileIOEncoding .GetBytes(chars , 0, 1, buffer, 0) == 1)
                {
                return buffer[0];
                }
                if (BitConverter.I sLittleEndian)
                {
                byte num4 = buffer[0];
                buffer[0] = buffer[1];
                buffer[1] = num4;
                }
                num = BitConverter.To Int16(buffer, 0);
                }
                catch (Exception exception)
                {
                throw exception;
                }
                return num;
                }





                Comment

                • Herfried K. Wagner [MVP]

                  #9
                  Re: C# equivelant of VB.NET's Asc()

                  "Nathan Sokalski" <njsokalski@hot mail.comschrieb :
                  VB.NET has a function, Asc(), that gets the Ascii value of a character. I
                  am attempting to find an equivelant function for C#. Can somebody help me
                  here?
                  'Asc' returns the Windows ANSI character code depending on the system's
                  Windows ANSI codepage. Is this really what you want to do?

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

                  Comment

                  • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

                    #10
                    Re: C# equivelant of VB.NET's Asc()

                    Fred wrote:
                    Dans : news:ePaA8NO2IH A.6096@TK2MSFTN GP06.phx.gbl,
                    Fred écrivait :
                    >Dans : news:OSp7aGO2IH A.5512@TK2MSFTN GP06.phx.gbl,
                    >Mr. Arnold écrivait :
                    >>"Arne Vajhøj" <arne@vajhoej.d kwrote in message
                    >>news:48659a67 $0$90274$147262 98@news.sunsite .dk...
                    >>>Nathan Sokalski wrote:
                    >>>>VB.NET has a function, Asc(), that gets the Ascii value of a
                    >>>>character . I am attempting to find an equivelant function for C#.
                    >>>>Can somebody help me here?
                    >>>>
                    >>>(int)c
                    >>>>
                    >>>PS: I believe Asc is a VB6'ism.
                    >>>
                    >>Asc() can still be used VB.Net. It was in VB3-VB6 and QuickBasic
                    >>too.
                    >>
                    >But it's not equivalent to Arne's solution as it returns windows
                    >default encoding character code (not Unicode value)
                    >
                    PS : Arne's solution is equivalent to AscW
                    Good point. There is a difference. I think chances are reasonable
                    good that the original poster want the Unicode value. But it is
                    obviously something he needs to be aware of.

                    Arne

                    Comment

                    • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

                      #11
                      Re: C# equivelant of VB.NET's Asc()

                      Michel Posseth [MCP] wrote:
                      '"I believe Asc is a VB6'ism"
                      >
                      It would have been if you needed to set a reference to the
                      Microsoft.Visua lBasic.Compatib ility namespace
                      (Microsoft.Visu alBasic.Compati blity.dll)
                      please note that if you do not need to set this reference you are working
                      with the Microsoft.Visua lBasic namespace wich is for current Visual Basic
                      .NET programs , it is a mamanged library that is part of the framework just
                      as system.data for instance, It is even possible to set a reference to the
                      VB namespace in C# and thus use all the VB shortcut methods in C#
                      OK.

                      But to me it is still a function that only exists for compatibility
                      reasons and is a procedural leftover in an object oriented world.

                      But that is of course not a technical view.

                      Arne

                      Comment

                      • Nathan Sokalski

                        #12
                        Re: C# equivelant of VB.NET's Asc()

                        No, I do not want the Unicode value. I do know the difference between Asc()
                        and AscW() (Although I do appreciate that you took into account that it is
                        an easy mistake to make), and I am looking for the equivelant of Asc(). My
                        planned use is for generating JavaScript (You can see the VB.NET version of
                        the code I am trying to convert on my site at
                        http://www.nathansokalski.com/code/R...utMethod.aspx).
                        --
                        Nathan Sokalski
                        njsokalski@hotm ail.com
                        有声小说网为广大读者提供热门小说在线免费阅读,本站收集的网络文学小说情节跌宕起伏,有声小说网是值得书友们收藏的小说在线阅读网。


                        "Arne Vajhøj" <arne@vajhoej.d kwrote in message
                        news:48665320$0 $90270$14726298 @news.sunsite.d k...
                        Fred wrote:
                        >Dans : news:ePaA8NO2IH A.6096@TK2MSFTN GP06.phx.gbl,
                        >Fred écrivait :
                        >>Dans : news:OSp7aGO2IH A.5512@TK2MSFTN GP06.phx.gbl,
                        >>Mr. Arnold écrivait :
                        >>>"Arne Vajhøj" <arne@vajhoej.d kwrote in message
                        >>>news:48659a6 7$0$90274$14726 298@news.sunsit e.dk...
                        >>>>Nathan Sokalski wrote:
                        >>>>>VB.NET has a function, Asc(), that gets the Ascii value of a
                        >>>>>characte r. I am attempting to find an equivelant function for C#.
                        >>>>>Can somebody help me here?
                        >>>>>
                        >>>>(int)c
                        >>>>>
                        >>>>PS: I believe Asc is a VB6'ism.
                        >>>>
                        >>>Asc() can still be used VB.Net. It was in VB3-VB6 and QuickBasic
                        >>>too.
                        >>>
                        >>But it's not equivalent to Arne's solution as it returns windows
                        >>default encoding character code (not Unicode value)
                        >>
                        >PS : Arne's solution is equivalent to AscW
                        >
                        Good point. There is a difference. I think chances are reasonable
                        good that the original poster want the Unicode value. But it is
                        obviously something he needs to be aware of.
                        >
                        Arne

                        Comment

                        • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

                          #13
                          Re: C# equivelant of VB.NET's Asc()

                          Joergen Bech <jbech<NOSPAM >@ wrote:
                          On Fri, 27 Jun 2008 21:56:55 -0400, Arne Vajhøj <arne@vajhoej.d k>
                          wrote:
                          >Nathan Sokalski wrote:
                          >>VB.NET has a function, Asc(), that gets the Ascii value of a character. I am
                          >>attempting to find an equivelant function for C#. Can somebody help me here?
                          >(int)c
                          If you *know* that your character is in the 0x00-0x7f range,
                          (int)c will do just fine.
                          Or if he wants unicode, which you normally would in .NET ...
                          But as you can see from using .Net Reflector (below), the full
                          Asc(char) function is a little more elaborate than that.
                          public static int Asc(char String)
                          {
                          int num;
                          int num2 = Convert.ToInt32 (String);
                          if (num2 < 0x80)
                          {
                          return num2;
                          }
                          try
                          {
                          byte[] buffer;
                          Encoding fileIOEncoding = Utils.GetFileIO Encoding();
                          char[] chars = new char[] { String };
                          if (fileIOEncoding .IsSingleByte)
                          {
                          buffer = new byte[1];
                          int num3 = fileIOEncoding. GetBytes(chars, 0, 1, buffer,
                          0);
                          return buffer[0];
                          }
                          buffer = new byte[2];
                          if (fileIOEncoding .GetBytes(chars , 0, 1, buffer, 0) == 1)
                          {
                          return buffer[0];
                          }
                          if (BitConverter.I sLittleEndian)
                          {
                          byte num4 = buffer[0];
                          buffer[0] = buffer[1];
                          buffer[1] = num4;
                          }
                          num = BitConverter.To Int16(buffer, 0);
                          }
                          catch (Exception exception)
                          {
                          throw exception;
                          }
                          return num;
                          }
                          Yuck - a piece of code.

                          Arne

                          Comment

                          • Nathan Sokalski

                            #14
                            Re: C# equivelant of VB.NET's Asc()

                            Yes, it is, I have completely tested the VB.NET version and everything works
                            perfectly and as I expected.
                            --
                            Nathan Sokalski
                            njsokalski@hotm ail.com
                            有声小说网为广大读者提供热门小说在线免费阅读,本站收集的网络文学小说情节跌宕起伏,有声小说网是值得书友们收藏的小说在线阅读网。


                            "Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.atwrot e in message
                            news:%23ekHP9R2 IHA.4772@TK2MSF TNGP03.phx.gbl. ..
                            "Nathan Sokalski" <njsokalski@hot mail.comschrieb :
                            >VB.NET has a function, Asc(), that gets the Ascii value of a character. I
                            >am attempting to find an equivelant function for C#. Can somebody help me
                            >here?
                            >
                            'Asc' returns the Windows ANSI character code depending on the system's
                            Windows ANSI codepage. Is this really what you want to do?
                            >
                            --
                            M S Herfried K. Wagner
                            M V P <URL:http://dotnet.mvps.org/>
                            V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

                            Comment

                            • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

                              #15
                              Re: C# equivelant of VB.NET's Asc()

                              Nathan Sokalski wrote:
                              No, I do not want the Unicode value. I do know the difference between Asc()
                              and AscW() (Although I do appreciate that you took into account that it is
                              an easy mistake to make), and I am looking for the equivelant of Asc(). My
                              planned use is for generating JavaScript (You can see the VB.NET version of
                              the code I am trying to convert on my site at
                              http://www.nathansokalski.com/code/R...utMethod.aspx).
                              Unless you have to support some legacy ASP stuff, then I would
                              recommend going Unicode internally and UTF-8 externally
                              for an ASP.NET web app.

                              Arne

                              Comment

                              Working...