-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?

3
  • @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 Commented Feb 8, 2019 at 7:19
  • 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. Commented Feb 8, 2019 at 8:10

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

6 Comments

How about this : BitConverter.ToString(ba).Replace("-",""); as given here stackoverflow.com/questions/311165/…
@Learning-Overthinker-Confused That will convert the bits to a hexadecimal string. Is that what you're trying to do?
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
@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?
@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.
|
0

Why did use need XmlConvert? Did you try this?

virtual string ToString(byte[] value) => System.Text.Encoding.Default.GetString(value);

1 Comment

Sorry but i guess this make byte[] conversion to string locale dependent that is what i am trying to avoid using XmlConvert class

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.