J'ai été très étonné de voir sur wikipédia qu'il y avait autant de variante de l'ascii qu'il y avait de machine et de système
IBM a son ascii, linux a son ascii et même le sinclair zx spectrum ou le comodore ou atari ont leur propre ascii !
Microsoft a créé 21 variantes de l'ascii sous forme de page de code (page850 pour l'europe occidentale, page437 pour les usa)
De plus Microsoft utilise l'ascii-page-de-code pour DOS et les fenêtres consoles mais les fenêtres standards (non consoles) utilisent d'autre jeux de caractères comme l'ansi...
Plus UNICODE qui contient ces 3 asciis.
Cependant, si on cherche à afficher les caractères et les lettres accentuées les plus utilisées comme le "é" alors presque toutes les polices unicodes fonctionneront avec un simple "chr".
Code : Tout sélectionner
; Original code from Ollivier http://www.purebasic.fr/french/posting.php?mode=reply&f=6&t=16283
; Modified by Mesa.
;https://fr.wikipedia.org/wiki/American_Standard_Code_for_Information_Interchange
;http://www.asciitable.com/ extended ascii
;https://fr.wikipedia.org/wiki/Page_de_code_850
;https://fr.wikipedia.org/wiki/Page_de_code_437
;IBM variant of the page850:http://www-01.ibm.com/software/globalization/cp/cp00850.html
;EnableExplicit
DataSection
;US-ASCII Extended
Ascii_EXT:
Data.u $00c7, $00fc, $00e9, $00e2, $00e4, $00e0, $00e5, $00e7, $00ea, $00eb, $00e8, $00ef, $00ee, $00ec, $00c4, $00c5
Data.u $00c9, $00e6, $00c6, $00f4, $00f6, $00f2, $00fb, $00f9, $00ff, $00d6, $00dc, $00a2, $00a3, $00a5, $20a7, $0192
Data.u $00e1, $00ed, $00f3, $00fa, $00f1, $00d1, $00aa, $00ba, $00bf, $2310, $00ac, $00bd, $00bc, $00a1, $00ab, $00bb
Data.u $2591, $2592, $2593, $2502, $2524, $2561, $2562, $2556, $2555, $2563, $2551, $2557, $255d, $255c, $255b, $2510
Data.u $2514, $2534, $252c, $251c, $2500, $253c, $255e, $255f, $255a, $2554, $2569, $2566, $2560, $2550, $256c, $2567
Data.u $2568, $2564, $2565, $2559, $2558, $2552, $2553, $256b, $256a, $2518, $250c, $2588, $2584, $258c, $2590, $2580
Data.u $03b1, $00df, $0393, $03c0, $03a3, $03c3, $00b5, $03c4, $03a6, $0398, $03a9, $03b4, $221e, $03c6, $03b5, $2229
Data.u $2261, $00b1, $2265, $2264, $2320, $2321, $00f7, $2248, $00b0, $2219, $00b7, $221a, $207f, $00b2, $25a0, $00a0
;MICROSOFT ASCII WESTERN EUROPE Extended
Page850:
Data.u $00c7, $00fc, $00e9, $00e2, $00e4, $00e0, $00e5, $00e7, $00ea, $00eb, $00e8, $00ef, $00ee, $00ec, $00c4, $00c5
Data.u $00c9, $00e6, $00c6, $00f4, $00f6, $00f2, $00fb, $00f9, $00ff, $00d6, $00dc, $00f8, $00a3, $00d8, $00d7, $0192
Data.u $00e1, $00ed, $00f3, $00fa, $00f1, $00d1, $00aa, $00ba, $00bf, $00ae, $00ac, $00bd, $00bc, $00a1, $00ab, $00bb
Data.u $2591, $2592, $2593, $2502, $2524, $00c1, $00c2, $00c0, $00a9, $2563, $2551, $2557, $255d, $00a2, $00a5, $2510
Data.u $2514, $2534, $252c, $251c, $2500, $253c, $00e3, $00c3, $255a, $2554, $2569, $2566, $2560, $2550, $256c, $00a4
Data.u $00f0, $00d0, $00ca, $00cb, $00c8, $0131, $00cd, $00ce, $00cf, $2518, $250c, $2588, $2584, $00a6, $00cc, $2580
Data.u $00d3, $00df, $00d4, $00d2, $00f5, $00d5, $00b5, $00fe, $00de, $00da, $00db, $00d9, $00fd, $00dd, $00af, $00b4
Data.u $00ad, $00b1, $2017, $00be, $00b6, $00a7, $00f7, $00b8, $00b0, $00a8, $00b7, $00b9, $00b3, $00b2, $25a0, $00a0
;Graphic mode n°00..31
Data.u $0020, $263a, $263b, $2665, $2666, $2663, $2660, $2022, $25d8, $25cb, $25d9, $2642, $2640, $266a, $266b, $263c
Data.u $25ba, $25c4, $2195, $203c, $00b6, $00a7, $25ac, $21a8, $2191, $2193, $2192, $2190, $221f, $2194, $25b2, $25bc
;Caracter n°127
Data.u $2302
;;MICROSOFT ASCII USA Extended (IBM)
;Page437:
; TODO
; TODO: VG5000, Sinclair ZX spectrum, comodore, atari, msx, as400 (EBCDIC), Apple, Unix, Linux, etc...
EndDataSection
Procedure.S Ascii2Unicode(*Ascii, Length = 0, Page.u=0, GraphicMode.b=#False)
;Page: 0 => ascii, 850 => PageDOS 850, 437 => PageDos 437, etc
;GraphicMode = Caracters 0 to 31 and 127 are not control caraters but printable caracters. Only with page 850, 437 ...
Protected.l i
Protected.i *Unicode
Protected.u Code
Protected.s StringUnicode
Dim Ascii_EXT.u(128+32+1)
If Length =0
MessageRequester("Attention", "Length can't be 0",#PB_MessageRequester_Warning)
End
EndIf
Select Page
Case 0 ; ========================= US-ASCII
Restore Ascii_EXT
For i=0 To 127
Read.u Ascii_EXT(i)
Next i
*Unicode = AllocateMemory(Length * 2)
If *Unicode = 0
MessageRequester("Attention", "Memory problem!", #PB_MessageRequester_Warning)
End
EndIf
For I = 0 To Length -1
Code = PeekA(*Ascii + I)
If Code > 127
Code = Ascii_EXT(Code-128)
EndIf
PokeU(*Unicode + (2 * (I - 1) ), Code)
Next
StringUnicode.s = PeekS(*Unicode, Length)
Case 850 ; ========================= Microsoft Page 850 Western Europe
Restore Page850
For i=0 To 160
Read.u Ascii_EXT(i)
Next i
*Unicode = AllocateMemory(Length * 2)
If *Unicode = 0
MessageRequester("Attention", "Memory problem!", #PB_MessageRequester_Warning)
End
EndIf
For I = 0 To Length -1
Code = PeekA(*Ascii + I)
If Code > 127
Code = Ascii_EXT(Code-128)
EndIf
If GraphicMode > 0 And code = 127 ; (Beware of Short-circuit evaluations with "if" ?)
Code = Ascii_EXT(160)
EndIf
If GraphicMode > 0 And code < 32
Code = Ascii_EXT(Code+128)
EndIf
PokeU(*Unicode + (2 * (I - 1) ), Code)
Next
StringUnicode.s = PeekS(*Unicode, Length)
;Case 437 ; ========================= Page 437 USA
; TODO
Default
MessageRequester("Attention", "Page unknown",#PB_MessageRequester_Warning)
End
EndSelect
ProcedureReturn StringUnicode
EndProcedure
; For example, fullfills the 256 caracters in a buffer
*x = AllocateMemory(256)
For I = 0 To 255
PokeA(*x + i, i)
Next
; Decoding...
; A$ = Ascii2Unicode(*x, 256); US-ASCII
; A$ = Ascii2Unicode(*x, 256, 850); Page 850 TEXTE
A$ = Ascii2Unicode(*x, 256, 850, #True); Page 850 GRAPHIQUE ; <=== Bug on the caractere 0 ! ! !
; The trick is there, use lucida font (Should be present on every platform, and very important for Windows XP!)
LoadFont(0, "lucida", 10)
OpenWindow(0, 0, 0, 880, 550, "Ascii to Unicode in a system's window...", #PB_Window_SystemMenu )
ListIconGadget(0, 0, 0, 880, 550, "0..31", 110, #PB_ListIcon_GridLines)
SetGadgetFont(0, FontID(0))
AddGadgetColumn(0, 1, "32..63", 110)
AddGadgetColumn(0, 2, "64..95", 110)
AddGadgetColumn(0, 3, "96..127", 110)
AddGadgetColumn(0, 4, "128..159", 110)
AddGadgetColumn(0, 5, "160..191", 110)
AddGadgetColumn(0, 6, "192..223", 110)
AddGadgetColumn(0, 7, "224..255", 110)
;Print...
For i=0 To 31
AddGadgetItem(0, -1, Mid(A$,i,1) + Chr(10) + Mid(A$,i +32,1) + Chr(10) +
Mid(A$,i + 64,1) + Chr(10) + Mid(A$,i +96,1) + Chr(10) +
Mid(A$,i + 128,1) + Chr(10) + Mid(A$,i +160,1) + Chr(10) +
Mid(A$,i + 192,1) + Chr(10) + Mid(A$,i +224,1))
Next i
OpenWindow(1, WindowX(0)+WindowWidth(0,#PB_Window_FrameCoordinate), 0, 300, 550, "... it's not a console !", #PB_Window_SystemMenu)
CanvasGadget(2, 0, 0, 300, 550)
StartDrawing(CanvasOutput(2) )
DrawingFont(FontID(0) )
A=0
For Y = 0 To 15
For X = 0 To 15
DrawText(10+X * 16, Y * 32, Mid(A$, A , 1), RGB(0, 0, 0), RGB(255, 255, 255) )
a=a+1
Next
Next
StopDrawing()
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
Mesa.