A new Unicode & Newbie problem

New to FreeBASIC? Post your questions here.
newbieforever
Posts: 117
Joined: Jun 21, 2018 11:14

Re: A new Unicode & Newbie problem

Post by newbieforever »

Wow...wow...wow!!! What magic wand is this now?! And it really works!!!

And you, Josep, are the magician! Thank you very much!!!

(Btw: What a strange dynamic this thread had! At first everything was unclear and it seemed that there was no solution. But then, by my naive questioning (and by my small rather random discovery to the variable won by LoadFile()) you decided to reach deep into your magic box! Or did you already know before that the solution goes in this direction?)

(Btw 2: I was extremely confused because I saw that the 2-byte code of the two "e" in "Сергeй" was not identical, and initially thought the method was not working properly. Only after a while did I realize: I copied "Сергeй" from somewhere, and the original writer typed two different "e", a Cyrillic one and a Latin one––both look the same.)
dodicat
Posts: 7979
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: A new Unicode & Newbie problem

Post by dodicat »

I tried a union with this.

Code: Select all





const size=1000

type ub
 as ubyte b(1 to size*2)
end type
union wsize
as wstring * size w
as ub b
end union

function getstring(z as wsize) as string
dim as string g
for n as long=1 to size*2
g+=chr(z.b.b(n))
next n
return g
end function

#Include "file.bi"


Sub savefile(filename As String,p As string)
    Dim As Integer n
    n=Freefile
    If Open (filename For Binary Access Write As #n)=0 Then
        Put #n,,p
        Close
    Else
        Print "Unable to save " + filename
    End If
End Sub

Function loadfile(file as string) as wString ptr
	If FileExists(file)=0 Then Print file;" not found":Sleep:end
   var  f=freefile
    Open file For Binary Access Read As #f
    static As wString * size text
    If Lof(f) > 0 Then
      text = String(Lof(f), 0)
      Get #f, , text
    End If
    Close #f
    return @text
end Function



dim as wsize x
dim as wstring * size w= "Сергeй"

for n as long=1 to 7
w+=w
next
w+=chr(10)+" Please close notepad if it is showing"
print "The wstring:"
print w
print

x.w=w
print "text to save to file:"
print getstring(x)

savefile("tester.txt",getstring(x))

shell "notepad tester.txt"
print
print
print "retrieve from file"
dim as wstring ptr  wp=loadfile("tester.txt")
print *wp

sleep
'kill "tester.txt"


  
I had to use the Poseidon Ide to handle unicode.
Post Reply