OVH Cloud OVH Cloud

Conversion ANSI UNICODE

1 réponse
Avatar
Jean Saint Jalmes
Bonjour,

Fausse manoeuvre sur le mail pr=E9c=E9dent, je recommence.

J'ai un tableau de Byte qui contient une cha=EEne ANSI (6=20
octets) suivie d'un Single (4 octets), d'un Integer (2=20
octets) et =E0 nouveau d'une cha=EEne ANSI (20 octets).

Existe t'il une API Windows, une instruction ou une=20
fonction VB qui permette de convertir les cha=EEnes ANSI du=20
tableau de Byte en string de longueur variable ?

Actuellement j'utilise la fonction ci-dessous mais je=20
pense qu'il existe une m=E9thode plus =E9l=E9gante et plus=20
rapide.

Dim Buffer() as Byte
Dim Ptr as Long
Dim Text1 as String
Dim Text2 as String

' Apr=E8s chargement du buffer

...
Ptr =3D 0
Get_String Text1,6
Ptr =3D Ptr + 6
Get_String Text2,20
...

Private Sub Get_String(strUNICODE as String, byval Length=20
as Long)
=20
Dim Cptr as Long

strUNICODE =3D space(Length)
For Cptr =3D 1 to Length
mid(strUNICODE,Cptr,1) =3D chr(Buffer(Ptr))
Ptr =3D Ptr + 1
Next Cptr

End Sub

Merci d'avance.


Jean

1 réponse

Avatar
ng
Salut,

Private Sub Get_String(strUNICODE as String, byval Length
as Long)

Dim Cptr as Long

strUNICODE = space(Length)
For Cptr = 1 to Length
mid(strUNICODE,Cptr,1) = chr(Buffer(Ptr))
Ptr = Ptr + 1
Next Cptr

End Sub



Je te conseille plutot d'utiliser les fonctions Space$() (ou String$()),
Chr$(), Mid$ à la pace de Space(), Chr(), Mid(). Car ces dernières
travaillent en variant ce qui est plus lent.

Private Sub Get_String(byref strUNICODE as String, byval Length as Long)

Dim Cptr as Long, Ptr as Long

strUNICODE = String$(Length," ") '//Plus rapide
For Cptr = 1 to Length
'//Je ne savais pas qu'on pouvais faire ça ! C'est étonnant, j'ai
testé ca marche !
'//On en apprend tous les jours ;)
Mid$(strUNICODE,Cptr,1) = Chr$(Buffer(Ptr)) '//Ptr n'est pas déclaré
et à quoi correspond Buffer() ?
Ptr = Ptr + 1
Next
End Sub

--
Nicolas G.
FAQ VB : http://faq.vb.free.fr
API Guide : http://www.allapi.net
Google Groups : http://groups.google.fr/
MZ-Tools : http://www.mztools.com/
http://apisvb.europe.webmatrixhosting.net/

Jean Saint Jalmes a écrit :

Bonjour,

Fausse manoeuvre sur le mail précédent, je recommence.

J'ai un tableau de Byte qui contient une chaîne ANSI (6
octets) suivie d'un Single (4 octets), d'un Integer (2
octets) et à nouveau d'une chaîne ANSI (20 octets).

Existe t'il une API Windows, une instruction ou une
fonction VB qui permette de convertir les chaînes ANSI du
tableau de Byte en string de longueur variable ?

Actuellement j'utilise la fonction ci-dessous mais je
pense qu'il existe une méthode plus élégante et plus
rapide.

Dim Buffer() as Byte
Dim Ptr as Long
Dim Text1 as String
Dim Text2 as String

' Après chargement du buffer

...
Ptr = 0
Get_String Text1,6
Ptr = Ptr + 6
Get_String Text2,20
...

Private Sub Get_String(strUNICODE as String, byval Length
as Long)

Dim Cptr as Long

strUNICODE = space(Length)
For Cptr = 1 to Length
mid(strUNICODE,Cptr,1) = chr(Buffer(Ptr))
Ptr = Ptr + 1
Next Cptr

End Sub

Merci d'avance.


Jean