Click here to Skip to main content
15,893,644 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am working on a project where I have to convert password into ASCII values. The code I wrote is
C#
con.Open();
    string var = "ashwani";
    int i ;
   
    int varLen = var.Length;
    for (i = 0; i <= varLen; i++)
    {
      
       string aaa = var.Substring(i, 1);
       byte[] ddd= Encoding.ASCII.GetBytes(aaa);
       String zz = Convert.ToString(xxx) + 290 + i;
       string main = string.Empty;
       main = main + zz;

    }

In variable zz it returns me System.Byte[]2900 but not the ascii value of alphabet "a'

plz ..help me
Posted
Updated 13-Jun-13 21:48pm
v2
Comments
Richard MacCutchan 14-Jun-13 4:00am    
This won't even compile; what is the value of xxx?

C#
string var = "ashwani";
byte[] ddd = System.Text.Encoding.ASCII.GetBytes(var);

ddd now contains the entire string in ASCII.
 
Share this answer
 
Try This Solution...

C#
class Program
    {
        static void Main(string[] args)
        {
            string var = "ashwani";
            var sample= var.ToCharArray();
            string main = string.Empty;
            foreach(char a in sample)
            {
                byte[] ddd = Encoding.ASCII.GetBytes(Convert.ToString(a));
                main += ddd[0];
            }
            Console.WriteLine(main);
            Console.ReadKey();
        }
    }
 
Share this answer
 
v2
Comments
johannesnestler 14-Jun-13 4:36am    
why are you converting to char Array and then convert each byte individually? This is inefficent and not neccessary - look at my solution..
SPASWIN 14-Jun-13 10:21am    
@johannesnestler ..Yes i agree Sir.
Hello ashwani,

I assume you are a beginner, and this is kind of "homework" project?
I can't resist to give you a tip learned in painfull lessons: the most important thing about programming is: inventing names and remembering them. those names are the symbols in your head for the problem domain. So: give your variables always meaningful names (aaa, xxx is not so good ;-) and using var as variable name is even worse - because it's a keyword! (I don't tell you now how to use keywords as names - it's possible - but you should never do it!)

To your problem:

I can't figure out what your code is trying to achive. Why are you adding 290 + counter variable i to a string? So forgive me if I got you wrong, I just present a solution for what I think you want:

C#
string strPassword = "ashwani"; // example password
 // Assume the password string is ASCII, so let's get the individual bytes and store them in a variable
 byte[] abyPasswordBytes = Encoding.ASCII.GetBytes(strPassword);

 // Now we can do something with the bytes
 // It seems you tried to write them to a string - let's do that
 string strAllByteValues = String.Empty;
 foreach (byte by in abyPasswordBytes)
 {
     strAllByteValues += by.ToString() + " "; // we add a space as separator
 }
 // the variable strAllByteValues now holds the string "97 115 104 119 97 110 105 "


So you see: after calling GetBytes you already have the individual bytes for your string - it seems you just got confused how to use them later...

So, good luck with your project and kind regards

Johannes
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900