reading an UTF16 text file

Discuss any general programming issues here
Post Reply
xabierpayet
Posts: 291
Joined: Fri Feb 24, 2012 9:34 am

reading an UTF16 text file

Post by xabierpayet »

is possible read an UTF 16LE file in hollywood?
i´m attempting to read this file ----> http://xabierpayet.com/ext.txt
but only have garbage characters in hollywood, any idea?
plouf
Posts: 699
Joined: Sun Feb 04, 2018 11:51 pm
Location: Athens,Greece

Re: reading an UTF16 text file

Post by plouf »

not native
hollywood is utf8 only

must create a custom routine or use an external tool, like iconv
Christos
Flinx
Posts: 353
Joined: Sun Feb 14, 2021 9:54 am
Location: Germany

Re: reading an UTF16 text file

Post by Flinx »

Here is a function for reading text that I wrote without much Hollywood experience. Probably there is a faster and easier way to do this, but since it works, I haven't changed anything. However, it only reads UCS-2 because that was sufficient in my case. Of course, you could also extend it to UTF-16 with more than two bytes.

Code: Select all

Function p_ReadLine(f, UCS2LE)
	Local l$=""
	While Not Eof(f)
		If UCS2LE
			c$=Chr(ReadShort(f,#IO_LITTLEENDIAN))
		Else
			c$=Chr(ReadShort(f))
		EndIf
		If c$<>"\r" And c$<>"\n" Then l$=l$ .. c$
		If c$="\n" Then Break
	Wend
	Return(l$)
EndFunction


id=OpenFile(Nil, "ext.txt")

Local bom=ReadShort(id)
If bom=0xFEFF
	UCS2BEBOM=True
ElseIf bom=0xFFFE
	UCS2LEBOM=True
Else
	Seek(id, 0)
	If ReadByte(id)=0xEF
		If ReadByte(id)=0xBB
			If ReadByte(id)=0xBF
				UTF8BOM=True
			EndIf
		EndIf
	EndIf
	If Not UTF8BOM
		Seek(id, 0)
	EndIf
EndIf

While Not Eof(id)
	If UCS2BEBOM Or UCS2LEBOM
		CurrentLine$=p_ReadLine(id, UCS2LEBOM)
	Else
		CurrentLine$=ReadLine(id)
	EndIf
	DebugPrint(CurrentLine$)
Wend
CloseFile(id)

xabierpayet
Posts: 291
Joined: Fri Feb 24, 2012 9:34 am

Re: reading an UTF16 text file

Post by xabierpayet »

ok, thanks i must attempt read it with holliwood, but really i don´t see how yet
xabierpayet
Posts: 291
Joined: Fri Feb 24, 2012 9:34 am

Re: reading an UTF16 text file

Post by xabierpayet »

perfect, i check it now
xabierpayet
Posts: 291
Joined: Fri Feb 24, 2012 9:34 am

Re: reading an UTF16 text file

Post by xabierpayet »

Flinx wrote: Fri Feb 13, 2026 4:02 pm Here is a function for reading text that I wrote without much Hollywood experience. Probably there is a faster and easier way to do this, but since it works, I haven't changed anything. However, it only reads UCS-2 because that was sufficient in my case. Of course, you could also extend it to UTF-16 with more than two bytes.

Code: Select all

Function p_ReadLine(f, UCS2LE)
	Local l$=""
	While Not Eof(f)
		If UCS2LE
			c$=Chr(ReadShort(f,#IO_LITTLEENDIAN))
		Else
			c$=Chr(ReadShort(f))
		EndIf
		If c$<>"\r" And c$<>"\n" Then l$=l$ .. c$
		If c$="\n" Then Break
	Wend
	Return(l$)
EndFunction


id=OpenFile(Nil, "ext.txt")

Local bom=ReadShort(id)
If bom=0xFEFF
	UCS2BEBOM=True
ElseIf bom=0xFFFE
	UCS2LEBOM=True
Else
	Seek(id, 0)
	If ReadByte(id)=0xEF
		If ReadByte(id)=0xBB
			If ReadByte(id)=0xBF
				UTF8BOM=True
			EndIf
		EndIf
	EndIf
	If Not UTF8BOM
		Seek(id, 0)
	EndIf
EndIf

While Not Eof(id)
	If UCS2BEBOM Or UCS2LEBOM
		CurrentLine$=p_ReadLine(id, UCS2LEBOM)
	Else
		CurrentLine$=ReadLine(id)
	EndIf
	DebugPrint(CurrentLine$)
Wend
CloseFile(id)

Uauuuuuuuuuuu, work perfect, at the first attempt, thank you for the code
Post Reply