Code Page - the way you want it!

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Code Page - the way you want it!

Post by MrSwiss »

Hi all,

we've had this problem before (at least once, with my ASCII-GR-Table.bas), because on non
USA code-page (437), it required at least a script to run successfully.

For reasons unknown, Redmond (aka: M$) decided, to change the console settings in WIN10.
(with the WIN10's, 1st annual upgrade, used to be: 437, is now: 850 [us-en/swiss settings])
After some research (FB-Doc mainly), I've got the solution: its Open Pipe
It lets you *get* the currently set CP, change it (for your prog's runtime) and thereafter, re-
store it to *original* before returning to the OS.

Below code is actually, as can be seen by the differing Dates, a mix of 2 prog's, to give a
complete example. See remarks/comments in the code, for more details:

Code: Select all

' Change_curr_CP.bas -- 2017-03-02, MrSwiss -- compile with: -s console

' updated: 2017-04-07 -- inserted: ASCII_Print.bas and added conditional
' compiling (debug defined/undefined)

#Define debug   ' comment out: if you don't want *CP change* output

Dim As UInteger start_pos
Dim As Long     fno = FreeFile
Dim As String   orig_CP

Open Pipe "chcp" For Input As #fno
Line Input #fno, orig_CP    ' only one line to get (original code-page)
Close #fno

start_pos = InStrRev(orig_CP, " ") + 1  ' get the start_pos (in string)
orig_CP = Trim(Mid(orig_CP, start_pos))    ' save original code-page
#Ifdef debug
    Print "saved CP = "; orig_CP    ' uncomment #Define debug, if output is wanted/needed
#EndIf  ' debug

' set the CP we want to use
#Ifndef debug
    Open Pipe "chcp 437 > nul" For Output As #fno  ' no screen output
#Else
    Open Pipe "chcp 437" For Output As #fno ' with screen output
#EndIf  ' debug
Close #fno


' ### your code here: runs under CP 437 now
Scope   ' for safety's sake (in case of duplicated variables etc.)

' ASCII_Print.bas -- 2017-01-07, MrSwiss
Const wid = 800, hei = 600, c_d = 32, mv = 48, sl = 98, vnm = 12, x = 8

#Define RangeUL(l, h) ( CULng( Rnd() * (h - l) + l) ) ' UL = unsigned 32bit INT

ScreenRes(wid, hei, c_d) ' define fbGFX Window
Randomize(Timer, 3) ' set seed: for Rnd()

For i As UInteger = 0 To 255 ' the whole ASCII table (CP = 437 = USA)
	' on every run: pre-calculate vertical pos. y (x is a constant)
	Var y = ((i Mod mv) + 1) * vnm
	' Macro RangeUL() gets a random color, here: the 'bright' 50% _
	' since: medium-grey = min. / white = max.
	Draw String(x, y), String(sl, Chr(i)), RangeUL(&hFF7F7F7F, &hFFFFFFFF)
	' after filling the screen, wait for a key press, before clearing/filling screen again
	If (i Mod mv) + 1 = mv Then Sleep : Cls
Next
' END ASCII_Print.bas (final 'Sleep' removed)

End Scope   ' back to original variables ...
' ### your code end
 
 
' reset the CP to original (the saved cp, at the top of prog.)
#Ifndef debug
    Open Pipe "chcp " + orig_CP + " > nul" For Output As #fno
#Else
    Open Pipe "chcp " + orig_CP For Output As #fno
#EndIf  ' debug
Close #fno

Sleep
Post Reply