PokeS() with Ascii format

Just starting out? Need help? Post your questions and find answers here.
papet34
User
User
Posts: 11
Joined: Fri Feb 15, 2019 11:16 am
Location: France

PokeS() with Ascii format

Post by papet34 »

Hi,
Retired developer, I continue to program for fun. I discovered Pure Basic nine months ago and enjoy converting old programs in Mac OS.
I have a little problem that looks very much like a bug.
Here is a piece of code to illustrate this abnormal behavior: we see that the same string loses its accents (cf gT $ (4) vs gT $ (3)).
Note that the writing of gT $ (4), character by character, gives the same result ...
Is a bug or am I affected by the age limit?
Sorry for my poor english
j @ cques

Code: Select all

Dim gT$(4)
  ; initialisation
  nbElem = 4
  gT$(1) = "Préférences.res"
  gT$(2) = "Préférences.res"
  gT$(3) = "Préférences.res"
  gT$(4) = "Préférences.res" ;!!! idem les précédents mais perd les accents
  ; si on remplace les 4 lignes précédentes par la suivante les accents sont bien conservés !
  ; For i = 1 To 4 : gT$(i) = "Préférences.res" : Next i
  
  ; écriture
  ; le bloc mémoire est constitué :
  ;   - un word pour mémoriser le nombre d'éléments
  ;   - et pour chaque élément :
  ;     . un asciiCharacter pour mémoriser la longueur de la chaîne (limitée à 255 caractères)
  ;     . la chaîne en Ascii 
  For i = 1 To nbElem : lgT + Len(gT$(i)) + 1 : Next i  
  *mb = AllocateMemory(2 + lgT + nbElem) ; nbElem pour l'octet de longueur
  PokeW(*mb, nbElem) : k = 2 ; pour pointer sur les éléments
  For i = 1 To nbElem
    lg = Len(gT$(i))
    PokeA(*mb + k, lg) : PokeS(*mb + k + 1, gT$(i), lg, #PB_Ascii)
    ; même comportement en remplacant le PokeS() par :
    ;For j = 1 To lg
    ;  PokeA(*mb + k + j, Asc(Mid(gT$(i), j, 1)))
    ;Next j
    k + 1 + lg
  Next i
  
  ; lecture
  nbElem = PeekW(*mb) : k = 2 : i = 1
  While i <= nbElem
    lg = PeekA(*mb + k)
    Debug PeekS(*mb + k + 1, lg, #PB_Ascii)
    i + 1 : k + 1 + lg
  Wend
  FreeMemory(*mb)

;  -> 
;Préférences.res
;Préférences.res
;Préférences.res
;Preferences.res
__________________________________________________
Code tags added
08.07.2019
RSBasic
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: PokeS() with Ascii format

Post by mk-soft »

Works fine here... PB v5.70 and PB v5.71; Xcode 9.0 and Xcode 10.x

P.S.
Check your PB-Preferences
- Compiler -> Defaults - Sourcefile Text Encoding = UTF8

The serialization of the data works

Code: Select all

Dim gT$(4)
  ; initialisation
  nbElem = 4
  gT$(1) = "Préférences1.res"
  gT$(2) = "Préférences20.res"
  gT$(3) = "Préférences300.res"
  gT$(4) = "Préférences4000.res" ;!!! idem les précédents mais perd les accents
  ; si on remplace les 4 lignes précédentes par la suivante les accents sont bien conservés !
  ; For i = 1 To 4 : gT$(i) = "Préférences.res" : Next i
 
  ; écriture
  ; le bloc mémoire est constitué :
  ;   - un word pour mémoriser le nombre d'éléments
  ;   - et pour chaque élément :
  ;     . un asciiCharacter pour mémoriser la longueur de la chaîne (limitée à 255 caractères)
  ;     . la chaîne en Ascii
  For i = 1 To nbElem : lgT + Len(gT$(i)) + 1 : Next i 
  *mb = AllocateMemory(2 + lgT + nbElem) ; nbElem pour l'octet de longueur
  PokeW(*mb, nbElem) : k = 2 ; pour pointer sur les éléments
  For i = 1 To nbElem
    lg = Len(gT$(i))
    PokeA(*mb + k, lg) : PokeS(*mb + k + 1, gT$(i), lg, #PB_Ascii)
    ; même comportement en remplacant le PokeS() par :
    ;For j = 1 To lg
    ;  PokeA(*mb + k + j, Asc(Mid(gT$(i), j, 1)))
    ;Next j
    k + 1 + lg
  Next i
  
  ShowMemoryViewer(*mb, 200)
  ; lecture
  nbElem = PeekW(*mb) : k = 2 : i = 1
  While i <= nbElem
    lg = PeekA(*mb + k)
    Debug PeekS(*mb + k + 1, lg, #PB_Ascii)
    i + 1 : k + 1 + lg
  Wend
  FreeMemory(*mb)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
papet34
User
User
Posts: 11
Joined: Fri Feb 15, 2019 11:16 am
Location: France

Re: PokeS() with Ascii format

Post by papet34 »

Hello mk-soft,
With your program, slightly modified compared to mine, it works too but if I restart my example I always lose accents ... Do you get a correct result with my source unchanged?
I checked the PB preferences (these are the original ones: Source Text Encoding UTF8)
PureBasic 5.70 LTS (MacOS X - x64)
Thank you for the test.
Sorry for my poor english
j@cques
User avatar
Danilo
Addict
Addict
Posts: 3037
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: PokeS() with Ascii format

Post by Danilo »

PB is using Unicode strings internally, and when you convert strings to ASCII it is always possible some chars get lost. It may depend on the codepage the user is using.

Code: Select all

s.s = "äöüß - éêî - 你好 - สวัสดี"

Debug "Ascii"
*memory1 = AllocateMemory( Len(s)+1 )
If *memory1
    Debug "Memory: "+MemorySize(*memory1)
    PokeS( *memory1, s, Len(s), #PB_Ascii )
    Debug PeekS( *memory1, Len(s), #PB_Ascii )
    FreeMemory( *memory1 )
EndIf

Debug "Utf8"
*memory2 = AllocateMemory( StringByteLength(s,#PB_UTF8)+1 )
If *memory2
    Debug "Memory: "+MemorySize(*memory2)
    PokeS( *memory2, s, Len(s), #PB_UTF8 )
    Debug PeekS( *memory2, Len(s), #PB_UTF8 )
    FreeMemory( *memory2 )
EndIf

Debug "Unicode"
*memory3 = AllocateMemory( (Len(s)+1)*2 )
If *memory3
    Debug "Memory: "+MemorySize(*memory3)
    PokeS( *memory3, s, Len(s), #PB_Unicode )
    Debug PeekS( *memory3, Len(s), #PB_Unicode )
    FreeMemory( *memory3 )
EndIf
To make sure nothing gets lost it is better to save strings as Unicode or UTF8.
papet34
User
User
Posts: 11
Joined: Fri Feb 15, 2019 11:16 am
Location: France

Re: PokeS() with Ascii format

Post by papet34 »

Thanks Danilo. I am not affected by the age limit ! ;-)
Sorry for my poor english
j@cques
Post Reply