1

I am using libiconv to convert array of char to encoded string. I am new to the library. I wonder how I can know what encoded type the given array is encoded in before I call iconv_open("To-be-encoded","given-encoded-type")

It's the second parameter that I need to know.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • `iconv_open` needs to know them. You should already know them. You didn't mention what platform you're on, but I would suggest `iconv --list` from a terminal prompt on Unix with any reasonable iconv support. They are system-dependent. [Example from OS X 10.10.1](http://pastebin.com/HgdrCwS5) – WhozCraig Jan 17 '15 at 11:06

1 Answers1

2

It's the second parameter that I need to know.

Yes, you indeed need to know it. I.e. it is you that needs to tell iconv what encoding your array is in. There is no reliable way of detecting what encoding was used to produce a set of bytes - at best you can take a guess based on character frequencies or other such heuristics.

But there is no way to be 100% sure without other information, from metadata or from the file/data format itself. (e.g. HTTP provides headers to indicate encoding, XML has that capability too.)

In other words, if you don't know how a stream of bytes you have is encoded, you cannot convert it to anything else. You need to know the starting point.

Community
  • 1
  • 1
Mat
  • 202,337
  • 40
  • 393
  • 406