Read a specific UTF-8 file

Just starting out? Need help? Post your questions and find answers here.
Lebostein
Addict
Addict
Posts: 807
Joined: Fri Jun 11, 2004 7:07 am

Read a specific UTF-8 file

Post by Lebostein »

I have a file with following content:

Code: Select all

...  <sets>
    <setID>26190</setID>
    <number>71014</number>
    <numberVariant>1</numberVariant>
    <name>Joachim Löw</name>
    <year>2016</year>
...
How use Readfile() + Readstring() to get a "Joachim Löw" in String?

If I use #PB_Ascii for Readstring() then I get this:

Code: Select all

    <name>Joachim Löw</name>
User avatar
HeX0R
Addict
Addict
Posts: 992
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: Read a specific UTF-8 file

Post by HeX0R »

It seems it has been stored wrong.
You need to first read it as UTF8, then poke it into a buffer as ASCII, then peek it back as UTF8
Then you can store the new file with a UTF8 BOM and all should be fine.
Lebostein
Addict
Addict
Posts: 807
Joined: Fri Jun 11, 2004 7:07 am

Re: Read a specific UTF-8 file

Post by Lebostein »

I use that function to receive these files from an API url:

Code: Select all

Procedure.s GetString(url.s): Protected HttpRequest, result.s, *Response

  HttpRequest = HTTPRequest(#PB_HTTP_Get, url)
  If HttpRequest
    If HTTPInfo(HttpRequest, #PB_HTTP_StatusCode) = "200"
      *Response = HTTPMemory(HTTPRequest)
      result = PeekS(*Response, MemorySize(*Response), #PB_Ascii)
      FreeMemory(*Response)
    EndIf
    FinishHTTP(HTTPRequest)
  EndIf
  ProcedureReturn result

EndProcedure
But if I remove the #PB_Ascii-Falg, then I get nothing....

Edit: Ahhh!!! I have to use #PB_UTF8 | #PB_ByteLength then it works!!!
Lebostein
Addict
Addict
Posts: 807
Joined: Fri Jun 11, 2004 7:07 am

Re: Read a specific UTF-8 file

Post by Lebostein »

Thanks for the hint with "stored wrong"! :wink:
Post Reply