Question

I'm writing a windows forms application in c#. The application allows the user to select source code-files from a listbox and displays them in colored code using ScintillaNET. The files are saved as byte arrays in a database. I've managed to make the conversion from a file on my hard drive to byte array and store it. The user should also be able to edit the code and then save it to the database without having to dowload the file to their local hard drive first, I don't know how to approach this.

Basically I want to save the text from the ScintillNET control and convert it to a byte array. And the other way around, take a byte array and print out the text as it originally appeared in ScintillaNET.

Was it helpful?

Solution

You can use the "Encoding" class from System.Text.

System.Text.Encoding.Unicode.GetBytes("Example");

This will return a byte array with the bytes equivalent to the text "string" using the unicode encoding. There are other encoding available, but I suggest using unicode since it supports more characters (anything you find in windows charmap, for example). In my case is because I'm latin and certain letters aren't available in UTF and I have my doubts about ASCII.

Now to convert from the byte array to string use:

byte[] exampleByteArray = MemStream.ToArray();
System.Text.Encoding.Unicode.GetString(exampleByteArray);

This code will return the string saved previously as a byte array in a memory stream. You can load the byte array with other methods, in you your case you are gonna load it from the database and call System.Text.Encoding.Unicode.GetString().

OTHER TIPS

I believe you are looking for the System.Text.Encoding namespace...

        // a sample string...
        string example = "A string example...";

        // convert string to bytes
        byte[] bytes = Encoding.UTF8.GetBytes(example);

        // convert bytes to string
        string str = System.Text.Encoding.UTF8.GetString(bytes);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top