IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage C++ Discussion :

String vers array of bytes.


Sujet :

Langage C++

  1. #1
    Membre régulier
    Homme Profil pro
    Second de cuisine
    Inscrit en
    Avril 2005
    Messages
    193
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Second de cuisine
    Secteur : Alimentation

    Informations forums :
    Inscription : Avril 2005
    Messages : 193
    Points : 99
    Points
    99
    Par défaut String vers array of bytes.
    Bonjour,

    Je me demande quelle est la meilleure méthode pour passer un string (Réception d'un socket) vers un tableau de valeures hex.

    Exemples:

    Mon socket recoit: 01 00 01 00 05 6b 6b 6b 6b 6b
    Quelle est la meilleure facon de transformer ca, en genre
    map<int, byte> M;

    De telle sorte que dans une classe, je puisse parcourir dans un seul sens, mes valeurs


    Voici une esquisse de la structure de la classe:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
     
     
     
    class Packet {
    private:
    	struct packet_s {
    		std::string pckt;
    		std::map<int, int> bytes;
    		int count;
    	 } packet_s;
    	int index;
    	void ArrayOfBytes();
    	void PacketInfo();
    	void Next();
    public:
    	Packet();
    	Packet(std::string p);
    	Packet(std::string p, bool nfo);
    	int GetHeader();
    	int GetAction();
    	int ReadInt();
    	long ReadLong();
    	std::string ReadString(int l);
    	void Skip();
    	~Packet();
     };
    void Packet::ArrayOfBytes() {}
    Packet::Packet() {
     
    }
    Packet::Packet(std::string p) {
    	Packet(p, true);
    }
    Packet::Packet(std::string p, bool nfo) {
    	 packet_s.pckt = p;
    	 this->ArrayOfBytes();
    	 // count
    	 this->index = 0;
    	 if(nfo) {
    		this->PacketInfo();
    	 }
     }
     void Packet::Next() {
    	this->index++;
     }
     void Packet::Skip() {
    	 int j=2;
    	 if(j < 1) {
    		throw "cant skip";
    	 }
    	this->index += j;
     }
     void Packet::PacketInfo() { }
     int Packet::GetHeader() {}
     int Packet::GetAction() {}
     int Packet::ReadInt() {}
     long Packet::ReadLong() {}
     std::string Packet::ReadString(int l) {}

  2. #2
    Membre régulier
    Homme Profil pro
    Second de cuisine
    Inscrit en
    Avril 2005
    Messages
    193
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Second de cuisine
    Secteur : Alimentation

    Informations forums :
    Inscription : Avril 2005
    Messages : 193
    Points : 99
    Points
    99
    Par défaut
    Bonjour!
    Ayant résolu mon problème, enfin je crois.

    J'ai continué ma classe, voici ou jen suis:

    Ca c'est le prog:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    int main() {
    	Packet p = Packet::Packet(std::string("01 00 01 00 00 05"));
    	cout << "header: " << p.GetHeader() << " & action: " << p.GetAction() << endl;
    	XServer::Server::Launch();
    	while(true) { }
    	return 0;
    }
    Ca, ma classe:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    #include "includes.h"
     
     
    void Packet::ArrayOfBytes() {
    	std::stringstream ss(packet_s.pckt);
    	int t;
    	int x = 0;
    	while (ss >> std::hex >> t) {
    		packet_s.bytes[x] = t;
    		++x;
    	}
    	packet_s.count = x;
    }
    Packet::~Packet() {
    	//delete this;
    }
    Packet::Packet() {
    	throw "ERROR: PacketManager: Constructor is Packet::Packet(string)";
    }
    Packet::Packet(std::string &p) {
    	Packet(p, true);
    }
    Packet::Packet(std::string &p, bool nfo) {
    	 packet_s.pckt = p;
    	 packet_s.header = -1;
    	 packet_s.action = -1;
    	 ArrayOfBytes();
    	 // count
    	 index = 0;
    	 if(nfo) {
    		PacketInfo();
    	 }
     }
     void Packet::Next() {
    	++index;
     }
     void Packet::PacketInfo() {
    	packet_s.header = ReadInt();
    	Next();
    	packet_s.action = ReadInt();
    	Next();
    }
     int Packet::GetHeader() { return packet_s.header; }
     int Packet::GetAction() { return packet_s.action;}
     int Packet::ReadInt() { 
    	 Next();
    	 return packet_s.bytes[(index-1)];
     }
     long Packet::ReadLong() { return 0;}
     std::string Packet::ReadString(int l) { return "hi"; }

    Et quand je lance ca, j'ai dans ma console
    header: 9564834388434 & action 9564834388434

    Ca doit etre une adresse mémoire, je sais pas trop.

    merci, nico.

  3. #3
    Membre régulier
    Homme Profil pro
    Second de cuisine
    Inscrit en
    Avril 2005
    Messages
    193
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Second de cuisine
    Secteur : Alimentation

    Informations forums :
    Inscription : Avril 2005
    Messages : 193
    Points : 99
    Points
    99
    Par défaut
    Problème résolu

    Si jamais ca intéresse:


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    #include "includes.h"
     
     
    void Packet::ArrayOfBytes() {
    	std::stringstream ss(packet_s.pckt);
    	int t;
    	int x = 0;
    	while (ss >> std::hex >> t) {
    		packet_s.bytes[x] = t;
    		++x;
    	}
    	packet_s.count = x;
    }
    Packet::~Packet() {
    	//delete this;
    }
    Packet::Packet() {
    	throw "ERROR: PacketManager: Constructor is Packet::Packet(string)";
    }
    Packet::Packet(std::string &p) {
    	packet_s.pckt = p;
    	ArrayOfBytes();
    	index = 2;
    }
    void Packet::Next() {
    	++index;
    }
    int Packet::GetHeader() { return packet_s.bytes[0]; }
    int Packet::GetAction() { return packet_s.bytes[1]; }
    int Packet::ReadInt() { 
    	Next();
    	return packet_s.bytes[(index-1)];
    }
    long Packet::ReadLong() {
    	return (ReadInt() << 8) + ReadInt();
    }
    std::string Packet::ReadString() {
    	int length = ReadInt();
    	std::string str;
    	for(int i=index, j=i+length; i<j; ++i) {
    		str += packet_s.bytes[i];
    	}
    	index += length-1;
    	return str;
    }

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. String vers byte[]
    Par EpOnYmE187 dans le forum Langage
    Réponses: 8
    Dernier message: 28/10/2011, 11h23
  2. String of byte array to byte array
    Par LadyN dans le forum Collection et Stream
    Réponses: 4
    Dernier message: 07/04/2010, 19h14
  3. Réponses: 5
    Dernier message: 08/07/2008, 17h33
  4. Problème de parse de String vers Byte
    Par Johan.Mazel dans le forum Java ME
    Réponses: 5
    Dernier message: 25/07/2007, 12h56
  5. Conversion String vers Byte
    Par zulianithomas dans le forum Delphi
    Réponses: 8
    Dernier message: 07/10/2006, 10h09

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo