MultiByte to UTF-8

Share your advanced PureBasic knowledge/code with the community.
User avatar
oryaaaaa
Addict
Addict
Posts: 825
Joined: Mon Jan 12, 2004 11:40 pm
Location: Okazaki, JAPAN

MultiByte to UTF-8

Post by oryaaaaa »

Code updated For 5.20+

Code: Select all

#CP_UTF8=65001
in.s = "ABCDEF"
l = MultiByteToWideChar_(#CP_OEMCP,0,@in,-1,0,0) 
l = MultiByteToWideChar_(#CP_OEMCP,0,@in,-1,*out,l) 
l = WideCharToMultiByte_(#CP_UTF8,0,*out,-1,0,0,0,0) 
out2.s = Space(l) 
l = WideCharToMultiByte_(#CP_UTF8,0,*out,-1,@out2,l,0,0) 
Debug out2
Windows98/Me/2K/XP (Win95 is removed.)

Because I had not had the example of converting UTF-8, it contributed.
When HTML is output, this is used.

Thanks
Last edited by oryaaaaa on Wed Aug 24, 2005 2:53 pm, edited 1 time in total.
User avatar
oryaaaaa
Addict
Addict
Posts: 825
Joined: Mon Jan 12, 2004 11:40 pm
Location: Okazaki, JAPAN

Post by oryaaaaa »

Code: Select all

Procedure.s UTF8toMB(U2S.s)
  #CP_UTF8=65001
  *u2s_out = AllocateMemory(1024) 
  l = MultiByteToWideChar_(#CP_UTF8,0,@U2S,-1,0,0) 
  l = MultiByteToWideChar_(#CP_UTF8,0,@U2S,-1,*u2s_out,l) 
  l = WideCharToMultiByte_(#CP_ACP,0,*u2s_out,-1,0,0,0,0) 
  u2s_out2.s = Space(l) 
  l = WideCharToMultiByte_(#CP_ACP,0,*u2s_out,-1,@u2s_out2,l,0,0)
  FreeMemory(*u2s_out)
  ProcedureReturn u2s_out2
EndProcedure

Procedure.s MBtoUTF8(s2u.s)
  #CP_UTF8=65001
  *s2u_out = AllocateMemory(1024) 
  l = MultiByteToWideChar_(#CP_OEMCP,0,@s2u,-1,0,0) 
  l = MultiByteToWideChar_(#CP_OEMCP,0,@s2u,-1,*s2u_out,l) 
  l = WideCharToMultiByte_(#CP_UTF8,0,*s2u_out,-1,0,0,0,0) 
  s2u_out2.s = Space(l) 
  l = WideCharToMultiByte_(#CP_UTF8,0,*s2u_out,-1,@s2u_out2,l,0,0)
  FreeMemory(*s2u_out)
  ProcedureReturn s2u_out2
EndProcedure
Sample - LeftUTF8
viewtopic.php?t=15200
Sample -
Image
User avatar
oryaaaaa
Addict
Addict
Posts: 825
Joined: Mon Jan 12, 2004 11:40 pm
Location: Okazaki, JAPAN

Post by oryaaaaa »

SJIS is MultiByteCharactor. I changed it into MultiByte. :D
Post Reply