2010-04-04 4 views
1

Comment puis-je entrer DEADBEEF et afficher DEBEEF comme tableaux de quatre octets?chaîne au tableau d'octets

+0

Vous avez besoin d'une balise 'homework'? –

Répondre

4
void hexconvert(char *text, unsigned char bytes[]) 
{ 
    int i; 
    int temp; 

    for(i = 0; i < 4; ++i) { 
     sscanf(text + 2 * i, "%2x", &temp); 
     bytes[i] = temp; 
    } 
} 
+0

Merci. C'est du travail. – hlgl

2

Semble que vous voulez analyser une chaîne hexadécimale en un nombre entier. Le chemin C++:

#include <iostream> 
#include <sstream> 
#include <string> 

template <typename IntType> 
IntType hex_to_integer(const std::string& pStr) 
{ 
    std::stringstream ss(pStr); 

    IntType i; 
    ss >> std::hex >> i; 

    return i; 
} 

int main(void) 
{ 
    std::string s = "DEADBEEF"; 
    unsigned n = hex_to_integer<unsigned>(s); 

    std::cout << n << std::endl; 
}