converting int to char

  • Thread starter Thread starter Nikhil Patel
  • Start date Start date
N

Nikhil Patel

Hi all,
How can I convert an integer to its equivalent ascii character without
using Microsoft.VisualBasic dll or any other dll(I want to reference only
System.dll).

Thanks.

-Nikhil
 
Thanks Alex,
Unfortunately I am using VB.Net not C#.

I tried

CType(10, Char)

in VB.Net. But it returned "1".
 
Nikhil,
Have you looked at ChrW?

Dim ch As Char = ChrW(10)

Hope this helps
Jay
 
You fail to mention the Text Encoding used, so I will assume the Integer in
question already represents ASCII.
Here is just a couple of the available methods, there are more depending upon
encoding.

Dim intChar As Integer = 65 'ASCII "A"
Dim c As Char

c = Chr(intChar)
c = Convert.ToChar(intChar)

Gerald
 
Thanks Jay.

The ChrW is in Microsoft.VisualBasic dll. I don't want to load this dll just
for this one function. Because my application is already very slow.

Is there any way to do this using only System.dll?

Thanks.
 
Sorry, failed to notice the part about just System.dll
These are of course in the VisualBasic portion.
 
CableWizard,
Thanks. It worked.

Cablewizard said:
You fail to mention the Text Encoding used, so I will assume the Integer in
question already represents ASCII.
Here is just a couple of the available methods, there are more depending upon
encoding.

Dim intChar As Integer = 65 'ASCII "A"
Dim c As Char

c = Chr(intChar)
c = Convert.ToChar(intChar)

Gerald
 
use the Convert static class.

Dim X as integer = 70
Dim Y as Char = Convert.ToChar(X)

Telmo Sampaio
 
I am a little confused however.
You posted to the VB group, but don't want to include the Microsoft.VisualBasic
dll?
You really can't do anything without that.
If you are running the IDE with a VB project open, that is automatically
referenced, and won't even appear in the references.
Are you confusing this dll with the Compatibility dll?

Gerald
 
Nikhil,
ChrW(10) is a constant expression!

Microsoft.VisualBasic will not be loaded!

Which also means that ChrW(10) does it without either
Microsoft.VisualBasic.dll or System.dll (mscorlib.dll really).

Only time that ChrW needs to load Microsoft.VisualBasic is when the
parameter is non constant, such as a variable or property.

Hope this helps
Jay
 
Telmo,
It uses the Microsoft.VisualBasic.
Which part of ChrW(10) is a constant do you not understand? :-|


Try it yourself, compile the following:

Public Shared Sub Main()

Dim ch As Char = ChrW(10)

End Sub


Use ILDASM to look at the code created:

method public static void Main() cil managed
{
.entrypoint
.custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01
00 00 00 )
// Code size 6 (0x6)
.maxstack 1
.locals init ([0] char ch)
.language '{3A12D0B8-C26C-11D0-B442-00A0244A1DD2}',
'{994B45C4-E6E9-11D2-903F-00C04FA302A1}',
'{00000000-0000-0000-0000-000000000000}'
// Source File 'C:\Telmo.vb'
//000161: Public Shared Sub Main()
IL_0000: nop
//000162:
//000163: Dim ch As Char = ChrW(10)
IL_0001: ldc.i4.s 10
IL_0003: stloc.0
//000164:
//000165: End Sub
IL_0004: nop
IL_0005: ret
} // end of method Grouping::Main


Where in the above IL do you see Microsoft.VisualBasic?

As I stated before: If the parameter to ChrW is a constant or a literal, the
compiler treats it as a constant Char, Microsoft.VisualBasic is not used!!!

Thanks
Jay
 
You are right. The VB to MSIL compiler does the work for you (i.e. it sees a
constant and therefore does not make a call to Microsoft.VisualBasic)
exactly as you have mentioned.
I still try not to use it since it is not a 'real' part of the CLS and it is
there for backward compatibility. I accept change and hope to see this
backard compatibility go away in newer versions of VB.

Telmo Sampaio (not good in VB6)

Jay B. Harlow said:
Telmo,
It uses the Microsoft.VisualBasic.
Which part of ChrW(10) is a constant do you not understand? :-|


Try it yourself, compile the following:

Public Shared Sub Main()

Dim ch As Char = ChrW(10)

End Sub


Use ILDASM to look at the code created:

method public static void Main() cil managed
{
.entrypoint
.custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01
00 00 00 )
// Code size 6 (0x6)
.maxstack 1
.locals init ([0] char ch)
.language '{3A12D0B8-C26C-11D0-B442-00A0244A1DD2}',
'{994B45C4-E6E9-11D2-903F-00C04FA302A1}',
'{00000000-0000-0000-0000-000000000000}'
// Source File 'C:\Telmo.vb'
//000161: Public Shared Sub Main()
IL_0000: nop
//000162:
//000163: Dim ch As Char = ChrW(10)
IL_0001: ldc.i4.s 10
IL_0003: stloc.0
//000164:
//000165: End Sub
IL_0004: nop
IL_0005: ret
} // end of method Grouping::Main


Where in the above IL do you see Microsoft.VisualBasic?

As I stated before: If the parameter to ChrW is a constant or a literal, the
compiler treats it as a constant Char, Microsoft.VisualBasic is not used!!!

Thanks
Jay

Telmo Sampaio said:
It uses the Microsoft.VisualBasic.

Telmo Sampaio
 
Telmo,
I still try not to use it since it is not a 'real' part of the CLS and it is
there for backward compatibility. I accept change and hope to see this
backard compatibility go away in newer versions of VB.
How can it be? CLS is Common Language Specification, which is "the set of
restrictions on the CTS (Common Type System) that ensure interoperability
among language". ChrW is a VB.NET keyword which happens to match the
"(char)" construct in C#. I would not expect ChrW to be part of the CLS any
more then "(char)" to be. The fact it uses a helper function for non
constant parameters is largely immaterial. The helper function simply
ensures that the supplied code point is within the proper range, I
understand that C# simply truncates the integer.

Did you perhaps mean the BCL (Base Class Library)? Does it really matter its
not part of the BCL, considering that Microsoft.VisualBasic is installed
with the framework, effectively making Microsoft.VisualBasic part of the
framework? (Yes it matters if you choose to use Mono, however I understand
that Mono has a Microsoft.VisualBasic assembly).

However! it is NOT there for backward compatibility! It is there as an
integral part of the VB.NET language, just as CType & DirectCast are also an
integral part of the VB.NET language.

You should realize that the contructs in Microsoft.VisualBasic.Compatibility
assembly are there for backward compatibility, which may or may not go away
in newer versions.

The following articles may help you understand the relationship of ChrW and
Convert and other integral parts of the VB.NET language:

http://www.panopticoncentral.net/archive/2004/05/31/1100.aspx

http://www.panopticoncentral.net/archive/2004/06/07/1200.aspx

(not good in VB6)
Funny I thought we were talking about VB.NET!

Hope this helps
Jay

Telmo Sampaio said:
You are right. The VB to MSIL compiler does the work for you (i.e. it sees a
constant and therefore does not make a call to Microsoft.VisualBasic)
exactly as you have mentioned.
I still try not to use it since it is not a 'real' part of the CLS and it is
there for backward compatibility. I accept change and hope to see this
backard compatibility go away in newer versions of VB.

Telmo Sampaio (not good in VB6)

Jay B. Harlow said:
Telmo,
It uses the Microsoft.VisualBasic.
Which part of ChrW(10) is a constant do you not understand? :-|


Try it yourself, compile the following:

Public Shared Sub Main()

Dim ch As Char = ChrW(10)

End Sub


Use ILDASM to look at the code created:

method public static void Main() cil managed
{
.entrypoint
.custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01
00 00 00 )
// Code size 6 (0x6)
.maxstack 1
.locals init ([0] char ch)
.language '{3A12D0B8-C26C-11D0-B442-00A0244A1DD2}',
'{994B45C4-E6E9-11D2-903F-00C04FA302A1}',
'{00000000-0000-0000-0000-000000000000}'
// Source File 'C:\Telmo.vb'
//000161: Public Shared Sub Main()
IL_0000: nop
//000162:
//000163: Dim ch As Char = ChrW(10)
IL_0001: ldc.i4.s 10
IL_0003: stloc.0
//000164:
//000165: End Sub
IL_0004: nop
IL_0005: ret
} // end of method Grouping::Main


Where in the above IL do you see Microsoft.VisualBasic?

As I stated before: If the parameter to ChrW is a constant or a literal, the
compiler treats it as a constant Char, Microsoft.VisualBasic is not used!!!

Thanks
Jay

Telmo Sampaio said:
It uses the Microsoft.VisualBasic.

Telmo Sampaio

Nikhil,
ChrW(10) is a constant expression!

Microsoft.VisualBasic will not be loaded!

Which also means that ChrW(10) does it without either
Microsoft.VisualBasic.dll or System.dll (mscorlib.dll really).

Only time that ChrW needs to load Microsoft.VisualBasic is when the
parameter is non constant, such as a variable or property.

Hope this helps
Jay

Thanks Jay.

The ChrW is in Microsoft.VisualBasic dll. I don't want to load
this
dll
just
for this one function. Because my application is already very slow.

Is there any way to do this using only System.dll?

Thanks.


message
Nikhil,
Have you looked at ChrW?

Dim ch As Char = ChrW(10)

Hope this helps
Jay

Thanks Alex,
Unfortunately I am using VB.Net not C#.

I tried

CType(10, Char)

in VB.Net. But it returned "1".


Nikhil.

did you try

char c = (char)13;

HTH
Alex

Hi all,
How can I convert an integer to its equivalent ascii
character
without
using Microsoft.VisualBasic dll or any other dll(I want to
reference
only
System.dll).

Thanks.

-Nikhil
 
It appears in the Project Imports - Project Properties, Common, Imports.
It can be removed from there. As a note to newbies reading this, looking
through the task list after removing the Imports and References to
Microsoft.VisualBasic, you can get an idea of which statements are now
unavailable.
____________________________________
The Grim Reaper
 
Jay,

As you see in
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/
vafctChr.asp ChrW is part of the Microsoft.VisualBasic Namespace. As a
trainer I show my students the use of System.dll and functions from
Microsoft.VisualBasic, explaining that in some cases the functions will be
even faster than the System.Convert class. However, I do not know if
Microsoft.VisualBasic will be there in the future, it may not. Since we are
talking about that, I liked the article you sent that shows that VS 2005
will go further on VB Functions.

As for the CLS, it is well defined here
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconWhatIsCommonLanguageSpecification.asp where it says that some
features of the Framework are not CLS-compliant and therefore cannot be used
by all languages. I assume that this was the case here, since I cannot
access those functions from other languages.

Telmo
P.S.: I am enjoying the discussion, do not take it personal

Jay B. Harlow said:
Telmo,
I still try not to use it since it is not a 'real' part of the CLS and
it
is
there for backward compatibility. I accept change and hope to see this
backard compatibility go away in newer versions of VB.
How can it be? CLS is Common Language Specification, which is "the set of
restrictions on the CTS (Common Type System) that ensure interoperability
among language". ChrW is a VB.NET keyword which happens to match the
"(char)" construct in C#. I would not expect ChrW to be part of the CLS any
more then "(char)" to be. The fact it uses a helper function for non
constant parameters is largely immaterial. The helper function simply
ensures that the supplied code point is within the proper range, I
understand that C# simply truncates the integer.

Did you perhaps mean the BCL (Base Class Library)? Does it really matter its
not part of the BCL, considering that Microsoft.VisualBasic is installed
with the framework, effectively making Microsoft.VisualBasic part of the
framework? (Yes it matters if you choose to use Mono, however I understand
that Mono has a Microsoft.VisualBasic assembly).

However! it is NOT there for backward compatibility! It is there as an
integral part of the VB.NET language, just as CType & DirectCast are also an
integral part of the VB.NET language.

You should realize that the contructs in Microsoft.VisualBasic.Compatibility
assembly are there for backward compatibility, which may or may not go away
in newer versions.

The following articles may help you understand the relationship of ChrW and
Convert and other integral parts of the VB.NET language:

http://www.panopticoncentral.net/archive/2004/05/31/1100.aspx

http://www.panopticoncentral.net/archive/2004/06/07/1200.aspx

(not good in VB6)
Funny I thought we were talking about VB.NET!

Hope this helps
Jay

Telmo Sampaio said:
You are right. The VB to MSIL compiler does the work for you (i.e. it
sees
a
constant and therefore does not make a call to Microsoft.VisualBasic)
exactly as you have mentioned.
I still try not to use it since it is not a 'real' part of the CLS and
it
is
there for backward compatibility. I accept change and hope to see this
backard compatibility go away in newer versions of VB.

Telmo Sampaio (not good in VB6)

Telmo,
It uses the Microsoft.VisualBasic.
Which part of ChrW(10) is a constant do you not understand? :-|


Try it yourself, compile the following:

Public Shared Sub Main()

Dim ch As Char = ChrW(10)

End Sub


Use ILDASM to look at the code created:

method public static void Main() cil managed
{
.entrypoint
.custom instance void [mscorlib]System.STAThreadAttribute::.ctor() =
(
01
00 00 00 )
// Code size 6 (0x6)
.maxstack 1
.locals init ([0] char ch)
.language '{3A12D0B8-C26C-11D0-B442-00A0244A1DD2}',
'{994B45C4-E6E9-11D2-903F-00C04FA302A1}',
'{00000000-0000-0000-0000-000000000000}'
// Source File 'C:\Telmo.vb'
//000161: Public Shared Sub Main()
IL_0000: nop
//000162:
//000163: Dim ch As Char = ChrW(10)
IL_0001: ldc.i4.s 10
IL_0003: stloc.0
//000164:
//000165: End Sub
IL_0004: nop
IL_0005: ret
} // end of method Grouping::Main


Where in the above IL do you see Microsoft.VisualBasic?

As I stated before: If the parameter to ChrW is a constant or a
literal,
the
compiler treats it as a constant Char, Microsoft.VisualBasic is not used!!!

Thanks
Jay

It uses the Microsoft.VisualBasic.

Telmo Sampaio

Nikhil,
ChrW(10) is a constant expression!

Microsoft.VisualBasic will not be loaded!

Which also means that ChrW(10) does it without either
Microsoft.VisualBasic.dll or System.dll (mscorlib.dll really).

Only time that ChrW needs to load Microsoft.VisualBasic is when the
parameter is non constant, such as a variable or property.

Hope this helps
Jay

Thanks Jay.

The ChrW is in Microsoft.VisualBasic dll. I don't want to load this
dll
just
for this one function. Because my application is already very slow.

Is there any way to do this using only System.dll?

Thanks.


message
Nikhil,
Have you looked at ChrW?

Dim ch As Char = ChrW(10)

Hope this helps
Jay

Thanks Alex,
Unfortunately I am using VB.Net not C#.

I tried

CType(10, Char)

in VB.Net. But it returned "1".


Nikhil.

did you try

char c = (char)13;

HTH
Alex

Hi all,
How can I convert an integer to its equivalent ascii
character
without
using Microsoft.VisualBasic dll or any other dll(I want to
reference
only
System.dll).

Thanks.

-Nikhil
 
ah, hmm... good to know. however I have never had any reason to consider
removing it. since we are coding in VB, i'm curious as to why someone would want
to remove the reference. a comment was made about performance, does referencing
this dll somehow slow things down?

Gerald
 
Telmo,
You do realize the fact that the ChrW keyword calls into
Microsoft.VisualBasic is an implementation detail that you really should not
worry about!

Also I find it better to write (and teach) "correct" programs then to worry
about this function performs better then that function. By "correct" I mean
OO and use the correct tool for the correct job (for example: when to use
ChrW and when to use Convert.ToChar). Remember that most programs follow the
80/20 rule (link below) that is 80% of the execution time of your program is
spent in 20% of your code. I will optimize a routine once that routine has
proven to be a performance problem via profiling (link below).

Like I stated it is ChrW is an integral part of the VB.NET language, if
Microsoft.VisualBasic should happen to no longer be available, I suspect
that the compiler will implement ChrW in a different manner, as ChrW is a
keyword. At the same time I can also see ChrW being implemented inline in
more cases while Microsoft.VisualBasic continues to be available...
I assume that this was the case here, since I cannot
access those functions from other languages.
Oh Really!! Most of Microsoft.Visualbasic you can access from any .NET
language you want, as they are "pure .NET" functions. I say "Most" as some
of the function as undocumented, this does not mean you cannot access them,
it just means they are undocumented & subject to change. Documented
functions would be marked as Obsolete, if changed...

For example, open a C# project, use 'Project - Reference - Add Reference' to
add a reference to the "Microsoft Visual Basic .NET Runtime", then call
"Microsoft.VisualBasic.Strings.Split"

using Microsoft.VisualBasic;

string [] fields = Strings.Split(Line2, "\r\n", -1,
CompareMethod.Binary);

For info on the 80/20 rule & optimizing only the 20% see Martin Fowler's
article "Yet Another Optimization Article" at
http://martinfowler.com/ieeeSoftware/yetOptimization.pdf

For a list of Martin's articles see:

http://martinfowler.com/articles.html

Info on the CLR Profiler:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag/html/scalenethowto13.asp

http://msdn.microsoft.com/library/d...y/en-us/dndotnet/html/highperfmanagedapps.asp


Hope this helps
Jay

Telmo Sampaio said:
Jay,

As you see in
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vblr7/html/
vafctChr.asp ChrW is part of the Microsoft.VisualBasic Namespace. As a
trainer I show my students the use of System.dll and functions from
Microsoft.VisualBasic, explaining that in some cases the functions will be
even faster than the System.Convert class. However, I do not know if
Microsoft.VisualBasic will be there in the future, it may not. Since we are
talking about that, I liked the article you sent that shows that VS 2005
will go further on VB Functions.

As for the CLS, it is well defined here
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconWhatIsCommonLanguageSpecification.asp where it says that some
features of the Framework are not CLS-compliant and therefore cannot be used
by all languages. I assume that this was the case here, since I cannot
access those functions from other languages.

Telmo
P.S.: I am enjoying the discussion, do not take it personal

Jay B. Harlow said:
Telmo, it
How can it be? CLS is Common Language Specification, which is "the set of
restrictions on the CTS (Common Type System) that ensure interoperability
among language". ChrW is a VB.NET keyword which happens to match the
"(char)" construct in C#. I would not expect ChrW to be part of the CLS any
more then "(char)" to be. The fact it uses a helper function for non
constant parameters is largely immaterial. The helper function simply
ensures that the supplied code point is within the proper range, I
understand that C# simply truncates the integer.

Did you perhaps mean the BCL (Base Class Library)? Does it really matter its
not part of the BCL, considering that Microsoft.VisualBasic is installed
with the framework, effectively making Microsoft.VisualBasic part of the
framework? (Yes it matters if you choose to use Mono, however I understand
that Mono has a Microsoft.VisualBasic assembly).

However! it is NOT there for backward compatibility! It is there as an
integral part of the VB.NET language, just as CType & DirectCast are
also
an
integral part of the VB.NET language.

You should realize that the contructs in Microsoft.VisualBasic.Compatibility
assembly are there for backward compatibility, which may or may not go away
in newer versions.

The following articles may help you understand the relationship of ChrW and
Convert and other integral parts of the VB.NET language:

http://www.panopticoncentral.net/archive/2004/05/31/1100.aspx

http://www.panopticoncentral.net/archive/2004/06/07/1200.aspx

(not good in VB6)
Funny I thought we were talking about VB.NET!

Hope this helps
Jay

Telmo Sampaio said:
You are right. The VB to MSIL compiler does the work for you (i.e. it
sees
a
constant and therefore does not make a call to Microsoft.VisualBasic)
exactly as you have mentioned.
I still try not to use it since it is not a 'real' part of the CLS and
it
is
there for backward compatibility. I accept change and hope to see this
backard compatibility go away in newer versions of VB.

Telmo Sampaio (not good in VB6)

Telmo,
It uses the Microsoft.VisualBasic.
Which part of ChrW(10) is a constant do you not understand? :-|


Try it yourself, compile the following:

Public Shared Sub Main()

Dim ch As Char = ChrW(10)

End Sub


Use ILDASM to look at the code created:

method public static void Main() cil managed
{
.entrypoint
.custom instance void [mscorlib]System.STAThreadAttribute::.ctor()
=
(
01
00 00 00 )
// Code size 6 (0x6)
.maxstack 1
.locals init ([0] char ch)
.language '{3A12D0B8-C26C-11D0-B442-00A0244A1DD2}',
'{994B45C4-E6E9-11D2-903F-00C04FA302A1}',
'{00000000-0000-0000-0000-000000000000}'
// Source File 'C:\Telmo.vb'
//000161: Public Shared Sub Main()
IL_0000: nop
//000162:
//000163: Dim ch As Char = ChrW(10)
IL_0001: ldc.i4.s 10
IL_0003: stloc.0
//000164:
//000165: End Sub
IL_0004: nop
IL_0005: ret
} // end of method Grouping::Main


Where in the above IL do you see Microsoft.VisualBasic?

As I stated before: If the parameter to ChrW is a constant or a literal,
the
compiler treats it as a constant Char, Microsoft.VisualBasic is not
used!!!

Thanks
Jay

It uses the Microsoft.VisualBasic.

Telmo Sampaio

message
Nikhil,
ChrW(10) is a constant expression!

Microsoft.VisualBasic will not be loaded!

Which also means that ChrW(10) does it without either
Microsoft.VisualBasic.dll or System.dll (mscorlib.dll really).

Only time that ChrW needs to load Microsoft.VisualBasic is when the
parameter is non constant, such as a variable or property.

Hope this helps
Jay

Thanks Jay.

The ChrW is in Microsoft.VisualBasic dll. I don't want to load this
dll
just
for this one function. Because my application is already very slow.

Is there any way to do this using only System.dll?

Thanks.


message
Nikhil,
Have you looked at ChrW?

Dim ch As Char = ChrW(10)

Hope this helps
Jay

Thanks Alex,
Unfortunately I am using VB.Net not C#.

I tried

CType(10, Char)

in VB.Net. But it returned "1".


Nikhil.

did you try

char c = (char)13;

HTH
Alex

Hi all,
How can I convert an integer to its equivalent ascii
character
without
using Microsoft.VisualBasic dll or any other dll(I
want
 
Gerald,
Yes referencing a DLL "can" slow down your app as the DLL needs to be
loaded, and the types within the DLL need to be loaded.

However! even it you remove the reference, VB.NET will still create your
assembly with a reference and call into it.

For example look at ILDASM on the following:

Dim s1, s2 As String

If s1 = s2 Then

End If

Rather then worry about removing Microsoft.VisualBasic I would be more
concerned in "right sizing" the assemblies I create...


Personally the VB in VB.NET stands for Visual Basic! ChrW is part of the
VB.NET language, so use it where its appropriate.
Microsoft.VisualBasic.Strings.Split is part of the VB.NET runtime, so use it
where it makes sense.

My only real cautions on Microsoft.VisualBasic are:
1. Avoid Collection as its too generalized, use System.Collections &
System.Collections.Specialized instead. (this is more an OO thing then
anything).
2. Don't mix System.String with Microsoft.VisualBasic.Strings, as VB.Strings
are all 1 based functions, while System.String are 0 based functions. I will
use VB.Strings.Split with System.String where I do need to split based on a
word...

Also as I told Telmo I would not worry about performance when you are
writing your app, I would worry about performance once a routine has proven
to be a performance problem... (the 80/20 rule).

Hope this helps
Jay

Cablewizard said:
ah, hmm... good to know. however I have never had any reason to consider
removing it. since we are coding in VB, i'm curious as to why someone would want
to remove the reference. a comment was made about performance, does referencing
this dll somehow slow things down?

Gerald
<<snip>>
 
Back
Top