No announcement yet.

Trouble formatting 'foreign text' output in chars matching console O/P code page

  • Filter
  • Time
  • Show
Clear All
new posts

  • Trouble formatting 'foreign text' output in chars matching console O/P code page

    The following test program demonstrates what I am trying to achieve. The attached (jpg) screenshot shows the output of the command line: TestCP502.exe P862 when run under Windows XP (SP3).

    I expected at least one of the five string-literal output lines (near the end of PBMain) to match the line directly above them (output line 2), but none of them do. Does this mean that you can only output 'foreign text' written in non-Latin characters by doing something like:
    print chr$(80,111,119,101,114,66,97,115,105,99) ' print "PowerBasic"

    Why doesn't the conversion using the ucode$() function behave as expected? One of those specifying the use of code page 862 should have worked. Am I doing something wrong?

    ' TestCP502.bas
    ' -------------
    ' Compiler: PBCC 5.02
    #DIM ALL
      %False = 0 : %True = not %False
    ' The following two function declares extracted from
    ' Last updated 27 August 2009 as shipped with PBCC 5.02
               ALIAS "SetConsoleOutputCP" (BYVAL dwCodePageID AS DWORD) AS LONG
               ALIAS "GetConsoleOutputCP" () AS LONG
    function PBMain&
    '        -------
        local Tail$, CP$, TailLength&, TailPos&, CodePage&, RtnVal&
        ' First set console output code page as determined by command line argument
        ' or default code page 437 OEM U.S.
        Tail$       = ucase$(rtrim$(command$))
        TailLength& = len(Tail$)
        TailPos&    = instr(Tail$, "P")
        if  TailPos& > 0 _
        and TailPos& < TailLength& then
           CP$ = extract$(TailPos& + 1, Tail$, $SPC)
           CodePage& = val(CP$)
        end if
        if CodePage& = 0 then CodePage& = 437  ' Default CodePage is IBM 437 OEM U.S.
        print "Setting code page to" CodePage& "... ";
        RtnVal& = FnSetOPCP& (CodePage&)
        if RtnVal& = 0 then
           print "Failed. Code page is" GetConsoleOutputCP
           print "Success. Code page is" RtnVal&
        end if
        ' -------  Experimental Hebrew output -------
        ' Part 1 using chr$(n) ...
        print chr$(128 to 154)  ' Hebrew alphabet
        ' Part 2 using string literals (string pasted from BabelPad)...
        print "àáâãäåæçèéêëìíîïðñòóôõö÷øùú"
        print ucode$("àáâãäåæçèéêëìíîïðñòóôõö÷øùú")
        print ucode$("àáâãäåæçèéêëìíîïðñòóôõö÷øùú", 862)
        print acode$(ucode$("àáâãäåæçèéêëìíîïðñòóôõö÷øùú"))
        print acode$(ucode$("àáâãäåæçèéêëìíîïðñòóôõö÷øùú", 862))
        print "Press any key to end ..."
    end function  ' PBMain& ---------------------------------------------
    ' FnSetOPCP&
    ' ----------
    '   Sets console output code page using API function.
    '   An alternative method is:
    '   shell " con: codepage select=" + ltrim$(str$(CodePage))
    '   Code page is specified in parameter CodePage.
    '   Return value is 0 (failure) or code page number as returned by the API
    '   function GetConsoleOutputCP.
    '   MSDN (
    '   says: Note  ANSI code pages can be different on different computers, or
    '   can be changed for a single computer, leading to data corruption. For the
    '   most consistent results, applications should use Unicode, such as UTF-8
    '   or UTF-16, instead of a specific code page.
    '   437   = IBM437       = OEM United States
    '   20127 = us-ascii     = US-ASCII (7-bit)
    '   20269 = x-cp20269    = ISO 6937 Non-Spacing Accent
    '   65000 = utf-7        = Unicode (UTF-7)
    '   65001 = utf-8        = Unicode (UTF-8)
    '   1200  = utf-16       = Unicode UTF-16, little endian byte order (BMP of
    '                           ISO 10646); available only to managed applications
    '   1201  = unicodeFFFE  = Unicode UTF-16, big endian byte order; available
    '                           only to managed applications
    '   862   = DOS-862      = OEM Hebrew; Hebrew (DOS)
    '   1255  = windows-1255 = ANSI Hebrew; Hebrew (Windows)
    '   10005 = x-mac-hebrew = Hebrew (Mac)
    '   20424 = IBM424       = IBM EBCDIC Hebrew
    '   28598 = iso-8859-8   = ISO 8859-8 Hebrew; Hebrew (ISO-Visual)
    '   38598 = iso-8859-8-i = ISO 8859-8 Hebrew; Hebrew (ISO-Logical)
    '   Unicode subsets: Hebrew = 0590 - 05FF, Box Drawing: 2500 - 257F
    function FnSetOPCP& (CodePage as dword)
    '        ----------
        local RtnVal&
        RtnVal& = SetConsoleOutputCP(CodePage)
        sleep 400 ' Give Windows time to react
        if isfalse RtnVal& then
           function = 0  ' failure
           RtnVal& = GetConsoleOutputCP
           function = RtnVal&
        end if
    end function  ' FnSetOPCP&  -----------------------------------------
    Attached Files
    Last edited by Mottel Gutnick; 1 Nov 2009, 05:48 AM. Reason: specified OS version

  • #2
    Just for the heck of it, what happens if you use STDOUT instead of PRINT?

    Or, maybe you need to ....
    FUNCTION WinMain....
          Now do all your own console manipulation
          I doubt the PRINT statement will work under these conditions 
          but there are Windows' Consolexxx()  functions  you can call 
         to send output to the console.
    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]


    • #3
      Originally posted by Michael Mattias View Post
      Just for the heck of it, what happens if you use STDOUT instead of PRINT?
      Well asked! I modified the program to duplicate all of the PRINT statements shown above with similar STDOUT statements. The differences (which were not entirely unexpected from previous experience tinkering with this stuff), are fully documented and illustrated here. (The upload restrictions in this forum made it difficult to demonstrate adequately within this thread.)

      None of it helps me with my particular problem, but I guess it is useful to document the differences in that they contribute something to the pool of knowledge about how these commands and Windows interact.


      • #4
        Maybe this whole project would be a lot easier if you forgot about trying to use the console and went to a "console-like" GUI object?

        Here's an example of using a listview control to simulate a console....

        Use Listview control as a console. September 13 2003.

        ...and here's an example of showing Unicode characters on GUI screens...
        Directory List with Non-ASCII (Unicode) characters in file names 5-31-08

        I have not worked with console applications a whole lot, but what I have learned is the console is its own animal.

        Or, maybe Perfect Sync's Console Tools product can help you?

        Michael Mattias
        Tal Systems (retired)
        Port Washington WI USA
        [email protected]