String format

Just starting out? Need help? Post your questions and find answers here.
Rookie
Enthusiast
Enthusiast
Posts: 106
Joined: Tue May 20, 2003 4:16 pm
Location: Florida
Contact:

String format

Post by Rookie »

I am have a bit of problems with a program I am writing. The file needs to encoded in UTF16BE. I have confirm this with Notepad and the makers of the hard the config file is for. Thing is, when I use WriteStringFormat, I do not get the same results as if I were to edit the config file in Notepad. I also notice when I use the IDE's file viewer, the unedited config file doesn't look like the one I make. Any idea.

Copies of the File Viewer.
This a unedited version of the config file needed. This will work with no problems

Code: Select all

00000000  FF FE 43 00 6F 00 6C 00 6F 00 72 00 3D 00 31 00  ÿþC.o.l.o.r.=.3.
00000010  36 00 34 00 31 00 35 00 0D 00 0A 00 54 00 65 00  2.7.6.7.....T.e.
00000020  78 00 74 00 3D 00 20 00 20 00 20 00 20 00 20 00  x.t.=. . . . . .
00000030  20 00 53 00 41 00 56 00 45 00 20 00 46 00 49 00   .S.A.V.E. .F.I.
00000040  4C 00 45 00 20 00 43 00 41 00 4E 00 20 00 4E 00  L.E. .C.A.N. .N.
00000050  4F 00 54 00 20 00 42 00 45 00 20 00 43 00 52 00  O.T. .B.E. .C.R.
00000060  45 00 41 00 54 00 45 00 44 00 0D 00 0A 00 54 00  E.A.T.E.D.....T.
00000070  65 00 78 00 74 00 3D 00 20 00 20 00 20 00 20 00  e.x.t.=. . . . .
00000080  20 00 20 00 54 00 52 00 59 00 20 00 4D 00 41 00   . .T.R.Y. .M.A.
00000090  4E 00 55 00 41 00 4C 00 5B 00 42 00 5D 00 20 00  N.U.A.L.[.B.]. .
000000A0  54 00 4F 00 20 00 53 00 45 00 4C 00 45 00 43 00  T.O. .S.E.L.E.C.
000000B0  54 00 20 00 47 00 41 00 4D 00 45 00 0D 00 0A 00  T. .G.A.M.E.....
000000C0  54 00 65 00 78 00 74 00 3D 00 20 00 20 00 20 00  T.e.x.t.=. . . .
000000D0  20 00 20 00 20 00 20 00 20 00 20 00 20 00 20 00   . . . . . . . .
000000E0  20 00 20 00 20 00 20 00 20 00 20 00 20 00 5B 00   . . . . . . .[.
000000F0  42 00 5D 00 20 00 42 00 41 00 43 00 4B 00 0D 00  B.]. .B.A.C.K...
00000100  0A 00 54 00 65 00 78 00 74 00 3D 00 20 00 20 00  ..T.e.x.t.=. . .
00000110  20 00 20 00 20 00 20 00 20 00 20 00 20 00 20 00   . . . . . . . .
00000120  20 00 20 00 4C 00 4F 00 41 00 44 00 49 00 4E 00   . .L.O.A.D.I.N.
00000130  47 00 20 00 53 00 41 00 56 00 45 00 20 00 46 00  G. .S.A.V.E. .F.
00000140  49 00 4C 00 45 00 2E 00 2E 00 2E 00 0D 00 0A 00  I.L.E...........
00000150  54 00 65 00 78 00 74 00 3D 00 20 00 20 00 20 00  T.e.x.t.=. . . .
00000160  20 00 20 00 20 00 20 00 20 00 20 00 20 00 20 00   . . . . . . . .
00000170  20 00 20 00 4C 00 4F 00 41 00 44 00 49 00 4E 00   . .L.O.A.D.I.N.
00000180  47 00 20 00 47 00 41 00 4D 00 45 00 2E 00 2E 00  G. .G.A.M.E.....
00000190  2E 00 0D 00 0A 00 54 00 65 00 78 00 74 00 3D 00  ......T.e.x.t.=.
000001A0  20 00 20 00 20 00 20 00 20 00 20 00 20 00 20 00   . . . . . . . .
000001B0  20 00 20 00 20 00 20 00 20 00 20 00 20 00 20 00   . . . . . . . .
000001C0  20 00 53 00 41 00 56 00 49 00 4E 00 47 00 2E 00   .S.A.V.I.N.G...
000001D0  2E 00 2E 00 0D 00 0A 00 54 00 65 00 78 00 74 00  ........T.e.x.t.
000001E0  3D 00 61 00 75 00 74 00 6F 00 6D 00 61 00 74 00  =.a.u.t.o.m.a.t.
000001F0  69 00 63 00 20 00 73 00 61 00 76 00 65 00 0D 00  i.c. .s.a.v.e...
00000200  0A 00 54 00 65 00 78 00 74 00 3D 00 4F 00 4E 00  ..T.e.x.t.=.O.N.
00000210  0D 00 0A 00 54 00 65 00 78 00 74 00 3D 00 4F 00  ....T.e.x.t.=.O.
00000220  46 00 46 00 0D 00 0A 00 54 00 65 00 78 00 74 00  F.F.....T.e.x.t.
00000230  3D 00 44 00 41 00 54 00 45 00 0D 00 0A 00 54 00  =.D.A.T.E.....T.
00000240  65 00 78 00 74 00 3D 00 54 00 49 00 4D 00 45 00  e.x.t.=.T.I.M.E.
00000250  0D 00 0A 00 54 00 65 00 78 00 74 00 3D 00 43 00  ....T.e.x.t.=.C.
00000260  4C 00 4F 00 43 00 4B 00 0D 00 0A 00 54 00 65 00  L.O.C.K.....T.e.
00000270  78 00 74 00 3D 00 54 00 45 00 4D 00 50 00 45 00  x.t.=.T.E.M.P.E.
00000280  52 00 41 00 54 00 55 00 52 00 45 00 0D 00 0A 00  R.A.T.U.R.E.....
00000290  54 00 65 00 78 00 74 00 3D 00 53 00 50 00 45 00  T.e.x.t.=.S.P.E.
000002A0  45 00 44 00 0D 00 0A 00 54 00 65 00 78 00 74 00  E.D.....T.e.x.t.
000002B0  3D 00 43 00 48 00 45 00 41 00 54 00 20 00 43 00  =.C.H.E.A.T. .C.
000002C0  4F 00 44 00 45 00 0D 00 0A 00 54 00 65 00 78 00  O.D.E.....T.e.x.
000002D0  74 00 3D 00 53 00 4F 00 46 00 54 00 20 00 52 00  t.=.S.O.F.T. .R.
000002E0  45 00 53 00 45 00 54 00 0D 00 0A 00 00           E.S.E.T......
Now here is what the File View shows when I make a config file with my program.

Code: Select all

þÿColor=32767
Text=      SAVE FILE CAN NOT BE CREATED
Text=      TRY MANUAL[B] TO SELECT GAME
Text=                  [B] BACK
Text=            LOADING SAVE FILE...
Text=             LOADING GAME...
Text=                 SAVING...
Text=automatic save
Text=ON
Text=OFF
Text=DATE
Text=TIME
Text=CLOCK
Text=TEMPERATURE
Text=SPEED
TEXT=CHEAT CODE
TEXT=SOFT RESET
Insanity is over rated, unless you can join me in mine.
Don't post questions at work
Don't post questions at work
Don't post questions at work
Rookie
Enthusiast
Enthusiast
Posts: 106
Joined: Tue May 20, 2003 4:16 pm
Location: Florida
Contact:

Post by Rookie »

Here is also the bit of code that writes my config file.

Code: Select all

Input.s=PathRequester("Please select the correct folder","C:\")
  If CreateFile(0, Input+"menu.txt")
    WriteStringFormat(0, #PB_UTF16BE)
    WriteStringN(0,"Color="+GetGadgetText(#Value))     ;Grabs the users data and outputs into a Text=value form
    WriteStringN(0,"Text="+GetGadgetText(#SaveError))  ;Grabs the users data and outputs into a Text=value form
    WriteStringN(0,"Text="+GetGadgetText(#ManualLoad)) ;Grabs the users data and outputs into a Text=value form
    WriteStringN(0,"Text="+GetGadgetText(#Back))       ;Grabs the users data and outputs into a Text=value form
    WriteStringN(0,"Text="+GetGadgetText(#LoadSave))   ;Grabs the users data and outputs into a Text=value form
    WriteStringN(0,"Text="+GetGadgetText(#LoadGame))   ;Grabs the users data and outputs into a Text=value form
    WriteStringN(0,"Text="+GetGadgetText(#Save))       ;Grabs the users data and outputs into a Text=value form
    WriteStringN(0,"Text=automatic save")              ;Part of the config file that doesn't need to be changed
    WriteStringN(0,"Text="+GetGadgetText(#On))         ;;Grabs the users data and outputs into a Text=value form
    WriteStringN(0,"Text="+GetGadgetText(#Off))        ;Grabs the users data and outputs into a Text=value form
    WriteStringN(0,"Text="+GetGadgetText(#Date))       ;Grabs the users data and outputs into a Text=value form
    WriteStringN(0,"Text="+GetGadgetText(#Time))       ;Grabs the users data and outputs into a Text=value form
    WriteStringN(0,"Text="+GetGadgetText(#Clock))      ;Grabs the users data and outputs into a Text=value form
    WriteStringN(0,"Text="+GetGadgetText(#Temp))       ;Grabs the users data and outputs into a Text=value form
    WriteStringN(0,"Text="+GetGadgetText(#Speed))      ;Grabs the users data and outputs into a Text=value form
    WriteStringN(0,"TEXT="+GetGadgetText(#Cheats))
    WriteStringN(0,"TEXT="+GetGadgetText(#SoftReset))
    End
  Else
    MessageRequester("Error", "The file menu.txt could not be created"+Chr(10)+"Please Email me at: DarkRookie@gmail.com"+Chr(10)+"Please include in the subject: Ver 1.0 Line 83 (File Failed)",16)
    End
  EndIf
Insanity is over rated, unless you can join me in mine.
Don't post questions at work
Don't post questions at work
Don't post questions at work
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Post by Demivec »

about WriteStringFormat the manual wrote:The #PB_Ascii, #PB_UTF8 and #PB_Unicode correspond to the flags supported by WriteString() and WriteStringN(). After placing such a BOM, the following strings should all be written with this flag. The other formats are represent string formats that cannot be directly written with PB string functions. They are included for completeness only.
You are writing a format string followed by a string in a different format.

In other words, you write the correct string BOM format for #PB_UTF16BE followed by strings written in a different format (PB doesn't write in UTF16BE format).
Last edited by Demivec on Tue Jul 08, 2008 8:52 am, edited 3 times in total.
User avatar
pdwyer
Addict
Addict
Posts: 2813
Joined: Tue May 08, 2007 1:27 pm
Location: Chiba, Japan

Post by pdwyer »

The first two characters are the BOM (Byte order marker) and are 245,255 note that in the hex dump they are the reverse order to the config file. That means one is BE and the other is LE. (or atleast that is how they are marked)

(Or I'm not understanding your question)
Paul Dwyer

“In nature, it’s not the strongest nor the most intelligent who survives. It’s the most adaptable to change” - Charles Darwin
“If you can't explain it to a six-year old you really don't understand it yourself.” - Albert Einstein
Rookie
Enthusiast
Enthusiast
Posts: 106
Joined: Tue May 20, 2003 4:16 pm
Location: Florida
Contact:

Post by Rookie »

@ Demivec
Since PB cant due it directly, is there a way to get around it.
Insanity is over rated, unless you can join me in mine.
Don't post questions at work
Don't post questions at work
Don't post questions at work
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Post by Rook Zimbabwe »

1. Get TEXT
2. Convert TEXT to UTF***
3. WriteN UTF***

:D
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
Rookie
Enthusiast
Enthusiast
Posts: 106
Joined: Tue May 20, 2003 4:16 pm
Location: Florida
Contact:

Post by Rookie »

That is the problem
That doesnt work
Insanity is over rated, unless you can join me in mine.
Don't post questions at work
Don't post questions at work
Don't post questions at work
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

- create fixed length strings or a memory block
- drop zeroes in between the chars
- then save the memory to disk
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
User avatar
Rook Zimbabwe
Addict
Addict
Posts: 4322
Joined: Tue Jan 02, 2007 8:16 pm
Location: Cypress TX
Contact:

Post by Rook Zimbabwe »

Actually I get an error:

Code: Select all

Input.s=PathRequester("Please select the correct folder","C:\")
  If CreateFile(0, Input+"menu.txt")
    WriteStringFormat(0, #PB_UTF16BE)
    WriteStringN(0,"Color=VIOLET")     ;Grabs the users data and outputs into a Text=value form
    WriteStringN(0,"ChowderMonkey=CHUNKY")
    WriteStringN(0,"Batman=CLOSET")  
    End
  Else
    MessageRequester("Error", "The file menu.txt could not be created"+Chr(10)+"Please Email me at: DarkRookie@gmail.com"+Chr(10)+"Please include in the subject: Ver 1.0 Line 83 (File Failed)",16)
    End
  EndIf
My error is: Current Format is not supported by Pure Basic

And yet with my PB4.2 docs it is! :evil:
Binarily speaking... it takes 10 to Tango!!!

Image
http://www.bluemesapc.com/
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Post by Demivec »

Here's a procedure I wrote called WriteStringUTF16BE(), it can be used in place of WriteString() and WriteStringN().

It adds only one optional parameter to cover whether or not you want a newline written. It should use the default newline for whichever OS it's compiled with.
  • Use WriteStringUTF16BE(filenum,text$) in place of WriteString(filenum,text$)
    and WriteStringUTF16BE(filenum,text$,1) in place of WriteStringN(filenum,text$)
I've added the procedure to the beginning of the code sample you provided and modified your code appropriately to show how it is used.

The function also returns the length written or -1 if it was unable to obtain memory for its buffer.

Code: Select all

Procedure.l WriteStringUTF16BE(filenum.l,asciiText.s,newline.l = 0) ;newline = 1 if the default newline string should be written after the string
  ;Author: Demivec (Jared)
  ;PureBasic v4.20
  ;Description:  to enable writing of Ascii string data to a file in UTF16BE format with or without a newline

  Protected i.l, *buffer.l,*buffPtr.l,*textPtr.l
  Static newline$ = #CRLF$

  *buffer = AllocateMemory((Len(asciiText) + Len(newline$)) * 2)
  If *buffer
    *buffPtr = *buffer
    *textPtr = @asciiText
    For i = 1 To Len(asciiText)
      PokeB(*buffPtr,PeekB(*textPtr))
      *buffPtr + 2
      *textPtr + 1
    Next 
    If newline
      *textPtr = @newline$
      For i = 1 To Len(newline$)
        PokeB(*buffPtr,PeekB(*textPtr))
        *buffPtr + 2
        *textPtr + 1
      Next
    EndIf 
    WriteData(filenum,*buffer,*buffPtr - *buffer)
    ProcedureReturn *buffPtr - *buffer ;return length written
  Else
    ProcedureReturn 0
  EndIf 
EndProcedure

Input.s=PathRequester("Please select the correct folder","C:\")
  If CreateFile(0, Input+"menu.txt")
    WriteStringFormat(0, #PB_UTF16BE)
WriteStringUTF16BE(0,"Color="+GetGadgetText(#Value),1)     ;Grabs the users data and outputs into a Text=value form
  WriteStringUTF16BE(0,"Text="+GetGadgetText(#SaveError),1)  ;Grabs the users data and outputs into a Text=value form
  WriteStringUTF16BE(0,"Text="+GetGadgetText(#ManualLoad),1) ;Grabs the users data and outputs into a Text=value form
  WriteStringUTF16BE(0,"Text="+GetGadgetText(#Back),1)       ;Grabs the users data and outputs into a Text=value form
  WriteStringUTF16BE(0,"Text="+GetGadgetText(#LoadSave),1)   ;Grabs the users data and outputs into a Text=value form
  WriteStringUTF16BE(0,"Text="+GetGadgetText(#LoadGame),1)   ;Grabs the users data and outputs into a Text=value form
  WriteStringUTF16BE(0,"Text="+GetGadgetText(#Save),1)       ;Grabs the users data and outputs into a Text=value form
  WriteStringUTF16BE(0,"Text=automatic save",1)              ;Part of the config file that doesn't need to be changed
  WriteStringUTF16BE(0,"Text="+GetGadgetText(#On),1)         ;;Grabs the users data and outputs into a Text=value form
  WriteStringUTF16BE(0,"Text="+GetGadgetText(#Off),1)        ;Grabs the users data and outputs into a Text=value form
  WriteStringUTF16BE(0,"Text="+GetGadgetText(#Date),1)       ;Grabs the users data and outputs into a Text=value form
  WriteStringUTF16BE(0,"Text="+GetGadgetText(#Time),1)       ;Grabs the users data and outputs into a Text=value form
  WriteStringUTF16BE(0,"Text="+GetGadgetText(#Clock),1)      ;Grabs the users data and outputs into a Text=value form
  WriteStringUTF16BE(0,"Text="+GetGadgetText(#Temp),1)       ;Grabs the users data and outputs into a Text=value form
  WriteStringUTF16BE(0,"Text="+GetGadgetText(#Speed),1)      ;Grabs the users data and outputs into a Text=value form
  WriteStringUTF16BE(0,"TEXT="+GetGadgetText(#Cheats),1)
  WriteStringUTF16BE(0,"TEXT="+GetGadgetText(#SoftReset),1)
    End
  Else
    MessageRequester("Error", "The file menu.txt could not be created"+Chr(10)+"Please Email me at: DarkRookie@gmail.com"+Chr(10)+"Please include in the subject: Ver 1.0 Line 83 (File Failed)",16)
    End
  EndIf
Rookie
Enthusiast
Enthusiast
Posts: 106
Joined: Tue May 20, 2003 4:16 pm
Location: Florida
Contact:

Post by Rookie »

@Demivec
That code worked. Was trying to do something like that on my own.
Honest to say I was getting no where. Thanks a lot.
Insanity is over rated, unless you can join me in mine.
Don't post questions at work
Don't post questions at work
Don't post questions at work
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Post by Demivec »

Your welcome. The procedure also could be extended without much effort to do UTF16LE, as well as a few other things.
Rookie
Enthusiast
Enthusiast
Posts: 106
Joined: Tue May 20, 2003 4:16 pm
Location: Florida
Contact:

Re: String format

Post by Rookie »

I have been working on some my old code and trying to compile this in 4.40 and it was writen in 4.20.
The line:

Code: Select all

Static newline$ = #CRLF$
Is throwing the error that constant is not found.
Insanity is over rated, unless you can join me in mine.
Don't post questions at work
Don't post questions at work
Don't post questions at work
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: String format

Post by Demivec »

It works under windows x86.

You can also define it if need be. In either case it should work as is.
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: String format

Post by rsts »

try adding

#CRLF$ = Chr(13) + Chr(10)

cheers
Post Reply