Text to Ascii or HTML Code Converter

Example scripts and tips (replaces Old Scripts & Tips archive)

Moderators: Dorian (MJT support), JRL, Phil Pendlebury

Post Reply
User avatar
Rain
Automation Wizard
Posts: 550
Joined: Tue Aug 09, 2005 5:02 pm
Contact:

Text to Ascii or HTML Code Converter

Post by Rain » Fri May 31, 2013 12:18 pm

Convert text to Ascii or HTML Codes. Paste the text to convert, check "Convert to HTML" to convert the text to HTML codes or leave unchecked to convert to Ascii and click convert.

For Example:
Hello World! to Ascii codes Returns

Code: Select all

72 101 108 108 111 32 87 111 114 108 100 33
Hello World! to HTML codes Returns

Code: Select all

&#72:&#101:&#108:&#108:&#111:&#32:&#87:&#111:&#114:&#108:&#100:&#33:
I had to replace the semicolons with colons to display the example HTML codes.

Code: Select all

Dialog>Dialog1
object Dialog1: TForm
  Left = 247
  Top = 96
  HelpContext = 5000
  BorderIcons = [biSystemMenu]
  Caption = 'Convert Text to Ascii or HTML Code'
  ClientHeight = 340
  ClientWidth = 287
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = True
  ShowHint = True
  OnTaskBar = False
  PixelsPerInch = 96
  TextHeight = 13
  object Label1: TLabel
    Left = 8
    Top = 8
    Width = 72
    Height = 13
    Caption = 'Text to convert'
  end
  object Label2: TLabel
    Left = 8
    Top = 128
    Width = 32
    Height = 13
    Caption = 'Output'
  end
  object Label3: TLabel
    Left = 8
    Top = 108
    Width = 60
    Height = 13
    Caption = '0 Characters'
  end
  object Label4: TLabel
    Left = 8
    Top = 296
    Width = 3
    Height = 13
  end
  object MSMemo1: tMSMemo
    Left = 8
    Top = 24
    Width = 273
    Height = 81
    ScrollBars = ssVertical
    TabOrder = 0
  end
  object MSMemo2: tMSMemo
    Left = 8
    Top = 144
    Width = 273
    Height = 121
    ScrollBars = ssVertical
    TabOrder = 1
  end
  object MSButton1: tMSButton
    Left = 128
    Top = 312
    Width = 75
    Height = 25
    Caption = 'Convert'
    TabOrder = 2
    DoBrowse = False
    BrowseStyle = fbOpen
  end
  object MSButton2: tMSButton
    Left = 208
    Top = 312
    Width = 75
    Height = 25
    Caption = 'Copy'
    TabOrder = 3
    DoBrowse = False
    BrowseStyle = fbOpen
  end
  object CheckBox1: TCheckBox
    Left = 8
    Top = 316
    Width = 105
    Height = 17
    Caption = 'Convert to HTML'
    TabOrder = 4
  end
  object ProgressBar1: TProgressBar
    Left = 8
    Top = 272
    Width = 273
    Height = 17
    TabOrder = 5
  end
  object MSButton3: tMSButton
    Left = 208
    Top = 112
    Width = 75
    Height = 25
    Caption = 'Paste'
    TabOrder = 6
    DoBrowse = False
    BrowseStyle = fbOpen
  end
  object MSButton4: tMSButton
    Left = 128
    Top = 312
    Width = 75
    Height = 25
    Caption = 'Stop'
    TabOrder = 15
    Visible = False
    DoBrowse = False
    BrowseStyle = fbOpen
  end
end
EndDialog>Dialog1


AddDialogHandler>Dialog1,MSMemo1,OnChange,GetLength
AddDialogHandler>Dialog1,msButton1,OnClick,Convert
AddDialogHandler>Dialog1,MSButton2,OnClick,Copy
AddDialogHandler>Dialog1,MSButton3,OnClick,Paste
AddDialogHandler>Dialog1,MSButton4,OnClick,Stop
AddDialogHandler>Dialog1,,OnClose,Exit
Let>StopFlag=0
Show>Dialog1
Label>Loop
Wait>1
Goto>Loop



SRT>Convert
  SetDialogProperty>Dialog1,msButton1,Visible,False
  SetDialogProperty>Dialog1,msButton1,Enabled,False
  SetDialogProperty>Dialog1,msButton2,Enabled,False
  SetDialogProperty>Dialog1,msButton3,Enabled,False
  SetDialogProperty>Dialog1,msButton4,Visible,True
  SetDialogProperty>Dialog1,CheckBox1,Enabled,False
  SetDialogProperty>Dialog1,MSMemo2,Text,
  Let>TextToConvert=WhED55oIO
  GetDialogProperty>Dialog1,MSMemo1,Text,TextToConvert
  Length>%TextToConvert%,InputLeng
  SetDialogProperty>Dialog1,ProgressBar1,Max,%InputLeng%
  SetDialogProperty>Dialog1,ProgressBar1,Position,0
  IF>InputLeng>0

   Let>CharCounter=0
   Let>OutputMsg=
   Repeat>CharCounter
    Add>CharCounter,1
    SetDialogProperty>Dialog1,ProgressBar1,Position,%CharCounter%
    MidStr>%TextToConvert%,%CharCounter%,1,Char
    StringReplace>%Char%,CR,CarriageReturn,crRes
    StringReplace>%Char%,%LF%,LineFeed,lfRes

    IF>{(%crRes%="CarriageReturn")OR(%lfRes%="LineFeed")}
        GetDialogProperty>Dialog1,CheckBox1,Checked,HTMLChecked
        IF>HTMLChecked=True
          IF>%crRes%=CarriageReturn
            Let>OutputMsg=%OutputMsg%<br>
          ENDIF
        ELSE
        Let>AsciiValue=13
        Let>OutputMsg=%OutputMsg%%SPACE%%AsciiValue%
        ENDIF
    ELSE
     GetDialogProperty>Dialog1,CheckBox1,Checked,HTMLChecked
     IF>HTMLChecked=True
       IF>Char="
        Let>AsciiValue=34
        Let>OutputMsg=%OutputMsg%&#%AsciiValue%;
       ELSE
        Let>TheChar="%Char%"
        VBEval>Asc(%TheChar%),AsciiValue
        Let>OutputMsg=%OutputMsg%&#%AsciiValue%;
       ENDIF

     ELSE
       IF>Char="
        Let>AsciiValue=34
        Let>OutputMsg=%OutputMsg%%SPACE%%AsciiValue%
       ELSE
        Let>TheChar="%Char%"
        VBEval>Asc(%TheChar%),AsciiValue
        Let>OutputMsg=%OutputMsg%%SPACE%%AsciiValue%
       ENDIF

     ENDIF
  
    ENDIF
     Let>CharRemaining=%InputLengA%-%CharCounter%
     SetDialogProperty>Dialog1,Label4,Caption,%CharRemaining% Characters remaining
     IF>StopFlag=1
       Let>CharCounter=%InputLeng%
     ENDIF
   Until>CharCounter,%InputLeng%

  SetDialogProperty>Dialog1,Label4,Caption,
  SetDialogProperty>Dialog1,MSMemo2,Text,%OutputMsg%

  ELSE

  SetDialogProperty>Dialog1,MSMemo2,Text,

  ENDIF
  
  Let>StopFlag=0
  SetDialogProperty>Dialog1,Label4,Caption,
  SetDialogProperty>Dialog1,msButton1,Visible,True
  SetDialogProperty>Dialog1,msButton1,Enabled,True
  SetDialogProperty>Dialog1,msButton2,Enabled,True
  SetDialogProperty>Dialog1,msButton3,Enabled,True
  SetDialogProperty>Dialog1,CheckBox1,Enabled,True
  SetDialogProperty>Dialog1,msButton4,Visible,False
END>Convert


SRT>Copy
  GetDialogProperty>Dialog1,MSMemo2,Text,OutputCode
  PutClipBoard>%OutputCode%
END>Copy


SRT>Paste
  GetClipBoard>TheClipBoard
  SetDialogProperty>Dialog1,MSMemo1,Text,%TheClipBoard%
END>Paste


SRT>GetLength
  GetDialogProperty>Dialog1,MSMemo1,Text,TextToConvert
  Length>%TextToConvert%,InputLengA
  SetDialogProperty>Dialog1,Label3,Caption,%InputLengA% Characters
END>GetLength


SRT>Stop
  Let>StopFlag=1
END>Stop


SRT>Exit
  Exit>1
END>Exit

armsys
Automation Wizard
Posts: 1108
Joined: Wed Dec 04, 2002 10:28 am
Location: Hong Kong

Post by armsys » Fri May 31, 2013 12:56 pm

Rain,
Thanks for sharing your script. The conversion script works perfectly in English, not in Chinese.
Image

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1347
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Post by Dorian (MJT support) » Fri May 31, 2013 11:13 pm

That's very clever, Rain, you never cease to amaze me. :)

That saves me always having to look these up, so I have saved it to my macro list.
Yes, we have a Custom Scripting Service. Message me or go here

fightcancer
Macro Veteran
Posts: 239
Joined: Fri Apr 15, 2005 8:32 am

Re: Text to Ascii or HTML Code Converter

Post by fightcancer » Thu Dec 02, 2021 7:48 pm

Thanks for this!

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts