By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,110 Members | 1,543 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,110 IT Pros & Developers. It's quick & easy.

+how to C# convert value to ascii ?

P: 14
Hi,
Im trying to build simple encryption,
I use
char d="a"
int intASC = System.Convert.ToInt32(d);

to convert it, and play with it,
now I want to change it (d) back for the original ascii value ("a"),
how do I do that ?
Oct 9 '07 #1
Share this Question
Share on Google+
6 Replies


Shashi Sadasivan
Expert 100+
P: 1,435
Do you mean you want to convert it from the ascii value to the char value?


Expand|Select|Wrap|Line Numbers
  1.  char x = Convert.ToChar(intASC);
PS: From your code that you gave, you should recieve a compilation error.
Expand|Select|Wrap|Line Numbers
  1. char d = 'a'; //d is a char not a string
cheers
Oct 9 '07 #2

P: 14
Sorry if I was not clear,
I have the following code:

class encrypt
{
public string enkrypt(string enc)
{

char[] thechars = enc.ToCharArray();
string tata = "";
foreach (char d in thechars)
{
int intASC = System.Convert.ToInt32(d);
tata = intASC.ToString();
}

return (tata);

}
}

now, when I call it, I get the value in this case 97.
encrypt enc = new encrypt();
MessageBox.Show(enc.enkrypt("A"));

what I am looking for now is a function that will do the opposite of
int intASC = System.Convert.ToInt32(d);

I want to take 97 and translate it to A ,
I am sure it is simple so forgive me if it sounds like stupid question.

Thanks,
-Kate


Do you mean you want to convert it from the ascii value to the char value?


Expand|Select|Wrap|Line Numbers
  1.  char x = Convert.ToChar(intASC);
PS: From your code that you gave, you should recieve a compilation error.
Expand|Select|Wrap|Line Numbers
  1. char d = 'a'; //d is a char not a string
cheers
Oct 9 '07 #3

Shashi Sadasivan
Expert 100+
P: 1,435
That is going to be tough, especially with no index.
your ascii values could be from a single digit to 3 digits long
so the string 167 could be interpreted in a lot of different ways.

Guess we need to come up with something better
Oct 9 '07 #4

Plater
Expert 5K+
P: 7,872
Consider this:

Expand|Select|Wrap|Line Numbers
  1. char myc = 'a';
  2. int myint = (int)myc;
  3. //now myint will = 97 (the ascii value)
  4. myint++;//make myint=98;
  5. char myreturnc= (char)myint;
  6. //now myreturnc='b'
  7.  
Oct 9 '07 #5

P: 14
its working!!
thank you,
sorry for the questions,
it just seems that unlike php finding how to do the simple things in C#
is very difficult.

-Kate
Oct 9 '07 #6

Plater
Expert 5K+
P: 7,872
Hehe, but if you know java, C# can be done in your sleep.
Oct 9 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.