473,386 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Is there a C# equivalent of the VB.NET asc function?

Hello,

I'm converting some old VB6 code to use with ASP.NET and have come
unstuck with the Asc() function. This was used in the old VB6 code to
convert a character to its ASCII numeric equivalent.

Is there such a function available in C#? I can see that VB.NET has one,
but I couldn't see how to get at it in C#.

For example, if I have ...

string str = "Hello";

.... how would I do the following (where I have used the VB Asc()
function in my C# pseudocode)...

int sum = 0;
for (int i=0; i:str.Length(); i++) {
sum += Asc(str.Substring(i,1));
}

TIA

--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #1
9 4021
Will this work?
x = System.Convert.ToInt32(Mychar.Substring(0, 1));

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net
"Alan Silver" <al*********@nospam.thanx> wrote in message
news:PS**************@nospamthankyou.spam...
Hello,

I'm converting some old VB6 code to use with ASP.NET and have come unstuck
with the Asc() function. This was used in the old VB6 code to convert a
character to its ASCII numeric equivalent.

Is there such a function available in C#? I can see that VB.NET has one,
but I couldn't see how to get at it in C#.

For example, if I have ...

string str = "Hello";

... how would I do the following (where I have used the VB Asc() function
in my C# pseudocode)...

int sum = 0;
for (int i=0; i:str.Length(); i++) {
sum += Asc(str.Substring(i,1));
}

TIA

--
Alan Silver
(anything added below this line is nothing to do with me)

Nov 19 '05 #2
> I'm converting some old VB6 code to use with ASP.NET and have come
unstuck with the Asc() function. This was used in the old VB6 code to
convert a character to its ASCII numeric equivalent.

Is there such a function available in C#? I can see that VB.NET has
one, but I couldn't see how to get at it in C#.


char c = 'A';
int val = (int)x;

-Brock
DevelopMentor
http://staff.develop.com/ballen

Nov 19 '05 #3
Jim
Give this a shot

sum += (int) str[i];

Jim

"Alan Silver" wrote:
Hello,

I'm converting some old VB6 code to use with ASP.NET and have come
unstuck with the Asc() function. This was used in the old VB6 code to
convert a character to its ASCII numeric equivalent.

Is there such a function available in C#? I can see that VB.NET has one,
but I couldn't see how to get at it in C#.

For example, if I have ...

string str = "Hello";

.... how would I do the following (where I have used the VB Asc()
function in my C# pseudocode)...

int sum = 0;
for (int i=0; i:str.Length(); i++) {
sum += Asc(str.Substring(i,1));
}

TIA

--
Alan Silver
(anything added below this line is nothing to do with me)

Nov 19 '05 #4
>> I'm converting some old VB6 code to use with ASP.NET and have come
unstuck with the Asc() function. This was used in the old VB6 code to
convert a character to its ASCII numeric equivalent.
Is there such a function available in C#? I can see that VB.NET has
one, but I couldn't see how to get at it in C#.


char c = 'A';
int val = (int)x;


Oh, isn't that clever!!

Can I convert back as easily? I mean the equivalent of the Chr()
function? I'm sure it's probably as simple as another cast, but my brain
isn't clear enough (1am after a really lousy day's work) to be sure I'm
doing it right!!

Also (whilst you're being so helpful), is there an equivalent of the Xor
operator? I was using this for some encryption, but couldn't work out
how to do it in C#. The .NET class library is so big that it's hard to
find your way around.

Thanks very much for the help. Any further help would be greatly
appreciated.

--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #5
>Give this a shot

sum += (int) str[i];
Ooh-err, another useful tip. I'd forgotten that you can treat strings
like char arrays. That will make life easier.

Thanks for the tip. Please read my other reply and see if you can shed
any light on the Xor question.

Thanks again
Jim

"Alan Silver" wrote:
Hello,

I'm converting some old VB6 code to use with ASP.NET and have come
unstuck with the Asc() function. This was used in the old VB6 code to
convert a character to its ASCII numeric equivalent.

Is there such a function available in C#? I can see that VB.NET has one,
but I couldn't see how to get at it in C#.

For example, if I have ...

string str = "Hello";

.... how would I do the following (where I have used the VB Asc()
function in my C# pseudocode)...

int sum = 0;
for (int i=0; i:str.Length(); i++) {
sum += Asc(str.Substring(i,1));
}

TIA

--
Alan Silver
(anything added below this line is nothing to do with me)


--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #6
Jim
Yes,

' ' == (char) 20

and you want ^
eg 11110000 ^ 10101010 equals 01011010

Sombody's in the UK writing some sort of license key/password hashing
algorithm??!

Jim

"Alan Silver" wrote:
I'm converting some old VB6 code to use with ASP.NET and have come
unstuck with the Asc() function. This was used in the old VB6 code to
convert a character to its ASCII numeric equivalent.
Is there such a function available in C#? I can see that VB.NET has
one, but I couldn't see how to get at it in C#.


char c = 'A';
int val = (int)x;


Oh, isn't that clever!!

Can I convert back as easily? I mean the equivalent of the Chr()
function? I'm sure it's probably as simple as another cast, but my brain
isn't clear enough (1am after a really lousy day's work) to be sure I'm
doing it right!!

Also (whilst you're being so helpful), is there an equivalent of the Xor
operator? I was using this for some encryption, but couldn't work out
how to do it in C#. The .NET class library is so big that it's hard to
find your way around.

Thanks very much for the help. Any further help would be greatly
appreciated.

--
Alan Silver
(anything added below this line is nothing to do with me)

Nov 19 '05 #7
>Yes,

' ' == (char) 20

and you want ^
eg 11110000 ^ 10101010 equals 01011010
Thanks!! I love the simplicity of C#. I just need to get it all in my
head.

It occurred to me in bed last night that the C# equivalent of Xor was
probably an operator, not a method of a class. That explains why I
couldn't find it in the class library. Ho hum.
Sombody's in the UK writing some sort of license key/password hashing
algorithm??!
Not quite that fancy, but close. I have an old VB6/ASP application that
encrypts some sensitive data before storing it in the database. I'm
trying to rewrite it bit by bit in ASP.NET and am trying to convert the
encryption/decryption routines as accurately as possible.

To be honest, the code is quite old and not my best!! I would really
like to re-engineer the whole thing from scratch, but there's too much
legacy data around to allow that at the moment. I need to get the new
system working with the old data as quickly as possible. I have the
following inelegant and inefficient function ...

Public Function Encode(Key As String, ByVal Str As String) As String
Dim Tmp As String, RealKey As String, sc As String, kc As String
Dim l As Integer, i As Integer
Tmp = StrReverse(Key) ' save the key passed to us
RealKey = ""
l = Len(Tmp & Main.RealSeed)
For i = 1 To l ' build up the full key from the seed and the passed
key
RealKey = RealKey & Mid(Tmp, i, 1) & Mid(Main.RealSeed, i, 1)
Next
Tmp = "" ' will hold the encoded string
For i = 1 To Len(Str)
sc = Asc(Mid(Str, i, 1))
kc = Asc(Mid(RealKey, i, 1))
Tmp = Tmp & Chr(sc Xor kc)
Next
Encode = Tmp
End Function

.... which I am trying to rewrite in C#. As you can see, the code is not
something to be proud of!! I wrote it a long time ago and have learned a
lot since then.

By the way, if you're trying to make sense of it, Main.RealSeed refers
to a variable in the Main from of the application that holds a seed for
the (admittedly basic) encryption routine.

Thanks for the help.
Jim

"Alan Silver" wrote:
>> I'm converting some old VB6 code to use with ASP.NET and have come
>> unstuck with the Asc() function. This was used in the old VB6 code to
>> convert a character to its ASCII numeric equivalent.
>> Is there such a function available in C#? I can see that VB.NET has
>> one, but I couldn't see how to get at it in C#.
>
>char c = 'A';
>int val = (int)x;


Oh, isn't that clever!!

Can I convert back as easily? I mean the equivalent of the Chr()
function? I'm sure it's probably as simple as another cast, but my brain
isn't clear enough (1am after a really lousy day's work) to be sure I'm
doing it right!!

Also (whilst you're being so helpful), is there an equivalent of the Xor
operator? I was using this for some encryption, but couldn't work out
how to do it in C#. The .NET class library is so big that it's hard to
find your way around.

Thanks very much for the help. Any further help would be greatly
appreciated.

--
Alan Silver
(anything added below this line is nothing to do with me)


--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #8
Jim
If you're not having fun doing it (and actually a better reason...) you
probably shouldn't be doing this yourself, as it's weak, and .NET already has
all this stuff done for you.

If it's one way hashing you need (like checking passwords) use MD5
System.Security.Cryptography.MD5

which is pretty easy
byte[] data = new byte[DATA_SIZE];

// This is one implementation of the abstract class MD5.
MD5 md5 = new MD5CryptoServiceProvider();

byte[] result = md5.ComputeHash(data);
If you need proper 2 way encryption use something like RSA
System.Security.Cryptography.RSACryptoServiceProvi der

admittedly it's not quite trivial, but the docs are good and there's tons of
examples out there. Plus you wont get screwed if someone decyphers your
"encoding" (it's not really encryption is it, its just encoding).

ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconcryptographyoverview.htm

I'm by no means an encryption expert so I can't recommend an encryption
scheme to use, but I would certainly suggest looking at this.

Jim
"Alan Silver" wrote:
Yes,

' ' == (char) 20

and you want ^
eg 11110000 ^ 10101010 equals 01011010


Thanks!! I love the simplicity of C#. I just need to get it all in my
head.

It occurred to me in bed last night that the C# equivalent of Xor was
probably an operator, not a method of a class. That explains why I
couldn't find it in the class library. Ho hum.
Sombody's in the UK writing some sort of license key/password hashing
algorithm??!


Not quite that fancy, but close. I have an old VB6/ASP application that
encrypts some sensitive data before storing it in the database. I'm
trying to rewrite it bit by bit in ASP.NET and am trying to convert the
encryption/decryption routines as accurately as possible.

To be honest, the code is quite old and not my best!! I would really
like to re-engineer the whole thing from scratch, but there's too much
legacy data around to allow that at the moment. I need to get the new
system working with the old data as quickly as possible. I have the
following inelegant and inefficient function ...

Public Function Encode(Key As String, ByVal Str As String) As String
Dim Tmp As String, RealKey As String, sc As String, kc As String
Dim l As Integer, i As Integer
Tmp = StrReverse(Key) ' save the key passed to us
RealKey = ""
l = Len(Tmp & Main.RealSeed)
For i = 1 To l ' build up the full key from the seed and the passed
key
RealKey = RealKey & Mid(Tmp, i, 1) & Mid(Main.RealSeed, i, 1)
Next
Tmp = "" ' will hold the encoded string
For i = 1 To Len(Str)
sc = Asc(Mid(Str, i, 1))
kc = Asc(Mid(RealKey, i, 1))
Tmp = Tmp & Chr(sc Xor kc)
Next
Encode = Tmp
End Function

.... which I am trying to rewrite in C#. As you can see, the code is not
something to be proud of!! I wrote it a long time ago and have learned a
lot since then.

By the way, if you're trying to make sense of it, Main.RealSeed refers
to a variable in the Main from of the application that holds a seed for
the (admittedly basic) encryption routine.

Thanks for the help.
Jim

"Alan Silver" wrote:
>> I'm converting some old VB6 code to use with ASP.NET and have come
>> unstuck with the Asc() function. This was used in the old VB6 code to
>> convert a character to its ASCII numeric equivalent.
>> Is there such a function available in C#? I can see that VB.NET has
>> one, but I couldn't see how to get at it in C#.
>
>char c = 'A';
>int val = (int)x;

Oh, isn't that clever!!

Can I convert back as easily? I mean the equivalent of the Chr()
function? I'm sure it's probably as simple as another cast, but my brain
isn't clear enough (1am after a really lousy day's work) to be sure I'm
doing it right!!

Also (whilst you're being so helpful), is there an equivalent of the Xor
operator? I was using this for some encryption, but couldn't work out
how to do it in C#. The .NET class library is so big that it's hard to
find your way around.

Thanks very much for the help. Any further help would be greatly
appreciated.

--
Alan Silver
(anything added below this line is nothing to do with me)


--
Alan Silver
(anything added below this line is nothing to do with me)

Nov 19 '05 #9
Jim,

Thanks for the reply...
If you're not having fun doing it (and actually a better reason...) you
probably shouldn't be doing this yourself, as it's weak, and .NET already has
all this stuff done for you.
I'm not having a bad time doing it, I'm just a bit pushed for time and
would prefer to have more chance to learn all this properly.

As far as the .NET framework's offerings, I would love to use them and
do the job properly, but as explained, I'm working with a lot of legacy
data that has been encrypted using the existing method. I need to make
sure the new system can read the old data.

At some point (when I ever get enough time, ha ha ha) I would like to
rewrite this system from scratch. At that point, I would use whatever
..NET has to offer as it's bound to be more stable and easier than
writing my own code. Unfortunately, I don't see that happening in the
near future ;-(

Thanks again for the reply. The info is useful anyway.
If it's one way hashing you need (like checking passwords) use MD5
System.Security.Cryptography.MD5

which is pretty easy
byte[] data = new byte[DATA_SIZE];

// This is one implementation of the abstract class MD5.
MD5 md5 = new MD5CryptoServiceProvider();

byte[] result = md5.ComputeHash(data);
If you need proper 2 way encryption use something like RSA
System.Security.Cryptography.RSACryptoServiceProv ider

admittedly it's not quite trivial, but the docs are good and there's tons of
examples out there. Plus you wont get screwed if someone decyphers your
"encoding" (it's not really encryption is it, its just encoding).

ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconcryptog
raphyoverview.htm

I'm by no means an encryption expert so I can't recommend an encryption
scheme to use, but I would certainly suggest looking at this.

Jim
"Alan Silver" wrote:
>Yes,
>
>' ' == (char) 20
>
>and you want ^
> eg 11110000 ^ 10101010 equals 01011010


Thanks!! I love the simplicity of C#. I just need to get it all in my
head.

It occurred to me in bed last night that the C# equivalent of Xor was
probably an operator, not a method of a class. That explains why I
couldn't find it in the class library. Ho hum.
>Sombody's in the UK writing some sort of license key/password hashing
>algorithm??!


Not quite that fancy, but close. I have an old VB6/ASP application that
encrypts some sensitive data before storing it in the database. I'm
trying to rewrite it bit by bit in ASP.NET and am trying to convert the
encryption/decryption routines as accurately as possible.

To be honest, the code is quite old and not my best!! I would really
like to re-engineer the whole thing from scratch, but there's too much
legacy data around to allow that at the moment. I need to get the new
system working with the old data as quickly as possible. I have the
following inelegant and inefficient function ...

Public Function Encode(Key As String, ByVal Str As String) As String
Dim Tmp As String, RealKey As String, sc As String, kc As String
Dim l As Integer, i As Integer
Tmp = StrReverse(Key) ' save the key passed to us
RealKey = ""
l = Len(Tmp & Main.RealSeed)
For i = 1 To l ' build up the full key from the seed and the passed
key
RealKey = RealKey & Mid(Tmp, i, 1) & Mid(Main.RealSeed, i, 1)
Next
Tmp = "" ' will hold the encoded string
For i = 1 To Len(Str)
sc = Asc(Mid(Str, i, 1))
kc = Asc(Mid(RealKey, i, 1))
Tmp = Tmp & Chr(sc Xor kc)
Next
Encode = Tmp
End Function

.... which I am trying to rewrite in C#. As you can see, the code is not
something to be proud of!! I wrote it a long time ago and have learned a
lot since then.

By the way, if you're trying to make sense of it, Main.RealSeed refers
to a variable in the Main from of the application that holds a seed for
the (admittedly basic) encryption routine.

Thanks for the help.
>Jim
>
>"Alan Silver" wrote:
>
>> >> I'm converting some old VB6 code to use with ASP.NET and have come
>> >> unstuck with the Asc() function. This was used in the old VB6 code to
>> >> convert a character to its ASCII numeric equivalent.
>> >> Is there such a function available in C#? I can see that VB.NET has
>> >> one, but I couldn't see how to get at it in C#.
>> >
>> >char c = 'A';
>> >int val = (int)x;
>>
>> Oh, isn't that clever!!
>>
>> Can I convert back as easily? I mean the equivalent of the Chr()
>> function? I'm sure it's probably as simple as another cast, but my brain
>> isn't clear enough (1am after a really lousy day's work) to be sure I'm
>> doing it right!!
>>
>> Also (whilst you're being so helpful), is there an equivalent of the Xor
>> operator? I was using this for some encryption, but couldn't work out
>> how to do it in C#. The .NET class library is so big that it's hard to
>> find your way around.
>>
>> Thanks very much for the help. Any further help would be greatly
>> appreciated.
>>
>> --
>> Alan Silver
>> (anything added below this line is nothing to do with me)
>>


--
Alan Silver
(anything added below this line is nothing to do with me)


--
Alan Silver
(anything added below this line is nothing to do with me)
Nov 19 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Susan Baker | last post by:
Hi, I'm working on s simple parser and I would like to split up multi-line expressions into single expressions. Is there a way whereby I could slurp the lines into an array (or other container)?...
7
by: am_ggh | last post by:
Is there a PHP equivalent of TCL's "upvar" ? I will appreciate any insights. Andy
6
by: exquisitus | last post by:
Hi all, I'm porting a DOS application to run on Linux. I need to replace this function or use an equivalent. Anyone knows how or where I can get this function's equivalent (or maybe someones...
14
by: Allen | last post by:
Greetings, I need to pass a file path to an application. This file path contains long directory and file names. The target application, pdftotext.exe, only accepts short directory and file...
2
by: ramu | last post by:
Hi I have to call a vc++ function in a c program. suppose i have a function dword fun(dword arg1, bstr arg2); in vc++. I have to call this function in c. But I don't have dword datatype in...
7
by: divya | last post by:
What is an equivalent TimeSerial() function of VBscript in the Java Script?? I have two variables hour and minutes. Timeserial(hour,minutes) returns the time hour : minutes. But I want to do...
6
by: Jon Paal | last post by:
what is vb equiv. of ++ shown in C# ?
6
by: openopt | last post by:
Thank you in advance for your response. Dmitrey
18
by: Csaba Gabor | last post by:
Is there a straightforward way of implementing a PHP equivalent to window.setTimeout? In a javascript web app, it's often the case that things are done on an event driven basis. For example,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.