Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: Converting byte[] to UTF8 encoded string

I am using a library called EXIFextractor to extract metadata information from images. This lib in part is using System.Drawing.Imaging.PropertyItem to do all the hard work. Some of the data in PropertyItem, such as Image Details etcetera, are fetched as an ASCII-string stored in a byte[] according to the Microsoft documentation.

My problem is that international characters (å, ä, ö, etcetera) are dropped and replaced by questionmarks. When I debug the code it is apparent that the byte[] is a representation of an UTF-8.

I'd like to parse the byte[] as an UTF8-string, how can I do this without loosing any information in the process?

Thanks in advance!


Update:

I have been asked to provide a snippet from my code:

The first snippet is from the class I use, namely the EXIFextractor.cs written by Asim Goheer

foreach( System.Drawing.Imaging.PropertyItem p in parr )
{
 string v = ""; 

                // ...

 else if( p.Type == 0x2 )
 {
  // string     
  v = ascii.GetString(p.Value);
 }

And this is my code where I try my best to handle the results of the above.

                try {
  EXIFextractor exif = new EXIFextractor(ref bmp, "");
  object o;
                    if ((o = exif["Image Description"]) != null)
                        MediaFile.Description = Tools.UTF8Encode(o.ToString()); 

I have also tried a couple of other ways of getting my precious å, ä, ö from the data, but nothing seems to do the trick. I am starting to think Hans Passant is right about his conclusions in his answer below.

like image 959
dotmartin Avatar asked Aug 04 '10 13:08

dotmartin


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is C full form?

History: The name C is derived from an earlier programming language called BCPL (Basic Combined Programming Language). BCPL had another language based on it called B: the first letter in BCPL.


2 Answers

string yourText = System.Text.Encoding.UTF8.GetString(yourByteArray);
like image 145
Scoregraphic Avatar answered Oct 12 '22 05:10

Scoregraphic


Use the GetString method on the Encoding.UTF8 object.

like image 4
Tim Robinson Avatar answered Oct 12 '22 05:10

Tim Robinson