begin process at 2012 02 13 03:36:05
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Réseau & Internet

 > STOCKER PLUSIEURS STRINGS EN UNE SEULE PUIS LES RETROUVER (PRATIQUE POUR PROG RÉSEAU)

STOCKER PLUSIEURS STRINGS EN UNE SEULE PUIS LES RETROUVER (PRATIQUE POUR PROG RÉSEAU)


 Information sur la source

 Description

Bonjour à tous ! :o)
Bon, j'avoue que le titre n'est peut-être pas très clair, en fait, c'est une classe un peu les NetworkPacket en DirectPlay. Vous écrivez différentes chaines de caractères, elles sont mises en une seule, puis vous pouvez les récuperer indépendemment... C'est très utile pour le réseau car ça permet de passer une commande et des arguments à un ordinateur distant... (pour un peu plus d'explication sur la problèmatique qui m'a amené à faire ça vous pouvez faire un tit tour sur mon blog: http://www.livejournal.com/users/lordthrend ca vous aidera peut-être à mieux comprendre de quoi il retourne ;))
L'utilisation est assez simple: WriteString permet d'ajouter une string et ReadString permet de lire la string suivante et ToString permet de récuperer LA string qui contient toutes les strings (c'est celle-là qu'il faut envoyer lorsque vous utilisez un socket ;)) et LoadFromEncoded permet de recréer à partir d'une string donnée par ToString.

Source

  • Public Class StringPacket
  • Const charseparator As Char = "&"
  • Dim strings(0) As String
  • Dim c As Integer = 0
  • Dim t As Integer = 0
  • Public Sub WriteString(ByVal str As String)
  • ' Add(HttpUtility.UrlEncode(str))
  • Add(URLEncode(str))
  • End Sub
  • Public Overrides Function ToString() As String
  • Return String.Join(" ", strings)
  • End Function
  • Private Sub Add(ByVal str As String)
  • ReDim Preserve strings(t)
  • strings(t) = str
  • t = t + 1
  • 'If UBound(strings) > 1 Then
  • ' ReDim Preserve strings(UBound(strings) + 1)
  • 'End If
  • 'strings(UBound(strings)) = str
  • 'MsgBox(UBound(strings))
  • 'If UBound(strings) = 0 Then
  • ' ReDim Preserve strings(UBound(strings) + 1)
  • 'End If
  • End Sub
  • Public Sub LoadFromEncoded(ByVal str As String)
  • strings = str.Split(" ")
  • Dim astr As String
  • Dim i As Integer
  • For i = 0 To UBound(strings)
  • strings(i) = URLDecode(strings(i))
  • Next
  • End Sub
  • Public Function ReadString() As String
  • c = c + 1
  • Return strings(c - 1)
  • End Function
  • Public Function URLEncode(ByVal strData As String) As String
  • Dim I As Integer
  • Dim strTemp As String
  • Dim strChar As String
  • Dim strOut As String
  • Dim intAsc As Integer
  • strTemp = Trim(strData)
  • For I = 1 To Len(strTemp)
  • strChar = Mid(strTemp, I, 1)
  • intAsc = Asc(strChar)
  • If (intAsc >= 48 And intAsc <= 57) Or _
  • (intAsc >= 97 And intAsc <= 122) Or _
  • (intAsc >= 65 And intAsc <= 90) Then
  • strOut = strOut & strChar
  • Else
  • strOut = strOut & "%" & Hex(intAsc)
  • End If
  • Next I
  • Return strOut
  • End Function
  • Private Function URLDecode(ByVal enStr)
  • Dim deStr
  • Dim c, i, v
  • deStr = ""
  • For i = 1 To Len(enStr)
  • c = Mid(enStr, i, 1)
  • If c = "%" Then
  • v = "&h" + Mid(enStr, i + 1, 2)
  • If v < 128 Then
  • deStr = deStr & Chr(v)
  • i = i + 2
  • Else
  • If isvalidhex(Mid(enStr, i, 3)) Then
  • If isvalidhex(Mid(enStr, i + 3, 3)) Then
  • v = "&h" + Mid(enStr, i + 1, 2) + Mid(enStr, i + 4, 2)
  • deStr = deStr & Chr(v)
  • i = i + 5
  • Else
  • v = "&h" + Mid(enStr, i + 1, 2) + CStr(Hex(Asc(Mid(enStr, i + 3, 1))))
  • deStr = deStr & Chr(v)
  • i = i + 3
  • End If
  • Else
  • deStr = deStr & c
  • End If
  • End If
  • Else
  • If c = "+" Then
  • deStr = deStr & " "
  • Else
  • deStr = deStr & c
  • End If
  • End If
  • Next
  • URLDecode = deStr
  • End Function
  • Private Function isvalidhex(ByVal str As String)
  • isvalidhex = True
  • str = str.ToUpper
  • If Len(str) <> 3 Then isvalidhex = False : Exit Function
  • If Left(str, 1) <> "%" Then isvalidhex = False : Exit Function
  • c = Mid(str, 2, 1)
  • If Not (((c >= "0") And (c <= "9")) Or ((c >= "A") And (c <= "Z"))) Then isvalidhex = False : Exit Function
  • c = Mid(str, 3, 1)
  • If Not (((c >= "0") And (c <= "9")) Or ((c >= "A") And (c <= "Z"))) Then isvalidhex = False : Exit Function
  • End Function
  • End Class
Public Class StringPacket
    Const charseparator As Char = "&"
    Dim strings(0) As String
    Dim c As Integer = 0
    Dim t As Integer = 0
    Public Sub WriteString(ByVal str As String)
        '  Add(HttpUtility.UrlEncode(str))
        Add(URLEncode(str))
    End Sub
    Public Overrides Function ToString() As String
        Return String.Join(" ", strings)
    End Function
    Private Sub Add(ByVal str As String)
        ReDim Preserve strings(t)
        strings(t) = str
        t = t + 1
        'If UBound(strings) > 1 Then
        '    ReDim Preserve strings(UBound(strings) + 1)
        'End If
        'strings(UBound(strings)) = str
        'MsgBox(UBound(strings))
        'If UBound(strings) = 0 Then
        '    ReDim Preserve strings(UBound(strings) + 1)
        'End If
    End Sub
    Public Sub LoadFromEncoded(ByVal str As String)
        strings = str.Split(" ")
        Dim astr As String
        Dim i As Integer

        For i = 0 To UBound(strings)
            strings(i) = URLDecode(strings(i))
        Next
    End Sub
    Public Function ReadString() As String
        c = c + 1
        Return strings(c - 1)
    End Function
    Public Function URLEncode(ByVal strData As String) As String

        Dim I As Integer
        Dim strTemp As String
        Dim strChar As String
        Dim strOut As String
        Dim intAsc As Integer

        strTemp = Trim(strData)
        For I = 1 To Len(strTemp)
            strChar = Mid(strTemp, I, 1)
            intAsc = Asc(strChar)
            If (intAsc >= 48 And intAsc <= 57) Or _
               (intAsc >= 97 And intAsc <= 122) Or _
               (intAsc >= 65 And intAsc <= 90) Then
                strOut = strOut & strChar
            Else
                strOut = strOut & "%" & Hex(intAsc)
            End If
        Next I

        Return strOut

    End Function

    Private Function URLDecode(ByVal enStr)
        Dim deStr
        Dim c, i, v
        deStr = ""
        For i = 1 To Len(enStr)
            c = Mid(enStr, i, 1)
            If c = "%" Then
                v = "&h" + Mid(enStr, i + 1, 2)
                If v < 128 Then
                    deStr = deStr & Chr(v)
                    i = i + 2
                Else
                    If isvalidhex(Mid(enStr, i, 3)) Then
                        If isvalidhex(Mid(enStr, i + 3, 3)) Then
                            v = "&h" + Mid(enStr, i + 1, 2) + Mid(enStr, i + 4, 2)
                            deStr = deStr & Chr(v)
                            i = i + 5
                        Else
                            v = "&h" + Mid(enStr, i + 1, 2) + CStr(Hex(Asc(Mid(enStr, i + 3, 1))))
                            deStr = deStr & Chr(v)
                            i = i + 3
                        End If
                    Else
                        deStr = deStr & c
                    End If
                End If
            Else
                If c = "+" Then
                    deStr = deStr & " "
                Else
                    deStr = deStr & c
                End If
            End If
        Next
        URLDecode = deStr
    End Function

    Private Function isvalidhex(ByVal str As String)
        isvalidhex = True
        str = str.ToUpper
        If Len(str) <> 3 Then isvalidhex = False : Exit Function
        If Left(str, 1) <> "%" Then isvalidhex = False : Exit Function
        c = Mid(str, 2, 1)
        If Not (((c >= "0") And (c <= "9")) Or ((c >= "A") And (c <= "Z"))) Then isvalidhex = False : Exit Function
        c = Mid(str, 3, 1)
        If Not (((c >= "0") And (c <= "9")) Or ((c >= "A") And (c <= "Z"))) Then isvalidhex = False : Exit Function
    End Function
End Class

 Conclusion

Les fonctions d'urlencode et decode ont été trouvées sur le net (mais je ne sais plus trop où), je sais le framework en propose mais je visais aussi le Compact Framework qui lui ne les supporte pas...
Voilà ! J'espère que cela vous aidera !


 Sources du même auteur

DHTML... UN SITE TRES DYNAMIQUE
Source avec Zip PARTICULES - CHAMPS D' ÉTOILES

 Sources de la même categorie

Source avec Zip Source avec une capture GESTIONNAIRE DE TÉLÉCHARGEMENT, AVEC REPRISE ET MULTITHREADI... par Madx23
Source avec Zip Source avec une capture CONVERTIR DU TEXTE RTF EN CODE HTML ET VICE-VERSA par vicosta
Source avec Zip Source avec une capture DICTIONAIRE TEXT/AUDIO/VISUELLE ANGLAIS AVEC WEBBROWSER CONT... par majnounmajda
Source avec Zip Source .NET (Dotnet) NSLOOKUP EN VB.NET OU COMMENT FAIRE UNE REQÛETE DNS EN PRÉCI... par ShareVB
Source avec Zip Source avec une capture MINI SEVEUR HTTP AVEC INTERFACE GRAPHIQUE ET IMPLÉMENTATIONS... par lemout

Commentaires et avis

Commentaire de kimmelf2 le 02/03/2004 00:01:01

un petit conseille : evite d'appeler tes variables comme les mots clefs !

y'a de forte chances que VB ne sache pas faire la difference entre ta variable str et la fonction str !

Commentaire de Benj1105 le 02/03/2004 16:39:19

Bien vu ! Je n'avais pas vu que str était un mot clef aussi ! (heu ceci dit VB fait très bien la différence mais la guideline du Framework veut de toutes façons que l'on évite cela...)

Commentaire de kimmelf2 le 02/03/2004 23:29:45

perso j'ai deja eu des cas ou ca buggai.

je suis daccord avec toi, ca PEU marcher, mais c'est pas conseiller de jouer avec ;-)

Commentaire de Blanc le 08/03/2004 22:10:46

Personnelement, j'adore utiliser str pour mes variables string...

 Ajouter un commentaire




Nos sponsors


Sondage...

CalendriCode

Février 2012
LMMJVSD
  12345
6789101112
13141516171819
20212223242526
272829    

Consulter la suite du CalendriCode

Photothèque

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 1,045 sec (3)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales