Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

UTF-8 to ASCII in a string

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
kimmi

Moderator



Joined: 24 Feb 2006
Posts: 1922
Location: Denmark

denmark.gif
PostPosted: Mon Feb 13, 2017 2:45 pm    Post subject: UTF-8 to ASCII in a string Reply with quote

Hi,
For a webradio i sent raw metadata via comport to mcu to display it on Nextion HMI lcd
metadata is in raw UTF-8 so i need to convert UTF-8 to ascii

it works but is there a better / faster way to convert UTF-8 to ascii in a string ?

Code:
'For lcd convert utf-8
Declare Sub Remove_utf8(byval Characters As String)
Dim Index As Byte , Len_characters As Byte , Index_1 As Byte
Dim Tmp_str_1 As String * 1 , Tmp_str_2 As Byte , Tmp_str_3 As Byte , Tmp As Byte
Dim My_string As String * 60

' THOR GÖRANS - FREDAG I KÖPENHAMN  <****>  Chr(195) + Chr(150) = hex C3 96    Ã–
My_string = "THOR G" + Chr(195) + Chr(150) + "RANS - FREDAG I K" + Chr(195) + Chr(150) + "PENHAMN"
Do
   Call Remove_utf8(my_string)
   Print My_string
  Loop

Sub Remove_utf8(byval Characters As String)
   Index = 1
   Len_characters = Len(characters)
   While Index <= Len_characters
      If Mid(characters , Index , 1) >= Chr(194) And Mid(characters , Index , 1) <= Chr(195) Then       ' C3 C4
         Index_1 = Index + 1
         Tmp_str_1 = Mid(characters , Index , 1)
         Tmp_str_2 = Asc(tmp_str_1)
         Shift Tmp_str_2 , Left , 6
         Tmp_str_2 = Tmp_str_2 And &HFF
         Tmp_str_1 = Mid(characters , Index_1 , 1)
         Tmp_str_3 = Asc(tmp_str_1)
         Tmp_str_3 = Tmp_str_3 And &H3F
         Tmp = Tmp_str_2 Or Tmp_str_3
         Tmp_str_1 = Chr(tmp)
         Mid(characters , Index , 1) = Tmp_str_1
         Delchar Characters , Index_1
      End If
      Incr Index
   Wend
 My_string = Characters
End Sub


(BASCOM-AVR version : 2.0.8.0 , Latest : 2.0.7.8 )

_________________
/ Kim
Back to top
View user's profile Visit poster's website MSN Messenger
AdrianJ

Bascom Expert



Joined: 16 Jan 2006
Posts: 2483
Location: Queensland

australia.gif
PostPosted: Tue Feb 14, 2017 2:35 am    Post subject: Reply with quote

Lookup table ?
_________________
Adrian Jansen
Computer language is a framework for creativity
Back to top
View user's profile Visit poster's website
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 6198
Location: Holland

blank.gif
PostPosted: Tue Feb 14, 2017 10:40 pm    Post subject: Reply with quote

if you want the clean ASCII, you only need to check the MS bit. If it is 1, skip, if it is 0, it is plain ASCII and keep it.
then make a simple loop :
Code:
z="" 'clear return string
for j=1 to len(s)
  b=asc(s,J) 'get byte
  if b.7=0 then  'normal ASCII
    z=z+chr(b)  'add to return string
  end if
next
 

now z contains the plain ascii code.

_________________
Mark
Back to top
View user's profile Visit poster's website
kimmi

Moderator



Joined: 24 Feb 2006
Posts: 1922
Location: Denmark

denmark.gif
PostPosted: Wed Feb 15, 2017 11:48 am    Post subject: Reply with quote

Hi,

The "normal ASCII" did not work for me on lcd
Quote:
S : THOR GÖRANS - FREDAG I KÖPENHAMN

Z : THOR GRANS - FREDAG I KPENHAMN


but you'r code was simple, so i change it and now it runs fine for me

Quote:
S : THOR GÖRANS - FREDAG I KÖPENHAMN

Z : THOR GÖRANS - FREDAG I KÖPENHAMN


Code:
Dim S As String * 60
Dim Z As String * 60
Dim J As Byte , B As Byte , A As Byte
' THOR GÖRANS - FREDAG I KÖPENHAMN  <****>  Chr(195) + Chr(150) = hex C3 96    Ã–
S = "THOR G" + Chr(195) + Chr(150) + "RANS - FREDAG I K" + Chr(195) + Chr(150) + "PENHAMN"
Do
   Print S                                                  ' Thor Gã–rans - Fredag I Kã–penhamn

   Z = ""                                                   'clear return string
   For J = 1 To Len(s)
      B = Asc(s , J)                                        'get byte
      If B >= 194 And B <= 195 Then                         'utf-8 ASCII
         J = J + 1                                          'incr j for next char
         A = Asc(s , J)                                     'get next char
         Shift B , Left , 6                                 ' shift it left by 6
        B = B Mod &HFF
        A = A Mod &HFF
         B = B Or A                                         'or  utf-8 with next char
         Z = Z + Chr(b)                                     'add to return string
      Else
         Z = Z + Chr(b)                                     'add to return string
      End If

   Next
    S = Z                                                   'Thor Görans - Fredag I Köpenhamn
Loop

_________________
/ Kim
Back to top
View user's profile Visit poster's website MSN Messenger
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 6198
Location: Holland

blank.gif
PostPosted: Wed Feb 15, 2017 3:35 pm    Post subject: Reply with quote

aha, i see. you want to have the other chars.
well you could do a lookup and replace them. and when not in the table, use a ?

_________________
Mark
Back to top
View user's profile Visit poster's website
kimmi

Moderator



Joined: 24 Feb 2006
Posts: 1922
Location: Denmark

denmark.gif
PostPosted: Wed Feb 15, 2017 5:25 pm    Post subject: Reply with quote

hi,
The Nextion HMI lcd i have added ISO 8859-15 so all chars are working with this code

_________________
/ Kim
Back to top
View user's profile Visit poster's website MSN Messenger
Duval JP

Bascom Member



Joined: 22 Jun 2004
Posts: 1198
Location: France

france.gif
PostPosted: Mon Apr 17, 2017 5:34 pm    Post subject: Reply with quote

I see the Nextion project,

Before trying

It seems to me it is a "Visual basic like" to do very fast interface, maybe you have a video about it... working with Bascom ?
How it works, serial instruction ?

many thanks for your help
JP

Rolling Eyes Question
Back to top
View user's profile Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
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
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum