Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have requirement such as I am converting a normal string into a bytearray and then storing it into a file. I want to read the byte array again from the file to convert it into normal string.
I have tried the following code but none seems to work



C#
    private void button1_Click(object sender, RoutedEventArgs e)
    {
        int i = 0;
        int j = 0;
        Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();

        // Set filter for file extension and default file extension
        dlg.DefaultExt = ".txt";
        dlg.Filter = "Text documents (.txt)|*.txt";

        // Display OpenFileDialog by calling ShowDialog method
        Nullable<bool> result = dlg.ShowDialog();

        // Get the selected file name and display in a TextBox
        if (result == true)
        {
            button2.IsEnabled = true;
            // Open document
            string filename = dlg.FileName;
            try
            {
                using (StreamReader sr = new StreamReader(filename))
                {
                    String line;

                    while ((line = sr.ReadLine()) != null)
                    {
                        if (line.Contains("Vault Serial"))
                        {
                            string[] words = line.Split(':');
                            vault_srno = words[1].Trim();
                            textBox1.Text = vault_srno;
                        }
                        if (line.Contains("Vault Angel Product Key"))
                        {
                            string[] words=line.Split(':');
                            emailnotification_srno = words[1].Trim();
                            textBox2.Text = emailnotification_srno;
                        }
                        if (line.Contains("Number of Licenses"))
                        {
                            string[] words = line.Split(':');
                            number_users = words[1].Trim();
                            textBox3.Text = number_users;
                        }
                        if (line.Contains("License Type"))
                        {
                            string[] words = line.Split(':');
                            lic_type = words[1].Trim();
                            textBox4.Text = lic_type;
                        }
                        if (line.Contains("Vault selected to configure"))
                        {
                            string[] words = line.Split(':');
                            vaults[i,0] = words[1].Trim();
                        }
                        if (line.Contains("Number of Users"))
                        {
                            string[] words = line.Split(':');
                            vaults[i, 1] = words[1].Trim();
                            i++;
                        }
                    }
                }
                while (vaults[j, 0] != null || vaults[j,1]!=null)
                {
                    sb.Append(vaults[j,0]);
                    sb.Append(" ");
                    sb.Append(vaults[j, 1]);
                    sb.Append(" ");
                    /*textBlock1.Text = vaults[j, 0];
                    textBlock1.Text = " ";
                    textBlock1.Text = vaults[j, 1];
                    textBlock1.Text = " ";*/
                    j++;
                }
                textBlock1.Text = sb.ToString();
                /*for (int j = 0; j < vaults.Length/2; j++)
                {
                    MessageBox.Show(vaults[j, 0]);
                    MessageBox.Show(vaults[j, 1]);
                }*/
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }

    private void button2_Click(object sender, RoutedEventArgs e)
    {
        parsefields();
    }
    public string ConvertStringToHex(String input, System.Text.Encoding encoding)
    {
        Byte[] stringBytes = encoding.GetBytes(input);
        StringBuilder sbBytes = new StringBuilder(stringBytes.Length * 2);
        foreach (byte b in stringBytes)
        {
            sbBytes.AppendFormat("{0:X2}", b);

        }
        return sbBytes.ToString();
    }
    private void parsefields()
    {
        int i = 0;
        StringBuilder sb = new StringBuilder();
        byte[] vaultBytes = Encoding.ASCII.GetBytes(vault_srno);
        byte[] emailBytes = Encoding.ASCII.GetBytes(emailnotification_srno);
        byte[] numb_lic = Encoding.ASCII.GetBytes(number_users);
        byte[] type = Encoding.ASCII.GetBytes(lic_type);
        byte[] notif = Encoding.ASCII.GetBytes("VaultSerialNo");
        byte[] notif2 = Encoding.ASCII.GetBytes("EmailNotificationNo");
        byte[] notif3 = Encoding.ASCII.GetBytes("NumberOfLicenses");
        byte[] notif4 = Encoding.ASCII.GetBytes("LicenseType");
        byte[] notif5 = Encoding.ASCII.GetBytes("Vaults");
        /*foreach (byte b in notif)
            sb.Append(b);*/
        foreach (byte b in vaultBytes)
            sb.Append(b);
        sb.Append("/");
        foreach (byte b in notif2)
            sb.Append(b);
        foreach (byte b in emailBytes)
            sb.Append(b);
        sb.Append("/");
        foreach (byte b in notif3)
            sb.Append(b);
        foreach (byte b in numb_lic)
            sb.Append(b);
        sb.Append("/");
        foreach (byte b in notif4)
            sb.Append(b);
        foreach (byte b in type)
            sb.Append(b);
        sb.Append("/");
        foreach (byte b in notif5)
            sb.Append(b);
        while(vaults[i,0]!=null || vaults[i,1]!=null)
        {
            foreach (byte b in vaults[i, 0])
                sb.Append(b);
            sb.Append("-");
            foreach (byte b in vaults[i, 1])
                sb.Append(b);
            sb.Append(",");
            i++;
        }
        unparse(sb.ToString());
        try
        {
            StreamWriter sw = new StreamWriter("C:\\Vault Angel Licences\\VA_" + vault_srno + ".lic");
            sw.WriteLine(sb.ToString());

            sw.Close();
            MessageBox.Show("Vault Angel licence successfully exported to C:\\Vault Angel Licences\\" + vault_srno + ".lic");

        }
        catch
        {
            MessageBox.Show("Error in Saving");
        }
    }
    static byte[] GetBytes(string str)
    {
        byte[] bytes = new byte[str.Length * sizeof(char)];
        System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
        return bytes;
    }
    private void unparse(string ps)
    {
        string[] words = ps.Split('/');
        textBox6.Text = words[0];
        textBox7.Text = words[1];
        textBox8.Text = words[2];
        textBox9.Text = words[3];
        textBox10.Text = words[4];
        //byte[] byteval=new byte[200];
        byte[] value = GetBytes(words[0]);
        //string something = Encoding.ASCII.GetString(words[0]);
    }
}
Posted
Comments
Bernhard Hiller 15-Apr-14 4:02am    
The requirement does not make sense at all. Are you sure you understood it correctly?
johannesnestler 15-Apr-14 9:54am    
Looks like very much code for a simple thing. Maybe do some proper (binary) serialization instead? What you want to achieve there? security by obscurity? Convert ASCII (!) string to bytes doesn not make much sense...

1 solution

Have a look at this: ByteArrayBuilder - a StringBuilder for Bytes[^] - it's what I use to do that: it lets me build up an array of bytes from the standard datatypes and then I save it to disk. When I read the file back, I can "undo" the array back to the standard datatypes very easily.

And it's pretty easy to add encryption to the byte array so your licence data is harder to read...

BTW: It's a bad idea to use a hardcoded folder, and especially one hanging off the root of a specific drive - and when that is normally the boot drive it's a very poor idea. What happens if my boot drive is read only?
Instead, consider keeping your data is a more appropriate place: Where should I store my data?[^] - that way teh system will keep track of it for you and you won't have problems.
 
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