Results 1 to 7 of 7

Thread: Chr() and Asc() problem

  1. #1

    Thread Starter
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Chr() and Asc() problem

    I'm creating my own encryption routine. In my function I have a loop that goes through the complete string character by character and finally, concatenating each character (encrypted of course) to the final result (in this case, "x"):
    Code:
    x = x & Chr(SomeCode) 'where "SomeCode" is for example 255 (last ASCII)
    Everything is fine until here. But when this function returns this result ("x"), 255 changes to 63.

    And ideas what am I doing wrong??

  2. #2
    Lively Member
    Join Date
    Apr 2010
    Posts
    105

    Re: Chr() and Asc() problem

    Can you post a more expanded code sample? Also, have you considered:
    A) Not implementing your own encryption method, and using a tried and tested method built into the .NET Framework?
    B) Using a StringBuilder for string appends for performance?

  3. #3

    Thread Starter
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Chr() and Asc() problem

    I have considered A, but still want to do my own

    It's quite a good logic, with this little exception I just can't get.

  4. #4
    Lively Member
    Join Date
    Apr 2010
    Posts
    105

    Re: Chr() and Asc() problem

    Can you post a little more of the code you're using?

  5. #5
    Frenzied Member ntg's Avatar
    Join Date
    Sep 2004
    Posts
    1,449

    Re: Chr() and Asc() problem

    You may want to try Text.ASCIIEncoding.GetEncoding(Globalization.CultureInfo.CurrentCulture.TextInfo.ANSICodePage).GetCh ars() instead of Chr() and Text.ASCIIEncoding.GetEncoding(Globalization.CultureInfo.CurrentCulture.TextInfo.ANSICodePage).GetBy tes() instead of Asc(). I think that Chr and Asc are pretty much culture agnostic and don't do a good job for anything else other than the standard ASCII character set.
    "Feel the force...read the source..."
    Utilities: POPFileDebugViewProcess ExplorerWiresharkKeePassUltraVNCPic2Ascii
    .Net tools & open source: DotNetNukelog4NetCLRProfiler
    My open source projects: Thales SimulatorEFT CalculatorSystem Info ReporterVSS2SVNIBAN Functions
    Customer quote: "If the server has a RAID array, why should we bother with backups?"
    Programmer quote: "I never comment my code. Something that is hard to write should be impossible to comprehend."
    Ignorant quote: "I have no respect for universities, as they teach not practicle stuff, and charge money for"

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Chr() and Asc() problem

    Quote Originally Posted by gavio View Post
    I'm creating my own encryption routine. In my function I have a loop that goes through the complete string character by character and finally, concatenating each character (encrypted of course) to the final result (in this case, "x"):
    Code:
    x = x & Chr(SomeCode) 'where "SomeCode" is for example 255 (last ASCII)
    Everything is fine until here. But when this function returns this result ("x"), 255 changes to 63.

    And ideas what am I doing wrong??
    The standard encoder returns ? (63) for any value greater than 127. You can use windows-1252 (i think that is the one) and get all 256.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Chr() and Asc() problem

    I was wrong. This works:

    Code:
            Dim s As String = ""
            For x As Integer = 0 To 255
                s &= Chr(x)
            Next
    
            For Each c In s
                Debug.Write(c & " ")
                Debug.WriteLine(Asc(c))
            Next
    Last edited by dbasnett; Apr 15th, 2010 at 04:58 PM.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width