I don't really care about encoding and stuff, as long as I get back the exact same byte array.  So to sum up: How do I convert a byte array into a string, and then that string back into the same byte array I started with?
39676
How do I convert a string to a byte[] in .NET (C#) without manually specifying a specific encoding?  I'm going to encrypt the string. I can encrypt it without converting, but I'd still like to know why encoding comes to play here.  Also, why should encoding even be taken into consideration? Can't I simply get what bytes the string has been stored in? Why is there a dependency on character encodings?
103070
I know how to do it in WinForms  byte[] binaryData = Convert.FromBase64String(bgImage64); image = Image.FromStream(new MemoryStream(binaryData));   but how do i do the same thing in WPF?
21914
I have 3 byte arrays in C# that I need to combine into one. What would be the most efficient method to complete this task?
2376
I was surprised to find today that I couldn't track down any simple way to write the contents of an InputStream to an OutputStream in Java. Obviously, the byte buffer code isn't difficult to write, but I suspect I'm just missing something which would make my life easier (and the code clearer).  So, given an InputStream in and an OutputStream out, is there a simpler way to write the following?  byte[] buffer = new byte[1024]; int len = in.read(buffer); while (len != -1) {     out.write(buffer, 0, len);     len = in.read(buffer); }
872
In Java, arrays don't override toString(), so if you try to print one directly, you get the className + '@' + the hex of the  of the array, as defined by Object.toString():  int[] intArray = new int[] {1, 2, 3, 4, 5}; System.out.println(intArray);     // prints something like '[I@3343c8b3'   But usually, we'd actually want something more like [1, 2, 3, 4, 5]. What's the simplest way of doing that? Here are some example inputs and outputs:  // Array of primitives: int[] intArray = new int[] {1, 2, 3, 4, 5}; //output: [1, 2, 3, 4, 5]  // Array of object references: String[] strArray = new String[] {"John", "Mary", "Bob"}; //output: [John, Mary, Bob]
5845
I tried to use java.io.FileReader to read some text files and convert them into a string, but I found the result is wrongly encoded and not readable at all.  Here's my environment:   Windows 2003, OS encoding: CP1252 Java 5.0   My files are UTF-8 encoded or CP1252 encoded, and some of them (UTF-8 encoded files)  may contain Chinese (non-Latin) characters.  I use the following code to do my work:     private static String readFileAsString(String filePath)     throws java.io.IOException{         StringBuffer fileData = new StringBuffer(1000);         FileReader reader = new FileReader(filePath);         //System.out.println(reader.getEncoding());         BufferedReader reader = new BufferedReader(reader);         char[] buf = new char[1024];         int numRead=0;         while((numRead=reader.read(buf)) != -1){             String readData = String.valueOf(buf, 0, numRead);             fileData.append(readData);             buf = new char[1024];         }         reader.close();         return fileData.toString();     }   The above code doesn't work. I found the FileReader's encoding is CP1252 even if the text is UTF-8 encoded. But the JavaDoc of java.io.FileReader says that:     The constructors of this class assume   that the default character encoding   and the default byte-buffer size are   appropriate.   Does this mean that I am not required to set character encoding by myself if I am using FileReader? But I did get wrongly encoded data currently, what's the correct way to deal with my situtaion? Thanks.
110846
While developing some code that uses the .NET AesManaged algorithm, I made some mistakes but was surprised at the results.  My encryption was correct.  I was generating a random IV block and writing it to the file (without encrypting the IV).  Then I was encrypting the plaintext and writing it.  The decryption was wrong though... instead of reading the first block from the file an using it as an IV, it was just using a random IV and decrypting everything.  The first block came out gibberish but the 2nd (and subsequent) ones were decrypted properly!  Why would this be?  And even weirder... when I tried to fix it by encrypting the IV in the encryption code (instead of writing the IV unchanged) the decryption result was the same!  (gibberish then correctly decrypted blocks).  Although I eventually fixed my code, how do these results make sense?
122863
I have a string, lets say "123|ABC|test|12345|FF" and I want to xor the ascii value of each character and print the result in hex.  What is the simplest way?
80408
What are the differences between UTF-8, UTF-16, and UTF-32?  I understand that they will all store Unicode, and that each uses a different number of bytes to represent a character. Is there an advantage to choosing one over the other?
5691
Is there a better way of getting this result? This function fails if num has more digits than digits, and I feel like it should be in the library somewhere (like Integer.toString(x,"%3d") or something)  static String intToString(int num, int digits) {     StringBuffer s = new StringBuffer(digits);     int zeroes = digits - (int) (Math.log(num) / Math.log(10)) - 1;      for (int i = 0; i &lt; zeroes; i++) {         s.append(0);     }     return s.append(num).toString(); }
75487
VB has a couple of native functions for converting a char to an ASCII value and vice versa  - Asc() and Chr().  Now I need to get the equivalent functionality in C#.  What's the best way?
36464
How do you do it? Given a byte array:  byte[] foo = new byte[4096];   How would I get the first x bytes of the array as a separate array? (Specifically, I need it as an IEnumerable&lt;byte&gt;)  This is for working with Sockets. I figure the easiest way would be array slicing, similar to Perls syntax:  @bar = @foo[0..40];   Which would return the first 41 elements into the @bar array. Is there something in C# that I'm just missing, or is there some other thing I should be doing?  LINQ is an option for me (.NET 3.5), if that helps any.
99569
Is there a way to write binary literals in C#, like prefixing hexadecimal with 0x? 0b doesn't work.  If not, what is an easy way to do it? Some kind of string conversion?
2031
How can you convert a byte array to a hexadecimal string, and vice versa?
120476
I'm looking into a mechanism for serialize data to be passed over a socket or shared-memory in a language-independent mechanism.  I'm reluctant to use XML since this data is going to be very structured, and encoding/decoding speed is vital.  Having a good C API that's liberally licensed is important, but ideally there should be support for a ton of other languages.  I've looked at  and .  Am I on the right track?  Is there something better?  Should I just implement my own packed structure and not look for some standard?
26241
I have the following string array:  var sa = new string[] {"yabba","dabba","doo"};   I can convert it to "yabba, dabba, doo" it using string.Join() but what is the super-cool LINQ way of doing it?  The Join extension method seems promising but for a novice like me very confusing.
31237
How can I do this fast?  Sure I can do this:  static bool ByteArrayCompare(byte[] a1, byte[] a2) {     if (a1.Length != a2.Length)         return false;      for (int i=0; i&lt;a1.Length; i++)         if (a1[i]!=a2[i])             return false;      return true; }   But I'm looking for either a  function or some highly optimized proven way to do this.  java.util.Arrays.equals((sbyte[])(Array)a1, (sbyte[])(Array)a2);   works nicely, but it doesn't look like that would work for x64.  Note my super-fast answer .