Accueil > > > 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
Sources de la même categorie
Commentaires et avis
|
Derniers Blogs
[WP7] DYNAMICALLY CHANGE STARTUP PAGE[WP7] DYNAMICALLY CHANGE STARTUP PAGE par KooKiz
Let's say that you want to allow the user to customize the startup page of your application. You can easily change the startup page by editing the 'NavigationPage' attribute in the manifest file. But the manifest cannot be modified once the applicatio...
Cliquez pour lire la suite de l'article par KooKiz SESSION SILVERLIGHT 5 3D : SLIDES ET DEMOSSESSION SILVERLIGHT 5 3D : SLIDES ET DEMOS par Groc
Durant les techdays, j'ai eu le plaisir d'animer une session sur Silverlight 5 et la 3D avec Simon Ferquel. Comme promis, voici nos slides et mes démos (celles avec le viper BSG) ici et là. Pour mémoire, les démos utilisent toutes le viper BSG...
Cliquez pour lire la suite de l'article par Groc [TECHDAYS 2012] SESSION WEBMATRIX 2 : LE COUTEAU SUISSE GRATUIT POUR VOS DéVELOPPEMENTS WEB - SLIDES[TECHDAYS 2012] SESSION WEBMATRIX 2 : LE COUTEAU SUISSE GRATUIT POUR VOS DéVELOPPEMENTS WEB - SLIDES par gpommier
Suite à la session que j'ai présenté sur WebMatrix 2, vous pouvez trouver les slides ici, ainsi que les démos en packages nuget : démos1 et démos2 J'en profite pour remercier chaleureusement tous ceux qui sont venus très nombreux à cette sess...
Cliquez pour lire la suite de l'article par gpommier [SHAREPOINT] LES SESSIONS TECHDAYS 2012.[SHAREPOINT] LES SESSIONS TECHDAYS 2012. par Patrick Guimonet
Voici donc pour ceux qui n'ont pas pu venir, ou ceux qui n'ont pas pu toutes les suivre la liste des sessions SharePoint aux TechDays 2012, que je mettrais à jour dès que les liens des vidéo seront disponibles. Ou ici : http...
Cliquez pour lire la suite de l'article par Patrick Guimonet TECHDAYS PARIS 2012 : SESSION PLEINIèRE JOUR 3TECHDAYS PARIS 2012 : SESSION PLEINIèRE JOUR 3 par ROMELARD Fabrice
Speaker: Bernard Ourghanlian Cette session est comme chaque jour transmise en live par BrainSonic, et j'ai donc suivi cette troisième pleinière par ce moyen sur mon iPad . Elle est dédiée comme chaque année à la mise en perspective de l'é...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice
Forum
LISTER KEYS.KEYLISTER KEYS.KEY par Onin42
Cliquez pour lire la suite par Onin42
Logiciels
Tribler (2012)TRIBLER (2012)Tribler est un client pair à pair (P2P/Peer-to-Peer) open source avec la capacité de regarder des... Cliquez pour télécharger Tribler OneSwarm (2012)ONESWARM (2012)Le peer-to-peer qui protège votre vie privée, c'est OneSwarm.
Ce logiciel de peer-to-peer crypté... Cliquez pour télécharger OneSwarm PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System Easy-Planning (1.0.0.1)EASY-PLANNING (1.0.0.1)Basé sur les mêmes principes que MyPlanning, Easy-Planning permet de créer des plannings sous la ... Cliquez pour télécharger Easy-Planning
|