1

Hi I am trying to convert my video file name that i have extracted from a folder to a byte array. My file name goes "video.mp4"

I have tried using this set of codes

byte[] bytes = System.IO.File.ReadAllBytes(filename);

but it does not work. Anyone?

Uwe Keim
  • 36,867
  • 50
  • 163
  • 268
heyhihello
  • 39
  • 2
  • 2
    What do you mean by "but it does not work"?. Be specific – Sriram Sakthivel Aug 10 '15 at 10:22
  • If i guess right, you are passing the filename, not the path to the file. But your question is confuse. Do you want to convert the file to a bytes array, or the name of the file (just the string) ? – Guillaume Beauvois Aug 10 '15 at 10:24
  • 3
    @Nalaka we [don't add "thanks" to posts](http://stackoverflow.com/review/suggested-edits/9107510). I see in your history you've been doing that to quite some posts. Please stop that. – CodeCaster Aug 10 '15 at 10:25
  • @heyhihello please provide more info about why your code is not working – DanielV Aug 10 '15 at 10:37
  • To clarify, are you wanting to get the **name** or the **contents** into a byte array? Your code suggests you want to read the contents, however the question subjects the name. – Andre Andersen Aug 10 '15 at 10:39

1 Answers1

-1

Another (maybe easier way) to convert a string to a byte array goes like this:

string filename = "video.mp4";
byte[] byteArray = Encoding.ASCII.GetBytes(filename);

To convert the byte array back to a string you can use:

string filename = Encoding.ASCII.GetString(byteArray);

For this to work you'll need to import the System.Text namespace.

Maurits van Beusekom
  • 3,264
  • 1
  • 12
  • 30
  • Please read the comments. It is not clear what OP wants to do. If it is _"Get a string's characters in a byte array"_, then [that's a duplicate of this one](http://stackoverflow.com/questions/472906/converting-a-string-to-byte-array-without-using-an-encoding-byte-by-byte). Using the ASCII encoding is also not very failsafe; filenames can use Unicode characters as well. – CodeCaster Aug 10 '15 at 12:16