-2

I have a class which is using XmlConvert to convert relevant datatype data in to string.Now XmlConvert has method ToString to convert int,decimal,byte data in to string but its overload doesnt have byte[].

public class StringConverter 
{
   public virtual string ToString(byte[] value) => XmlConvert.ToString(value); //Error
}

Update : Reason why i am using XmlConvert class to convert data to string is to make this conversion locale independent

Since i am tying to use XmlConvert class to convert byte[] to string in locale independent so this is not a duplicate because i have already gone through couple of links below :

How do you convert a byte array to a hexadecimal string, and vice versa?

How to convert UTF-8 byte[] to string?

It is not possible to use XmlConvert class to convert byte[] to string?

I Love Stackoverflow
  • 6,738
  • 20
  • 97
  • 216
  • @WaiHaLee I have already checked this link and i am trying to convert byte[] to string in locale independent using xmlconvert class so its not a duplicate – I Love Stackoverflow Feb 08 '19 at 07:19
  • [What every programmer absolutely, positively needs to know about encodings and character sets to work with text](http://kunststube.net/encoding/) – Wai Ha Lee Feb 08 '19 at 07:26
  • 1
    Please define what you mean by "locale independent"? Some encodings work better for some locales (due to the support character set) that is a matter of encoding choice, the actual conversion (Encoding.GetString()`) is the same. – Richard Feb 08 '19 at 08:10

2 Answers2

1

Why did use need XmlConvert? Did you try this?

virtual string ToString(byte[] value) => System.Text.Encoding.Default.GetString(value);
Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62
1

If you want to convert bytes to a string, you have to use some encoding. There's no one-size-fits-all encoding that works all the time, and so XmlConvert is not going to solve this problem for you.

But nowadays, UTF-8 is the defacto standard, so if you're not sure which one to use, that may be your best bet.

virtual string ToString(byte[] value) => System.Text.Encoding.UTF8.GetString(value);
JLRishe
  • 99,490
  • 19
  • 131
  • 169
  • How about this : BitConverter.ToString(ba).Replace("-",""); as given here https://stackoverflow.com/questions/311165/how-do-you-convert-a-byte-array-to-a-hexadecimal-string-and-vice-versa – I Love Stackoverflow Feb 08 '19 at 07:17
  • @Learning-Overthinker-Confused That will convert the bits to a hexadecimal string. Is that what you're trying to do? – JLRishe Feb 08 '19 at 07:32
  • Actually i am trying read varbinary,binary,image datatype type data from sql server using data reader from database and the answer in your code was throwing error with image datatype byte[] conversion to string – I Love Stackoverflow Feb 08 '19 at 07:35
  • 1
    @Learning-Overthinker-Confused The line in my answer is for converting a byte array representing readable text to a string of readable text. An image is another matter. If you want to represent images as strings, you have to clarify how you intend to represent them. Hexadecimal? Base64? – JLRishe Feb 08 '19 at 07:37
  • Actually i am trying to say that if i would have image converted to byte[] then code in your answer will throw error and i am trying to store this string conversion in my database column thats the purpose.If you will see data reader methods to read varbinary,image,binary datatypes,all this returns byte[] only – I Love Stackoverflow Feb 08 '19 at 07:40
  • 1
    @Learning-Overthinker-Confused Yeah, because the requirement of "I want to represent an image as a string" makes no sense on its own. You have to figure out what it means in your case to "represent an image as a string". There are various ways of representing binary values as strings (two of which I listed). You need to decide what you intend to use in your case. Until you figure out what your actual requirements are, nobody here can help you. – JLRishe Feb 08 '19 at 08:26