converting VB asc() to C# Code

tim 160 Reputation points
2024-09-10T13:39:36.8566667+00:00

I'm trying to convert the following VB code to C#

I'm stuck on the C# equivalent of VB's ASC

this

//Newstr = Newstr & chr(asc(Mid(str,x,1)) - Asc(Mid(key,KeyPos,1)))

to this

Newstr = Newstr & Convert.ToChar(asc(reverseString.Substring(x, 1)) - asc(key.Substring( KeyPos, 1)));

I assume that I converted CHR correctly.

TIA

Tim

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,858 questions
{count} votes

Accepted answer
  1. Jiale Xue - MSFT 44,751 Reputation points Microsoft Vendor
    2024-09-10T14:01:56.9266667+00:00

    Hi @tim , Welcome to Microsoft Q&A,

    In C#, the Asc function from VB can be replaced with Convert.ToInt32(char) when working with characters, as Asc returns the ASCII (or Unicode) value of a character.

    VB Code:

    
    Newstr = Newstr & Chr(Asc(Mid(str,x,1)) - Asc(Mid(key,KeyPos,1)))
    
    

    C# Conversion:

    
    Newstr += Convert.ToChar((int)reverseString.Substring(x, 1)[0] - (int)key.Substring(KeyPos, 1)[0]);
    
    

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.