I am porting an old VB code base to PCL-compatible format. I have managed to build new versions of things like Left and CInt, but Asc and Chr have me stumped. The typical answer is "import Microsoft.VisualBasic", but that does not exist in PCL.
In C# you simply cast the string to int to get the Unicode value. The equivalent would seem DirectCast in VB.net, but that does not work. Does anyone know how to write Asc and Chr in VB.net using PCL-compatible functions?
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