Click here to Skip to main content
15,909,373 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i am using string s=Encoding.ASCII.GetString(bytes) output shoul be 111187686557 but it gives
111187686557\0\0\0\0\ etc many times \0 plz help me?
byte array shows 10 value and other values are 0 but while converting to the string it takes 0 as \0 help me
Posted
Updated 11-Feb-14 22:22pm
v2

Why do you think if should give this string? If it does not, the content stored in your bytes is really not what you expected encoded as ASCII.

It could be anything: 1) your original string could be encoded not as ASCII, 2) your original string could really contain many zeroes; this is not that silly C-style null-terminated string; in .NET strings, null is valid; 3) it could be anything else. If you had to serialize some string, deserialize it and compare the result of this round-trip operation, it would be a different story. If you show the code making bytes from your string, we can consider it.

—SA
 
Share this answer
 
Comments
Garth J Lancaster 12-Feb-14 2:18am    
beats my first comment Sergey :-) - I said use s.Trim() d'oh forgetting the input was byte[] .. sigh
Sergey Alexandrovich Kryukov 12-Feb-14 2:26am    
Trim is irrelevant. By the way, Trim() without parameters should not trim those nulls (surprise?). When you serialize the string if becomes array of byte. Deserialize it with the same encoding, and it will give the same string. No matter what it was. But it the character repertoire of the encoding (ASCII! generally, should not be used with .NET), it won't be the case. Only UTFs guarantee correct round trip.
—SA
you do realise that

s=Encoding.ASCII.GetString(bytes)


can also be specified

s=Encoding.ASCII.GetString(bytes, offset, length)


offset is likely 0, as per SA's comments, its up to you to determine the correct length

'g'
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 12-Feb-14 2:28am    
Please see my comment to your comment to my answer. ASCII does not guarantee correct round trip. Further discussion is pretty much useless, unless OP clearly shows how bytes were obtained.
—SA

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