Scripting & Programming

MySQL procedure for converting UTF8 to CP1251 encoding and vice versa

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

And converting CP1251 to UTF8 encoding is done this way:

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

Leave a Reply