New topic: 

Encoding problem when replacing

<http://forums.realsoftware.com/viewtopic.php?t=41970>

         Page 1 of 1
   [ 8 posts ]                 Previous topic | Next topic          Author  
Message        vfran          Post subject: Encoding problem when 
replacingPosted: Tue Dec 27, 2011 9:55 am                         
Joined: Mon Dec 07, 2009 7:30 pm
Posts: 70                I'm trying to replace markertext %CLIENT_FIRSTNAME% 
with app.firstname in an emailtemplate.

The string 'app.firstname' coming from a database holds a foreign 
name/character eg "Søren"
When I do a replace the foreign character becomes a blank in the final text. "S 
ren"

But when adding the value "Søren" 'manually' to the string: app.firstname = 
"Søren" before replacing it works.
 
  t=f.OpenAsTextFile
  t.Encoding=Encodings.UTF8 
  Subject.text=t.ReadLine
  
  Dim result As String
  result = t.readall
  emailarea.text= Replace(result,"%CLIENT_FIRSTNAME%",app.firstname)
  
t.Close

Any ideas?   
                             Top                 mjh          Post subject: Re: 
Encoding problem when replacingPosted: Tue Dec 27, 2011 10:04 am                
                 
Joined: Sun Feb 19, 2006 3:10 pm
Posts: 1553
Location: Hamburg, Germany                Did you remember to set the encoding 
of app.firstname? Since the string originates in a database it would have no 
known encoding unless you set it. By setting the property to "Søren" in code 
you are setting it to an UTF8 string; most likely your database is using a 
different encoding. Once the encoding is known RB should do the right thing, 
even when replacing parts of a string with another string in a different 
encoding (the encoding will be converted automagically).      
_________________
Michael J. Hußmann 
http://digicam-experts.de  
                             Top                vfran          Post subject: 
Re: Encoding problem when replacingPosted: Tue Dec 27, 2011 10:14 am            
             
Joined: Mon Dec 07, 2009 7:30 pm
Posts: 70                I have tried a few different ways
eg app.firstname =app.firstname.ConvertEncoding(Encodings.UTF8)

but the same thing   
                             Top                 mjh          Post subject: Re: 
Encoding problem when replacingPosted: Tue Dec 27, 2011 10:16 am                
                 
Joined: Sun Feb 19, 2006 3:10 pm
Posts: 1553
Location: Hamburg, Germany                ConvertEncoding won’t work if the 
original encoding isn’t known. You have to set the encoding to the one you 
know the string is in. Once you’ve done that, converting the encoding to 
another is usually unnecessary.      
_________________
Michael J. Hußmann 
http://digicam-experts.de  
                             Top                vfran          Post subject: 
Re: Encoding problem when replacingPosted: Tue Dec 27, 2011 10:22 am            
             
Joined: Mon Dec 07, 2009 7:30 pm
Posts: 70                I thought that when string from database is parsed to 
an app string (app.firstname) that it is always UTF8?   
                             Top                 npalardy          Post 
subject: Re: Encoding problem when replacingPosted: Tue Dec 27, 2011 10:39 am   
                    Real Software Engineer          
Joined: Sat Dec 24, 2005 8:18 pm
Posts: 6964
Location: Canada, Alberta, Near Red Deer                vfran wrote:I thought 
that when string from database is parsed to an app string (app.firstname) that 
it is always UTF8?

No
When data comes from an outside source (database, text file, serial port, tcp 
connection, etc) you should pretty much always use DefineEncoding on that data. 
Otherwise it's just a bunch of bytes with no encoding. Any text operations 
(like replacing) may work but they may also silently fail.

Once you let Real Studio's runtimes know what the encoding is (by calling 
DefineEncoding) then things should work fine.      
_________________
My web site Great White Software
RBLibrary.com REALbasic learning  
                             Top                vfran          Post subject: 
Re: Encoding problem when replacingPosted: Tue Dec 27, 2011 10:47 am            
             
Joined: Mon Dec 07, 2009 7:30 pm
Posts: 70                Thanks
Got it finally:
app.firstname = DefineEncoding(app.firstname, Encodings.windowsANSI)

Any idea why the MSSQL DB stored the characters as ANSI instead of UTF?   
                             Top                 timhare          Post subject: 
Re: Encoding problem when replacingPosted: Tue Dec 27, 2011 1:32 pm             
            
Joined: Fri Jan 06, 2006 3:21 pm
Posts: 10599
Location: Portland, OR  USA                It didn't.  When you transfer data 
to/from the database, meta data such as encoding is lost.  The DB assumes a 
particular encoding.  Your code must do the same (via DefineEncoding) when you 
read it back.  There is nothing about the data that tells you or the database 
what the encoding acutally is.   
                             Top             Display posts from previous: All 
posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost 
timeSubject AscendingDescending          Page 1 of 1
   [ 8 posts ]      
-- 
Over 1500 classes with 29000 functions in one REALbasic plug-in collection. 
The Monkeybread Software Realbasic Plugin v9.3. 
http://www.monkeybreadsoftware.de/realbasic/plugins.shtml

[email protected]

Reply via email to