Page 1 sur 1

[Func] _OEMToAnsi / _AnsiToOEM convertir texte ANSI<=>OEM

Posté : mer. 18 nov. 2009 21:49
par Tlem
Voici deux fonctions qui vous permettrons de convertir le retour de vos commande DOS (qui sont au format OEM) en texte lisible par Windows (format ANSI) et inversement

Code : Tout sélectionner

Func _OEMToAnsi($sOEM)
    Local $a_AnsiFName = DllCall('user32.dll', 'Int', 'OemToChar', 'str', $sOEM, 'str', '')
    If @error = 0 Then $sAnsi = $a_AnsiFName[2]
    Return $sAnsi
EndFunc   ;==>_OEMToAnsi

Func _AnsiToOEM($sAnsi)
    Local $a_OEMFName = DllCall('user32.dll', 'Int', 'CharToOem', 'str', $sAnsi, 'str', '')
    If @error = 0  Then Return $a_OEMFName[2]
EndFunc   ;==>_AnsiToOEM
Voici un petit exemple de ce que cela peut donner pour _OEMToAnsi() :

Code : Tout sélectionner

Dim $Foo, $line
$Foo = Run(@ComSpec & ' /c DIR', @ScriptDir, @SW_HIDE, 2) ; 2 = $STDOUT_CHILD
; Lecture du STDOut
While 1
    $line &= StdoutRead($Foo)
    If @error Then ExitLoop
WEnd

MsgBox(0, 'Version OEM', $line)
MsgBox(0, 'Version ANSI', _OEMToAnsi($line))

Func _OEMToAnsi($sOEM)
    Local $a_AnsiFName = DllCall('user32.dll', 'Int', 'OemToChar', 'str', $sOEM, 'str', '')
    If @error = 0 Then $sAnsi = $a_AnsiFName[2]
    Return $sAnsi
EndFunc   ;==>_OEMToAnsi

Re: [Func] _OEMToAnsi => Convertir du texte DOS en Texte Windows

Posté : ven. 04 déc. 2009 10:59
par tofu
Bonjour,

Si je peux me permettre, il y a peu j'ai été confronté à un problème de conversion de ce type. Il est apparu après quelques recherches deux solutions. Celle que vous présentez ici, et une autre qui consiste à utiliser l'objet "OlePrn.OleCvt" de Windows.

Quelques infos sur les propriétés de cette interface et notamment sur les deux fonctions que sont ToUnicode() et ToUtf8().

Pour montrer un cas concret, je vais reprendre votre exemple de conversion OEM/ANSI:

Code : Tout sélectionner

Dim $Foo, $line
$Foo = Run(@ComSpec & ' /c DIR', @ScriptDir, @SW_HIDE, 2) ; 2 = $STDOUT_CHILD
; Lecture du STDOut
While 1
    $line &= StdoutRead($Foo)
    If @error Then ExitLoop
WEnd

MsgBox(0, 'Version OEM', $line)
MsgBox(0, 'Version ANSI', _ChangePageCode($line,1))

Func _ChangePageCode($sString, $iPageCode)
    Local $objConv = ObjCreate("OlePrn.OleCvt")
    return $objConv.ToUnicode($sString,$iPageCode)
EndFunc
 
L'intérêt de cette méthode réside dans la possibilité de spécifier dynamiquement le page code (desfois que quelqu'un veuille convertir sa chaîne en caractères japonais ? :mrgreen: )

Voilà, ce n'est pas grand chose, mais ça peut rendre service au besoin :wink:

Cordialement,

Re: [Func] _OEMToAnsi => Convertir du texte DOS en Texte Windows

Posté : ven. 04 déc. 2009 12:22
par ani
C'est une solution, mais cette objet sert essentiellement pour l'impression (OlePrn.OleCvt= Print convertion ?), en prime elle n'est pas présente sur tout les windows, le minimum est le 2000 :(

Merci pour cette manipulation.

Il existe encore d'autre solution. ^^'

Re: [Func] _OEMToAnsi => Convertir du texte DOS en Texte Windows

Posté : ven. 04 déc. 2009 15:14
par funkey
Thank you Tlem for posting this function. It is very usefull!!
One problem is, that the string must not be to long.

Code : Tout sélectionner

Func _GetAllFolders($sPath = @HomeDrive)
    ;funkey 04.12.2009
    Local $line, $aFolder
    Local $cmd = 'dir /ad /b /s "' & $sPath & '\"'
    Local $Pid = Run(@ComSpec & " /c " & $cmd, "", @SW_HIDE, 2)
    While 1
        $line &= StdoutRead($Pid, 0, 0)
        If @error Then ExitLoop
    WEnd
    $line = _OEMToAnsi($line)
    $aFolder = StringSplit($line, @CRLF, 3)
    ReDim $aFolder[UBound($aFolder) - 1]
    Return $aFolder
EndFunc   ;==>_GetAllFolders
 
The resulting string is often to long, so I have to do it this way:

Code : Tout sélectionner

Func _GetAllFolders($sPath = @HomeDrive)
    ;funkey 04.12.2009
    Local $line, $aFolder
    Local $cmd = 'dir /ad /b /s "' & $sPath & '\"'
    Local $Pid = Run(@ComSpec & " /c " & $cmd, "", @SW_HIDE, 2)
    While 1
        $line &= StdoutRead($Pid, 0, 0)
        If @error Then ExitLoop
    WEnd
    $aFolder = StringSplit($line, @CRLF, 3)
    ReDim $aFolder[UBound($aFolder) - 1]
    For $i = 0 To UBound($aFolder) - 1
        $aFolder[$i] = _OEMToAnsi($aFolder[$i])
    Next
    Return $aFolder
EndFunc   ;==>_GetAllFolders
 

Re: [Func] _OEMToAnsi / _AnsiToOEM convertir texte ANSI<=>OEM

Posté : sam. 06 mars 2010 19:03
par Tlem
Petite mise à jour pour rajouter la conversion ANSI => OEM. ;)
Cela peux servir pour ceux qui veulent créer une application console.