slt,je voudrais savoir comment convetir un tableau de char en tableau de byte?
MerciCode:
1
2
3 const char* buffer="0010FFE000111ED"; //en hexadécimal BYTE* buffer_en_byte;
Version imprimable
slt,je voudrais savoir comment convetir un tableau de char en tableau de byte?
MerciCode:
1
2
3 const char* buffer="0010FFE000111ED"; //en hexadécimal BYTE* buffer_en_byte;
bon, j'ai essayé ca
mai j'obtiens une exception:Format invalide :(Code:
1
2
3
4
5
6
7
8
9
10 void Serveur::CharToByte(const char *p ,BYTE* ByteArray, int longueur) { for (int ix = 0; ix < longueur; ix++ ) { unsigned int i; sscanf_s(p,"%2.2x",&i);// sscanf_s(p,"%2x",&i); ByteArray[ix] = i; p+=2; }
Pourquoi passer par un char* ???
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 #include "stdafx.h" #define byte unsigned char int _tmain(int argc, _TCHAR* argv[]) { unsigned long long start_adress = 0x10FFE000111EDl; byte* startadress = reinterpret_cast<byte*>(&start_adress); byte toto = startadress[0]; byte titi = startadress[1]; byte tata = startadress[2]; byte tutu = startadress[3]; return 0; }
je ne peux pas utiliser unsigned long long .En fait, j'ai une application client serveur . Le client et le serveur font un chat avec des chaines de caracteres contenant des valeurs hexadecimaux. Lorsque le client, par exemple recoit la chaine ch il la convertit en tableau de byte pour effectuer des traitements.
bon j'ai essayé ca aussi mai je croi que ca marche pa bien :
pour l'appel:Code:
1
2
3
4
5
6
7
8
9
10 unsigned CClientApp::CharToHex(char in ) { switch(in) { case'0': case'1': case'2': case'3': case'4': case'5': case'6':case'7': case'8': case'9': return (in-'0'); case'A':case'B': case'C':case'D': case'E':case'F':return(in-'A'+10); } }
Code:
1
2
3
4
5
6
7
8
9
10
11
12 int len_buffreq=strlen(buffreq); int j=0;int i=0; unsigned x;unsigned y; while(i<len_buffreq) { x=CharToHex(buffreq[i]); y=CharToHex(buffreq[i+1]); byte_buffreq[j]=((x<<4) | y); j++; i+=2; }
Voir à la fin de cette FAQ
http://www.codeguru.com/forum/showthread.php?t=316299
Mais franchement, je ne vois pas d'intérêts à toutes ces manipulations de chaînes sauf peut-être pour faire des bots et des virus se propageant par IRC ou MSN faits par des scripts kiddies boutonneux.