LeftUTF8(String, Pos)

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

LeftUTF8(String, Pos)

Post by oryaaaaa »

Code updated For 5.20+

Code: Select all

Procedure.s LeftUTF8(Utf8.s, Utf8_index.b)
  Utf8_result.s="" : Utf8_Total.b=1
  Utf8_len=Len(Utf8)
  For Utf8_pos=1 To Utf8_len
    If Asc(Mid(Utf8, Utf8_pos, 1))<$80
      If Utf8_index=>Utf8_Total
        Utf8_result+Mid(Utf8, Utf8_pos, 1)
        Utf8_Total+1
      EndIf
    ElseIf Asc(Mid(Utf8, Utf8_pos, 1))>$C1 And Asc(Mid(Utf8, Utf8_pos, 1))<$E0
      If Utf8_index=>(Utf8_Total+1)
        Utf8_result+Mid(Utf8, Utf8_pos, 2)
        Utf8_pos+1
        Utf8_Total+2
      EndIf
    ElseIf Asc(Mid(Utf8, Utf8_pos, 1))>$DF And Asc(Mid(Utf8, Utf8_pos, 1))<$F0
      If Utf8_index=>(Utf8_Total+1)
        Utf8_result+Mid(Utf8, Utf8_pos, 3)
        Utf8_pos+2
        Utf8_Total+2
      EndIf
    Else
      If Utf8_index=>(Utf8_Total+1)
        Utf8_result+Mid(Utf8, Utf8_pos, 4)
        Utf8_pos+3
        Utf8_Total+2
      EndIf 
    EndIf 
  Next
  ProcedureReturn Utf8_result 
EndProcedure
Return UTF-8 Strings

TX
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Post by Droopy »

what's this function do ? :shock:
Xombie
Addict
Addict
Posts: 898
Joined: Thu Jul 01, 2004 2:51 am
Location: Tacoma, WA
Contact:

Post by Xombie »

Aren't UTF-8 strings wide character?
User avatar
oryaaaaa
Addict
Addict
Posts: 825
Joined: Mon Jan 12, 2004 11:40 pm
Location: Okazaki, JAPAN

Post by oryaaaaa »

Oh...

UTF-8 (WideCharactor ex:Japanese, Korea, China... etc) :)
zikitrake
Addict
Addict
Posts: 876
Joined: Thu Mar 25, 2004 2:15 pm
Location: Spain

Post by zikitrake »

A sample, please...

My ZikiTranslator will be more nice if I could add it Chinese and Japanese languages.

thank you!
PB 6.21 beta, PureVision User
Blade
Enthusiast
Enthusiast
Posts: 362
Joined: Wed Aug 06, 2003 2:49 pm
Location: Venice - Italy, Japan when possible.
Contact:

Post by Blade »

@oryaaaaa
Hi, in my system (Win2000 wich usually handles correctly japanese filenames, despite it's not a Win-J) command such as OpenFile fails when trying to load those files...
Do you have the same problem in your (I assume) Japanese Windows?
Have you found some solution?
User avatar
oryaaaaa
Addict
Addict
Posts: 825
Joined: Mon Jan 12, 2004 11:40 pm
Location: Okazaki, JAPAN

Post by oryaaaaa »

Sample Japanese
Image

Code: Select all

Procedure.s UTF8toSJIS(U2S.s)
  *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 SJIStoUTF8(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
kore wa SJIS denakutemo iikamo...
User avatar
oryaaaaa
Addict
Addict
Posts: 825
Joined: Mon Jan 12, 2004 11:40 pm
Location: Okazaki, JAPAN

Post by oryaaaaa »

@blade

Code: Select all

For i=0 To 100
  String1.s=""
  For S1.b=0 To 70
    String1.s+Chr(Random(100)+64)
  Next
  If CreateFile(1,String1)
  UseFile(1)
  WriteStringN("TEST")
  CloseFile(1)
  EndIf 
  If ReadFile(1, String1)
    Debug "OK"
    ;Debug String1
    CloseFile(1)
  Else
    Debug "NG"
    ;Debug String1 
  EndIf
  DeleteFile(String1)
Next
Windows Japanese Edition (Win2000-SP4-JPN)
NG
NG
NG
NG
NG
NG
NG
OK
OK
OK
OK
NG
NG
NG
OK
NG
NG
OK
OK
NG
NG
OK
OK
NG
NG
OK
NG
NG
OK
NG
OK
OK
OK
NG
OK
NG
NG
OK
OK
OK
NG
OK
NG
NG
NG
NG
NG
NG
OK
NG
NG
NG
NG
NG
OK
NG
NG
NG
NG
OK
NG
OK
NG
NG
NG
NG
NG
NG
NG
NG
NG
OK
NG
NG
OK
NG
NG
NG
NG
NG
OK
OK
NG
NG
OK
NG
NG
OK
NG
NG
NG
NG
NG
NG
NG
NG
NG
OK
NG
NG
OK
A better test doesn't hit on.
Post Reply