Subject: |Overstocked Auto lots| (2014 models)

From: Auto Overstock <[email-masked]>
Date: Tue, 27 May 2014 11:00:17 -0700

Do the recent Automobile Recalls have you worried about your families safety?

Take a deep breath.....

Dealers have teamed up and are offering a special Clearance
-to put you in a new safe vehicle

View Clearance Inventory ->  http://www.gidoval.com/summer/auto/clearance.index

_Dealer Clearance Update_

- send modifications - Update Notice Messages
118 Kecks Lane Ruff Dale, PA 15679 or http://www.gidoval.com/ihg/sfgk.s49tf
@Travis: The last thing I will tell you (because I just noticed it right now) is to read this answer below. I had already mentioned this before, but since that answer actually demonstrates it, I'll say it again: my answer saves & restores the string perfectly; encoding-based methods fail to work on char sequences that can't be represented correctly.   Mehrdad Aug 6 '13 at 0:41 
3	  	
@Mehrdad, thanks for the recommendation. Downvoted.   Travis Aug 6 '13 at 14:23
1	  	
Isn't it that, because .NET uses UTF-16 internally and 16 bit characters therefore, a string in this example is in fact encoded using UTF-16? If you use Encoding.Unicode.GetString(), which is UTF-16, on the byte-array created in this example, it produces the original string value.   Kai Hartmann Aug 9 '13 at 22:31
1	  	
Yes, this answer works for niche use cases. But other answers work for all use cases. Why not use the superior (and just as easy to use... and less error prone) techniques that require you to type in an encoding? Giving this a big fat -1 because of that.   Thomas Eding Sep 27 '13 at 18:16
  	  	
@Thomas: No, the other answers don't work for all use cases. You came to that conclusion without reading Michael Buen's answer.   Mehrdad Sep 27 '13 at 21:40
1	  	
@Mehrdad: I didn't say "all other answers work for all use cases" (see the extra all). Pedantic reply for a pseudo-pedantic response.   Thomas Eding Sep 27 '13 at 22:15 
2	  	
@Thomas: I never said you said that either; you didn't understand what I'm trying to tell you. Did you read his answer? His answer tells you why mine can handle cases that none of the other answers can. None of the answers here handles all cases, but mine handles the relevant cases to the OP.   Mehrdad Sep 27 '13 at 22:44 
  	  	
@Mehrdad: Fair enough. But I still don't like this solution. Not sure completely about his (I don't feel like learning about unpaired surrogates at the moment, but it seems like it is something along the lines of a trap representation).   Thomas Eding Sep 27 '13 at 22:56 
  	  	
@Thomas: I don't really care if you "like" the solution (heck, I don't particularly either), but you can't deny that it's the only correct answer given here for the OP's use case (conversion between strings and byte arrays). The other answers destroy some char sequences in the process, mine doesn't. Keep your downvote, but please think twice before hopping on the bandwagon and spreading misinformation.   Mehrdad Sep 27 '13 at 23:01 
  	  	
@Mehrdad: How do I convert a string to a byte array in .NET (C#)? is the OP's described use case. Literally any answer that returns a byte[] would be technically correct. But I'm done with this extended chat.   Thomas Eding Sep 27 '13 at 23:03 
  	  	
@ThomasEding: If all you wanted to do was to downvote this answer because it doesn't please you, then I don't see why you bothered to comment in the first place. Don't bother doing it next time, otherwise people might think you actually care to know the reason behind the answer you're commenting on, and they'll waste their time trying to explain it to you.   Mehrdad Sep 27 '13 at 23:19 
2	  	
"Worrying about encodings just makes your life more complicated for no real reason." - Er, except that the answers that do worry about encoding are much simpler than this one. And of course, this answer still does rely on a particular encoding - str.ToCharArray() must rely on an encoding, even if that encoding is not explicitly mentioned in the code (which can only be considered bad). I respect you a lot, Mehrdad, but this is a terrible answer.   BlueRaja - Danny Pflughoeft Oct 1 '13 at 19:16 
  	  	
@BlueRaja-DannyPflughoeft: Read my comments above. The abstraction layer we concern ourselves with here (i.e. the need for perfect 1:1 reconstruction on a given system) isn't the same as when you worry about encodings (i.e. interoperability with another system). They're two completely unrelated concerns and the former has nothing to do with encoding (and in fact cannot be done with any encoding scheme here).   Mehrdad Oct 1 '13 at 19:47 
1	  	
This does not keep the encoding intact. Too bad this is the accepted answer with the highest votes because I just wasted 2 hours trying to find out why my strings get garbled. Chased it down to a method that used this answer to convert string -> byte[].   user1151923 Oct 9 '13 at 12:26
  	  	
@user1151923: Can you show me an example of an actual string that gets garbled? I can't fix the answer if you don't tell me how to reproduce the problem...   Mehrdad Oct 9 '13 at 19:29 
  	  	
var input = "   "; var ms = new MemoryStream(GetBytes(input)); var sr = new StreamReader(ms); var output = sr.ReadToEnd(); output is B5AB   user1151923 Oct 10 '13 at 13:43
  	  	
I would add that I don't think "For those goals" is a justification to an answer that [sometimes] messes up encoding. What people are gonna see when they open this question is the question (.NET String to byte Array C#) and a highly rated answer claiming you don't need to worry about encoding in bold text (which by the way is missing the "for those goals" part). There are answers below that are shorter or just as long and that keep the encoding intact regardless of where and how you use the code.   user1151923 Oct 10 '13 at 13:49 
3	  	
@user1151923: Dude, the problem is with your code, not my answer! You're using GetBytes to convert a string to bytes, but you're not using GetString to go the reverse direction! These are supposed to be used in pairs; you can't just do whatever you feel like and expect it to work. If you don't use encodings one way you also have to ignore them in the reverse direction, but you ignored the fact that StreamReader is encoding-based! Read my comment earlier: stackoverflow.com/questions/472906/   Mehrdad Oct 10 '13 at 18:11 
  	  	
@user1151923: And before you blame me for not warning you, realize that what has happened in your code is exactly equivalent to using new StreamReader(stream).ReadToEnd() to go in one direction, but using Encoding.UTF8 to go in the other direction. It's wrong because the writer was careless, and it has nothing to do with the answer that might have told you to use UTF8. If the fact that StreamReader uses UTF-16 by default is confusing, don't blame it on my answer; it's not my fault it was designed that way.   Mehrdad Oct 10 '13 at 18:26 
2	  	
@Mehrad Just because your answer is technically correct in this case doesn't make it a good answer for the reasons stated by many before me. It's like recommending the goto statement when better alternatives are available because "well it works in this case doesn't it?". This site is meant for answers that will function properly for as many usecases as possible within the scope of the question. You announcing "YOU DONT NEED ENCODING" in a big size at the top of your answer while leaving the main caveat as a little side note at the bottom could lead to problems.   Leon Lucardie Oct 23 '13 at 10:48 
6	  	
@LeonLucardie: The other alternatives aren't "better"; in fact, they're worse because they break on strings that can't be encoded correctly (such as those that contain unpaired surrogates). I've mentioned this a million times now, but apparently it's very convenient for people to ignore this fact...   Mehrdad Oct 23 '13 at 10:51 
2	  	
@Mehrdad Even in a perfect world where people would act professional, they won't take the time to do a little research. I'm almost positive that all pros and cons of this solution have been addressed in the comments here, as well as in the other answers. If there are still those who won't realize this fact and feel that continuing to argue (even 1.25 years later) over points that have already been addressed then it's not worth your time nor anyone else's to argue further. There are answers here that apply to both 'need-encoding' and 'don't-need-encoding' use cases; it's as simple as that.