Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. String implementation in .NET Framework

String implementation in .NET Framework

Scheduled Pinned Locked Moved C#
csharpdotnetquestion
10 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    J4amieC
    wrote on last edited by
    #1

    I have some legacy VB6 code which im converting over to C#, and it seems that strings were implemented very differently in old VB than in the Framework. I get vastly different results from the ported code than I do from an app written in VB and I was wondering if anyone could put their finger on the reason why? here is the old VB code (pstrResponse is the input to the method):

    For i = 1 To Len(pstrResponse)

      j = Asc(Mid(pstrResponse, i, 1))
    
      If j > 127 Then
    
         j = j - 128
    
         Mid(pstrResponse, i, 1) = Chr(j)
    
      End If
    

    Next i

    Here is my ported C# (input is the input):

    char[] charArray = input.ToCharArray();
    int bit;
    for (int i = 0; i < charArray.Length; i++)
    {
    bit = (int)charArray[i];
    if (bit > 127)
    {
    bit -= 128;
    charArray[i] = (char)bit;
    }
    }
    return new string(charArray);

    As you can see, simple code & easy to port but very different results. Any ideas people?

    E P 2 Replies Last reply
    0
    • J J4amieC

      I have some legacy VB6 code which im converting over to C#, and it seems that strings were implemented very differently in old VB than in the Framework. I get vastly different results from the ported code than I do from an app written in VB and I was wondering if anyone could put their finger on the reason why? here is the old VB code (pstrResponse is the input to the method):

      For i = 1 To Len(pstrResponse)

        j = Asc(Mid(pstrResponse, i, 1))
      
        If j > 127 Then
      
           j = j - 128
      
           Mid(pstrResponse, i, 1) = Chr(j)
      
        End If
      

      Next i

      Here is my ported C# (input is the input):

      char[] charArray = input.ToCharArray();
      int bit;
      for (int i = 0; i < charArray.Length; i++)
      {
      bit = (int)charArray[i];
      if (bit > 127)
      {
      bit -= 128;
      charArray[i] = (char)bit;
      }
      }
      return new string(charArray);

      As you can see, simple code & easy to port but very different results. Any ideas people?

      E Offline
      E Offline
      Ennis Ray Lynch Jr
      wrote on last edited by
      #2

      .NET stores string in Unicode not Ascii and a char is 2 bytes not one I think? Which would mean the -=128 could have a different value in an integer data type? I don't really know I am just speculating.

      Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
      Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
      Most of this sig is for Google, not ego.

      J 1 Reply Last reply
      0
      • E Ennis Ray Lynch Jr

        .NET stores string in Unicode not Ascii and a char is 2 bytes not one I think? Which would mean the -=128 could have a different value in an integer data type? I don't really know I am just speculating.

        Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
        Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
        Most of this sig is for Google, not ego.

        J Offline
        J Offline
        J4amieC
        wrote on last edited by
        #3

        Ennis Ray Lynch, Jr. wrote:

        Which would mean the -=128 could have a different value in an integer data type

        Any idea what it could be?

        E D 2 Replies Last reply
        0
        • J J4amieC

          Ennis Ray Lynch, Jr. wrote:

          Which would mean the -=128 could have a different value in an integer data type

          Any idea what it could be?

          E Offline
          E Offline
          Ennis Ray Lynch Jr
          wrote on last edited by
          #4

          Maybe, Asc(Mid(pstrResponse, i, 1)) != (int)characters[i]; Try using the System.Text.AsciiEncoder to change the string by ascii bytes only first and see what happens?

          Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
          Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
          Most of this sig is for Google, not ego.

          J 1 Reply Last reply
          0
          • J J4amieC

            Ennis Ray Lynch, Jr. wrote:

            Which would mean the -=128 could have a different value in an integer data type

            Any idea what it could be?

            D Offline
            D Offline
            DaveyM69
            wrote on last edited by
            #5

            Apparently this still exists in the Microsoft.VisualBasic namespace. This is the Asc function after being run through reflector - it seems to work OK.

            public static int Asc(char theChar)
            {
                int num;
                int num2 = Convert.ToInt32(theChar);
                if (num2 < 0x80)
                {
                    return num2;
                }
                try
                {
                    byte\[\] buffer;
                    Encoding fileIOEncoding = Encoding.Default;
                    char\[\] chars = new char\[\] { theChar };
                    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.IsLittleEndian)
                    {
                        byte num4 = buffer\[0\];
                        buffer\[0\] = buffer\[1\];
                        buffer\[1\] = num4;
                    }
                    num = BitConverter.ToInt16(buffer, 0);
                }
                catch (Exception exception)
                {
                    throw exception;
                }
                return num;
            }
            

            Dave
            BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
            Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

            J 1 Reply Last reply
            0
            • J J4amieC

              I have some legacy VB6 code which im converting over to C#, and it seems that strings were implemented very differently in old VB than in the Framework. I get vastly different results from the ported code than I do from an app written in VB and I was wondering if anyone could put their finger on the reason why? here is the old VB code (pstrResponse is the input to the method):

              For i = 1 To Len(pstrResponse)

                j = Asc(Mid(pstrResponse, i, 1))
              
                If j > 127 Then
              
                   j = j - 128
              
                   Mid(pstrResponse, i, 1) = Chr(j)
              
                End If
              

              Next i

              Here is my ported C# (input is the input):

              char[] charArray = input.ToCharArray();
              int bit;
              for (int i = 0; i < charArray.Length; i++)
              {
              bit = (int)charArray[i];
              if (bit > 127)
              {
              bit -= 128;
              charArray[i] = (char)bit;
              }
              }
              return new string(charArray);

              As you can see, simple code & easy to port but very different results. Any ideas people?

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              J4amieC wrote:

              As you can see, simple code & easy to port but very different results

              No, I can't see; what are the results? Provided you don't care about Unicode, you could use: System.Text.Encoding.ASCII.GetBytes() and System.Text.Encoding.ASCII.GetString() You could also access the characters in an unsafe context much like your VB code. Something like:

              private unsafe static void
              ASCIIize
              (
              string Subject
              )
              {
              fixed ( char* subject = Subject )
              {
              int* length = (int*) subject - 1 ;
              int index = 0 ;

                  while ( index < \*length )
                  {
                      \*(subject + index) &= (char) 0x7F ;
              
                      index++ ;
                  }
              }
              
              return ;
              

              }

              (Though I haven't tested this with the problematic characters.)

              J 1 Reply Last reply
              0
              • E Ennis Ray Lynch Jr

                Maybe, Asc(Mid(pstrResponse, i, 1)) != (int)characters[i]; Try using the System.Text.AsciiEncoder to change the string by ascii bytes only first and see what happens?

                Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
                Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
                Most of this sig is for Google, not ego.

                J Offline
                J Offline
                J4amieC
                wrote on last edited by
                #7

                Thanks for the pointers. With your help ive managed to solve the problem, but strangly enough I thought id need to convert my input to ASCII using ASCIIEncoding.GetBytes. In actual fact I had to use DefaultEncoding.GetBytes to get the result I was expecting. :confused: here's the working code now:

                byte[] byteArray = Encoding.Default.GetBytes(input);

                StringBuilder sb = new StringBuilder(byteArray.Length);
                byte bit;
                for (int i = 0; i < byteArray.Length; i++)
                {
                bit = (byte)byteArray[i];
                if (bit > 127)
                {
                bit -= 128;
                }
                sb.Append((char)bit);
                }
                return sb.ToString();

                Thanks again for your help.

                1 Reply Last reply
                0
                • D DaveyM69

                  Apparently this still exists in the Microsoft.VisualBasic namespace. This is the Asc function after being run through reflector - it seems to work OK.

                  public static int Asc(char theChar)
                  {
                      int num;
                      int num2 = Convert.ToInt32(theChar);
                      if (num2 < 0x80)
                      {
                          return num2;
                      }
                      try
                      {
                          byte\[\] buffer;
                          Encoding fileIOEncoding = Encoding.Default;
                          char\[\] chars = new char\[\] { theChar };
                          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.IsLittleEndian)
                          {
                              byte num4 = buffer\[0\];
                              buffer\[0\] = buffer\[1\];
                              buffer\[1\] = num4;
                          }
                          num = BitConverter.ToInt16(buffer, 0);
                      }
                      catch (Exception exception)
                      {
                          throw exception;
                      }
                      return num;
                  }
                  

                  Dave
                  BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                  Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

                  J Offline
                  J Offline
                  J4amieC
                  wrote on last edited by
                  #8

                  Yeah, I saw that but I was attempting to avoid the use of nasty VB namespaces X|

                  D 1 Reply Last reply
                  0
                  • P PIEBALDconsult

                    J4amieC wrote:

                    As you can see, simple code & easy to port but very different results

                    No, I can't see; what are the results? Provided you don't care about Unicode, you could use: System.Text.Encoding.ASCII.GetBytes() and System.Text.Encoding.ASCII.GetString() You could also access the characters in an unsafe context much like your VB code. Something like:

                    private unsafe static void
                    ASCIIize
                    (
                    string Subject
                    )
                    {
                    fixed ( char* subject = Subject )
                    {
                    int* length = (int*) subject - 1 ;
                    int index = 0 ;

                        while ( index < \*length )
                        {
                            \*(subject + index) &= (char) 0x7F ;
                    
                            index++ ;
                        }
                    }
                    
                    return ;
                    

                    }

                    (Though I haven't tested this with the problematic characters.)

                    J Offline
                    J Offline
                    J4amieC
                    wrote on last edited by
                    #9

                    PIEBALDconsult wrote:

                    No, I can't see; what are the results?

                    The results are cryptic, horrible and mostly non-printable characters which is why I didnt post them. I meant the code was easy enough to port on the face of it.

                    1 Reply Last reply
                    0
                    • J J4amieC

                      Yeah, I saw that but I was attempting to avoid the use of nasty VB namespaces X|

                      D Offline
                      D Offline
                      DaveyM69
                      wrote on last edited by
                      #10

                      J4amieC wrote:

                      nasty VB

                      :laugh: If you use the code above you don't need to - it's just that function converted to C#

                      Dave
                      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                      Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups