It is currently Tue Nov 12, 2019 12:38 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: PokeS() with Ascii format
PostPosted: Mon Jul 08, 2019 9:34 am 
Offline
New User
New User

Joined: Fri Feb 15, 2019 11:16 am
Posts: 8
Location: France
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:
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


Top
 Profile  
Reply with quote  
 Post subject: Re: PokeS() with Ascii format
PostPosted: Mon Jul 08, 2019 7:02 pm 
Offline
Addict
Addict
User avatar

Joined: Fri May 12, 2006 6:51 pm
Posts: 2020
Location: Germany
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:
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 / OOP-BaseClassDispatch / EventDesigner V3
PB v3.30 / v5.70 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace


Top
 Profile  
Reply with quote  
 Post subject: Re: PokeS() with Ascii format
PostPosted: Tue Jul 09, 2019 5:44 am 
Offline
New User
New User

Joined: Fri Feb 15, 2019 11:16 am
Posts: 8
Location: France
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


Top
 Profile  
Reply with quote  
 Post subject: Re: PokeS() with Ascii format
PostPosted: Thu Jul 18, 2019 6:40 am 
Offline
Addict
Addict
User avatar

Joined: Sat Apr 26, 2003 8:26 am
Posts: 2926
Location: Planet Earth
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:
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: PokeS() with Ascii format
PostPosted: Thu Jul 18, 2019 8:14 am 
Offline
New User
New User

Joined: Fri Feb 15, 2019 11:16 am
Posts: 8
Location: France
Thanks Danilo. I am not affected by the age limit ! ;-)

_________________
Sorry for my poor english
j@cques


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 36 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye