What is the C# equivalent of vb Chr() function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DragonLord
    New Member
    • Mar 2007
    • 122

    What is the C# equivalent of vb Chr() function

    I am trying to convert an application from VB to C# and running into the classic problem of trying to replicate the VB Chr() function with little success.. let me get across two facts and I hope it doesn't sound rude...but....

    1. No I don't want to use the Visual Basic Dll the whole purpose of porting it to C# is to avoid using VB.

    2. I have tried (char) and Convert.ToChar( ) they do work but not quite the same. Let me explain...

    In VB if i do Chr(8) I get back this little black rectangle with a white circle.... yet to figure out what character set this is from..certainly not ASCII...

    In C# Convert.ToChar( 8) returns /b which is incorrect in my applications functionality and when it tries to read this character it throws an invalid format exception...

    Could anyone help so that I can use Encoding of some type to find the exact same character that VB throws back... Oh I have tried Encoding.Defaul t.GetBytes()
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    ASCII/Char 8 is a backspace. So /b is correct.

    A black rectangle with white circle?
    Post a picture so we can see what you see.

    Comment

    • DragonLord
      New Member
      • Mar 2007
      • 122

      #3
      I know it right, but it is not right character set here is what i see.. VB is obviously using a different Encoding but I can't seem to find out what.
      Attached Files

      Comment

      • Monomachus
        Recognized Expert New Member
        • Apr 2008
        • 127

        #4
        Originally posted by DragonLord
        I am trying to convert an application from VB to C# and running into the classic problem of trying to replicate the VB Chr() function with little success.. let me get across two facts and I hope it doesn't sound rude...but....

        1. No I don't want to use the Visual Basic Dll the whole purpose of porting it to C# is to avoid using VB.

        2. I have tried (char) and Convert.ToChar( ) they do work but not quite the same. Let me explain...

        In VB if i do Chr(8) I get back this little black rectangle with a white circle.... yet to figure out what character set this is from..certainly not ASCII...

        In C# Convert.ToChar( 8) returns /b which is incorrect in my applications functionality and when it tries to read this character it throws an invalid format exception...

        Could anyone help so that I can use Encoding of some type to find the exact same character that VB throws back... Oh I have tried Encoding.Defaul t.GetBytes()
        Well I found this one Chr (VB.NET) vs char (C#)

        Here The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)

        I found this phrase which indicates that VB native string encoding is Unicode

        [...], we decided to do everything internally in UCS-2 (two byte) Unicode, which is what Visual Basic, COM, and Windows NT/2000/XP use as their native string type.
        And here Strings in .NET and C# author talks about unicode being default in general .NET

        Encoding

        As stated at the start of the article, strings are always in Unicode encoding. The idea of "a Big-5 string" or "a string in UTF-8 encoding" is a mistake (as far as .NET is concerned) and usually indicates a lack of understanding of either encodings or the way .NET handles strings. It's very important to understand this - treating a string as if it represented some valid text in a non-Unicode encoding is almost always a mistake.
        Hope it helps or at least gives you the directions to go into.

        P.S. Is it this one http://www.fileformat.info/info/unic...25d8/index.htm ?

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          If you have a byte value of 8 (0x08) then in ASCII you will get the backspace character. Which is escaped with a \b (just like the tab character is escaped with \t ) The character itself is still the same, its just how it is presented to you.
          Odd that string data would use such non-printable characters.
          Are you sure you shouldn't be doing this in a byte[] (byte array)?

          Comment

          • DragonLord
            New Member
            • Mar 2007
            • 122

            #6
            It is the same but it is not the same, when you try to read the value back and translate it then it doesn't recognize the \b because it is reading one encoding and you are passing it another. this is why it is important that I discover what encoding the vb Chr() function is using so that i can match it so the rest of the code works.... It is funny that i read many forums and the answers i am getting here are all the same.

            The common ones are...

            1. Use the visual basic dll and use the Chr() - kinda defeats the point of porting it to C#

            2. 8 is 8 yup no kidding....took alot of thought for that one i bet...

            3. \b is correct.... never said it was wrong just not right for me lol....

            4. use the VB program if it works......ya well we could debate that one for hours.

            5. do it a different way...... what no body wants to answer the question?

            I really appreciate all the help you guys are trying to help with unfortunately all the suggestion while excellent comments are already used and I was really hoping to find out which character set uses the shape above as the representation for 8. lol. cause then i could change the decoder to read it properly or at least know how to switch the encoder and decoder properly to use the \b.

            Does anyone know what that symbol comes from where that symbol is the representation of '8'. Unicode isn't it ASCII isn't it Default on my laptop seems to be ASCII, and unfortunately the above links unfortunately gives me another character set but '8' is a space in that one.

            I know this reply might sound a bit snarky but I really do appreciate that you guys have at least tried, just frustrated that after so many years of people converting the whole Chr() thingy is just still so much of a mystery.

            Comment

            • DragonLord
              New Member
              • Mar 2007
              • 122

              #7
              P.s I am really picking on the fact that every other post i read on google had the answers above... it just happens that here a couple happen to be the same as those but you guys put them alot nicer than some other people replying to similar posts.

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                It had been my assumption that Chr() just converted an int into a character using ascii encoding. But vb did everything is an "odd" sort of way.

                Maybe you can find something here:

                Comment

                • DragonLord
                  New Member
                  • Mar 2007
                  • 122

                  #9
                  OH YAHHHH I know what VB Chr() is doing now only I don't know how to replicate it in code.

                  if you open up notepad and you hold down your alt. key and press a number on the number pad key then let it go you will get a ascii code map character.

                  ie. alt 1 lift alt you get ☺
                  alt 2 lift alt you get ☻
                  and....
                  here it comes...
                  alt 8 lift alt you get ◘

                  this is exactly what VB Chr() function returns instead of \b it returns ◘ ... can anyone help me replicate this function in C#

                  Comment

                  • DragonLord
                    New Member
                    • Mar 2007
                    • 122

                    #10
                    This is exactly what i want returned back http://www.tedmontgomery.com/tutorial/altchrc.html

                    Comment

                    • tlhintoq
                      Recognized Expert Specialist
                      • Mar 2008
                      • 3532

                      #11
                      Monomachus already pointed you at the page for that character.


                      It is unicode 25d8
                      It is ASCII 8
                      Reproducable as "\u25d8"

                      Your box with a circle *is* a backspace. alt+8 is how you create the character since you can't hit the backspace key on your keyboard and have it sent: The keyboard reacts to the backspace key by backing up one space. You can't display a backspace, so in a font that can display this character, you see a box with a circle. Its all the same thing.

                      You aren't catching that "12 inches" and "1 foot" are the same thing.

                      Comment

                      • DragonLord
                        New Member
                        • Mar 2007
                        • 122

                        #12
                        I asure you I am far from stupid, and I really don't appreciate your comment.

                        "You aren't catching that "12 inches" and "1 foot" are the same thing. "

                        it is not hard to understand 8 is 8 ... what i am saying is that i would like to have a call pass in to a method and have it return the character code image that is the equivalent of alt + 8 not the backspace or \ b but the pretty little black box with a circle.

                        so If anyone can tell me how to programmaticly return alt+ the value as an image i would appreciate the help.

                        Comment

                        • Plater
                          Recognized Expert Expert
                          • Apr 2007
                          • 7872

                          #13
                          The "call" appears to be just a difference in encoding and perhaps font.

                          Comment

                          • DragonLord
                            New Member
                            • Mar 2007
                            • 122

                            #14
                            Ya I have never done it before so I don't even have a clue what to do.

                            Comment

                            • DragonLord
                              New Member
                              • Mar 2007
                              • 122

                              #15
                              Use ALT Shift Codes programatically

                              I would like to know if anyone knows how to creates a string programatically containing the alt shift codes of ascii values...here is the example.

                              http://indika.info/misc/special-alt-characters/

                              you can replicate this in notepad by holding down the ALT key press a number between 1 and 255 then release the ALT key.

                              I would like to pass in a value to a method and have it return back the equivalent as done above.

                              So if I pass in 8 it will return ◘ does anyone know how to do this?

                              Comment

                              Working...