What's the Equivalent of Vb's Asc() and Chr() Functions in C#

What's the equivalent of VB's Asc() and Chr() functions in C#?

You could always add a reference to Microsoft.VisualBasic and then use the exact same methods: Strings.Chr and Strings.Asc.

That's the easiest way to get the exact same functionality.

C# Equivalent to VB6 Chr(number)

Feel like taking risk to answer but how about?

string sCenter;
sCenter = "" + (char)27 + (char)97 + (char)1;

Thanks to James Curran's comment about char integral addition. As his suggested, I added meaningless "" with char to get string + char which uses .ToString() with String.Concat method at background.

C# Char from Int used as String - the real equivalent of VB Chr()

You are opening Pandora's box with this question. Chr() is a legacy function in VB.NET, any modern code should be using ChrW() instead. The difference is the way the character value should be interpreted, ChrW() assumes the character code is Unicode (W = wide). Chr() rolls back the clock to the previous century, a stone age without Unicode where characters were either in the ASCII character set (0..127) or an "extended" character (128..255). Where the extended characters belong to a code page. Many, many different code pages were in common use. A very significant disaster, programs could not properly interpret text that was generated by another machine located in a different country. Or even in the same country, Japan had multiple code pages in common use with none of them dominant. Producing mojibake.

I'll assume you mean ChrW(), nobody likes mojibake. Not C# either. Using Char.ToString() is fine, the alternative is to use the string constructor that takes a char:

  string mystring = new string((char)7, 1);

Or the more general form you might prefer:

  public static string ChrW(int code) {
return new string((char)code, 1);
}

Not the only way to do it, using literals is possible as well and likely to be what you prefer over a helper method. And the basic reason that C# does not need a helper function like Chr(). ASCII control code 7 is the bell character, it BEEPs you when you write it to the console, you can use an escape for that:

  string mystring = "\a";

Not exactly memorable, this comes from Unix. Other ones are "\b" for backspace, "\t" for a tab, "\r" for a carriage return and "\n" for a line feed. A classic trick to erase the last typed character in a console window is Console.Write("\b \b");. The Environment.NewLine property should be noted. Which is about as far as you should push it with control characters.

And last but not least the \U and \u specifier that lets you encode any character:

  string mystring = "\u0007";

Not obvious from the example but the \u value needs to be hexadecimal. \U is needed when you use codepoints from the upper Unicode bit planes.

How to convert vb .net function to c#?

Try this:

VB.Net to C# Conversion

public string DecryptString(string EncryptedString)
{
string functionReturnValue = null;

string TempLine = null;
string TempChar = null;
string FinalStr = null;
int i = 0;
int value = 0;

FinalStr = null;
value = 0;

if (!string.IsNullOrEmpty(EncryptedString)) {
for (i = 1; i <= Strings.Len(EncryptedString); i++) {
try {
TempChar = Strings.Mid(EncryptedString, i, 1);
value = Strings.Asc(TempChar);
value = (value - (120));
FinalStr = FinalStr + Strings.Chr(value);
} catch {
functionReturnValue = "";
return functionReturnValue;
}
}
functionReturnValue = FinalStr;
return functionReturnValue;
}

functionReturnValue = "";
return functionReturnValue;
}

Refer:

1.) Strings.Asc Method (String) :Returns an Integer value representing the character code corresponding to a character.
(Namespace: Microsoft.VisualBasic)

2.) What's the equivalent of VB's Asc() and Chr() functions in C#?:

For Asc() you can cast the char to an int like this:

int i = (int)your_char;

and for Chr() you can cast back to a char from an int like this:

char c = (char)your_int;

PCL-compatible Chr and Asc in VB?

The key is System.Text.ASCIIEncoding.UTF.GetString/GetBytes. These both work on arrays so you have to copy your input (an Int32 in my case) into a Byte() before going into GetString, and a Substring(0,1) into a Char() before going into GetBytes. This is, of course, more complex than the C# solution, where you can just cast the char/int. anyway...

   Public Function Asc(CharIn As String) As Integer
Dim c As Char = Convert.ToChar(CharIn.Substring(0, 1))
Dim cArray(0) As Char
cArray(0) = c
Return Convert.ToInt32(System.Text.ASCIIEncoding.UTF8.GetBytes(cArray)(0))
End Function
Public Function Chr(NumIn As Integer) As String
Dim bArray(0) As Byte
Dim AscString As String
bArray(0) = Convert.ToByte(NumIn)
AscString = System.Text.ASCIIEncoding.UTF8.GetString(bArray)
Return AscString
End Function

VB equivalent code to C#; List of Strings

  • Add the VB DLL reference to the project (import Microsoft.VisualBasic;).

Try this code:

List<int> intValidChars = new List<int>(new int[] {
Strings.Asc("0"),
Strings.Asc("1"),
Strings.Asc("2"),
Strings.Asc("3"),
Strings.Asc("4"),
Strings.Asc("5"),
Strings.Asc("6"),
Strings.Asc("7"),
Strings.Asc("8"),
Strings.Asc("9"),
Strings.Asc("A"),
Strings.Asc("B"),
Strings.Asc("C"),
Strings.Asc("D"),
Strings.Asc("E"),
Strings.Asc("F"),
Strings.Asc("G"),
Strings.Asc("H"),
Strings.Asc("I"),
Strings.Asc("J")
});

Translating VB.NET string manipulation to C#

You have to add a refererence to Microsoft.Visualbasic (Top menu, under "Add Reference" and the ".NET" Tab) and use Strings.Chr and Strings.Asc if you want to emulate perfectly the behaviour of Chr and Asc (as warned in this link by Garry Shutler's answer). Your code would become:

string Part1 = "Some string";
string p_str = null;
for (int I = 0; I < Part1.Length; I++)
{
p_str = Strings.Chr(Strings.Asc(Part1.Substring(I, 1)) + 17).ToString();
}

CLARIFICATION 1: the original code you are posting is not (pure) VB.NET, it is VB (VB.NET allows most of the VB commands to be written in). That's why so many changes are required (for example: changing the starting index); VB.NET is much more similar to C#.NET than this (as shown below).

CLARIFICATION 2: it is generally accepted that the C# translations for VB's Asc and Chr are mere casts (to int and char respectively), as you can see in the most voted answer in the aforementioned link. THIS IS WRONG. Test this code with both alternatives to confirm that only the Strings options deliver always the right result.

"PROPER" VB.NET CODE:

Dim Part1 As String = "Some string"
Dim p_str As String = Nothing
For I As Integer = 0 To Part1.Length - 1
p_str = Chr(Asc(Part1.Substring(I, 1)) + 17)
Next

Translating VB6 chars to C# strings

Chr converts a character code into the character, in C# you can just cast:

char c1 = (char)0x31;

(Also changing to use C#'s hexidecimal literals rather than VB6's.)

But when building a string, using escapes is easier:

string s1 = "\x31\x01\x50\x31\x16";

Chr(34) equivalent

You can cast an integer to a char, so an "automatic" translation would be:

inputString = inputString.Replace(, ((char) 34).ToString(), "")

That being said, the characters that maps to 34 (according to ASCII) is ", so you can simply construct a string with the double quote char:

inputString = inputString.Replace(, "\"", "")

This will also improve readability, since it is now clear what you are doing in that part: you remove double quotation characters.



Related Topics



Leave a reply



Submit