How to convert between utf8 and cp1251 without iconv

When you need convert some data from utf8 to cp1251(windows-1251) or cp1251 to utf8 you must use system function iconv. Frequently the hosting providers do not allow to use this function.

Here are two php function for this operation



       function cp1251_to_utf8($s){
           $c209 = chr(209); $c208 = chr(208); $c129 = chr(129);
           for($i=0; $i<strlen($s); $i++)    {
               $c=ord($s[$i]);
               if ($c>=192 and $c<=239) $t.=$c208.chr($c-48);
               elseif ($c>239) $t.=$c209.chr($c-112);
               elseif ($c==184) $t.=$c209.$c209;
               elseif ($c==168)    $t.=$c208.$c129;
               else $t.=$s[$i];
           }
           return $t;
       }

        function utf8_to_cp1251($s)
        {
            for ($c=0;$c<strlen($s);$c++)
            {
               $i=ord($s[$c]);
               if ($i<=127) $out.=$s[$c];
                   if ($byte2){
                       $new_c2=($c1&3)*64+($i&63);
                       $new_c1=($c1>>2)&5;
                       $new_i=$new_c1*256+$new_c2;
                   if ($new_i==1025){
                       $out_i=168;
                   } else {
                       if ($new_i==1105){
                           $out_i=184;
                       } else {
                           $out_i=$new_i-848;
                       }
                   }
                   $out.=chr($out_i);
                   $byte2=false;
                   }
               if (($i>>5)==6) {
                   $c1=$i;
                   $byte2=true;
               }
            }
            return $out;
        }

 

 

Comments:

Erema (12-09-2008 08:27) :
Thanks!!! :) Your code really helped me!

Pavel (10-10-2008 13:24) :
Thanks again. I was overwrite this code for MS SQL Server:
<pre>
CREATE FUNCTION STR_UTF8_1251(@s varchar(8000))
RETURNS VARCHAR(8000)
AS
BEGIN
declare @i int, @c int, @byte2 int, @c1 int, @new_c1 int, @new_i int, @new_c2 int, @out_i int
declare @out varchar(8000), @a int
SET @i = 1
SET @byte2 = 0
SET @out = ''
WHILE (@i<=len(@s))
BEGIN
SET @c=ascii(SUBSTRING(@s,@i,1))

if (@c<=127) SET @out=@out+SUBSTRING(@s,@i,1)

if (@byte2>0) BEGIN
SET @new_c2=(@c1&3)*64+(@c&63)

--Right shift @new_c1 2 bits
SET @new_c1=CAST(@c1/2 as INT)
SET @new_c1=(CAST(@new_c1/2 as INT))&5
SET @new_i = @new_c1*256+@new_c2
if (@new_i=1025) SET @out_i=168
if (@new_i=1105) SET @out_i=184
if (@new_i<>1025 and @new_i<>1105) SET @out_i=@new_i-848

SET @out = @out + char(@out_i)
SET @byte2 = 0
END

--Right shift @c 5 bits
SET @a = CAST(@c/2 as INT)
SET @a = CAST(@a/2 as INT)
SET @a = CAST(@a/2 as INT)
SET @a = CAST(@a/2 as INT)
SET @a = CAST(@a/2 as INT)

if (@a=6) BEGIN
SET @c1=@c
SET @byte2=1
END
SET @i=@i+1
END
RETURN @out
END

CREATE FUNCTION STR_1251_UTF8(@s varchar(8000))
RETURNS VARCHAR(8000)
AS
BEGIN
declare @c209 char(1), @c208 char(1), @c129 char(1), @i int, @c int, @t varchar(8000)
SET @c209 = char(209)
SET @c208 = char(208)
SET @c129 = char(129)
SET @i=1
SET @t = ''
WHILE @i<len(@s)
BEGIN
SET @c = ASCII( SUBSTRING(@s, @i, 1) )
if (@c>=192 and @c<=239) SET @t=@t+@c208+char(@c-48)
else
if (@c>239) SET @t=@t+@c209+char(@c-112)
else
if (@c=184) SET @t=@c209+@c209
else
if (@c=168) SET @t=@c208+@c129
else SET @t=@t+SUBSTRING(@s,@i,1)
SET @i=@i+1
END

RETURN @t
END
</pre>

anon (15-04-2009 18:08) :
iconv("utf8", "cp1251", "data string")

Deniska (09-09-2009 18:28) :
->anon
->iconv("utf8", "cp1251", "data string")
As you can see in topic title, it's an example of conversion without iconv extension. Yes, it's posible that this extension will not present on a server (for example - shared hosting without customization).

convert utf8 (16-11-2010 02:06) :
There is an <a href="http://www.convertunicode.com">Unicode Char Encoding Converter</a> that supports character encoding conversion between Unicode(UTF-8/UTF-16/UTF-7/UTF-32) and non Unicode(Ansi,Chinese simplified GBK, Chinese traditional BIG5, Japanese SHIFT-JIS, Japanese EUC-JP, Korean euc-kr characters set encoding etc).

slawek (24-01-2011 12:26) :
I wrote something like this:
(for polish characters)

CREATE FUNCTION STR_UTF8_1251(@s varchar(5000))
RETURNS VARCHAR(5000)
AS
BEGIN

SET @s = REPLACE(@s,'Ä„','Ą')
SET @s = REPLACE(@s,'Ä…','ą')
SET @s = REPLACE(@s,'Ć','Ć')
SET @s = REPLACE(@s,'ć','ć')
SET @s = REPLACE(@s,'Ę','Ę')
SET @s = REPLACE(@s,'Ä™','ę')
SET @s = REPLACE(@s,'Ł','Ł')
SET @s = REPLACE(@s,'Ĺ‚','ł')
SET @s = REPLACE(@s,'Ń','Ń')
SET @s = REPLACE(@s,'Ĺ„','ń')
SET @s = REPLACE(@s,'Ă“','Ó')
SET @s = REPLACE(@s,'Ăł','ó')
SET @s = REPLACE(@s,'Ĺš','Ś')
SET @s = REPLACE(@s,'Ĺ›','ś')
SET @s = REPLACE(@s,'Ĺ»','Ż')
SET @s = REPLACE(@s,'ĹĽ','ż')
SET @s = REPLACE(@s,'Ĺą','Ź')
SET @s = REPLACE(@s,'Ĺş','ź')

RETURN @s
END

Nick (14-02-2011 07:33) :
Thank you so much!!! It works=))!!

atv (09-09-2011 18:08) :
Thank you very much.
This functions really helped me.

HuMan (24-10-2011 23:39) :
Thank you for this very good post!!!
Very helpful!!!

Z&M (18-11-2011 22:59) :
Thanks very very much :) Saved my life :)

Yola (06-12-2011 13:25) :
little bit of text would be appreciated

Back to articles list

This page was last modified on 2017-03-28 05:21:43