[Solved]Why wstring data is different from its raw data

General FreeBASIC programming questions.
guawoo
Posts: 2
Joined: Mar 11, 2019 2:43

[Solved]Why wstring data is different from its raw data

Postby guawoo » Mar 16, 2019 6:32

look at code

dim as wstring * 10 ws = "你好"

printf("%x \n", ws[0]) 'print 4f60
printf("%x \n", ws[1]) 'print 597d

printf(!"%ws\n", ws) 'print 你好

Dim As Zstring Ptr zsp

zsp = Strptr(ws)

For i As Integer = 0 To 3
printf(!"%x ", (*zsp)[i]) 'here , print 60 4f 7d 59, It's different from the print above.
Next

'I output it to file

Dim As FILE Ptr f

f=fopen("debug.txt", "wb")
If f<> 0 Then
fputs(Strptr(ws), f)
End If
fclose(f)

'then file raw data also is 60 4f 7d 59 , It's bad order, "你好"characters can't be show correctly, the right order is 4f 60 59 7d


It's a bug? how do I get the order right?
Last edited by guawoo on Mar 16, 2019 9:40, edited 2 times in total.
jj2007
Posts: 1215
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: Why wstring data is different from its raw data

Postby jj2007 » Mar 16, 2019 7:22

It depends on what is "right" for you. Under the hood, memory is
60 4F 7D 59 你好

You are under Windows or Linux?

The issue here is endianness:
printf("0: %x \n", ws[0]) ' print 4f60 this is a WORD
printf("%x ", (*zsp)[i]) ' this prints BYTEs
guawoo
Posts: 2
Joined: Mar 11, 2019 2:43

Re: Why wstring data is different from its raw data

Postby guawoo » Mar 16, 2019 9:39

oh, thank you @jj2007, your answer inspired me! I get the problem with my code, the source file encoding was wrong, now I changed it to utf-8.bom from utf-8, that everything work ok:)

(btw: I use windows)

Thank you so much.
jj2007
Posts: 1215
Joined: Oct 23, 2016 15:28
Location: Roma, Italia
Contact:

Re: [Solved]Why wstring data is different from its raw data

Postby jj2007 » Mar 16, 2019 9:41

Glad I could help, and welcome to the forum!

Return to “General”

Who is online

Users browsing this forum: No registered users and 21 guests