conversion d'une trame hex en binaire
bonsoir,
j'ai rencontré un petit problème concernant le décodage d'une trame , donc mon trame reçu est en hex et je veux la convertir en binaire puis mettre ses variable binaire dans un tableau pour les traiter ensuite , voila c'est mon code avec 0 erreur , quand je compile tout a fait normal avec des entiers(0->9) mais quand il reçoit une lettre (A->F) une erreur de compilation apparaît , je trouve pas la solution pour cette erreur.
merci pour votre aide.
Code:
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
#include "stdafx.h"
#include <iostream>
#include <sstream>
#include <bitset>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
using namespace std;
const char* hex2bin(char c)
{
switch (toupper(c))
{
case '0': return "0000";
case '1': return "0001";
case '2': return "0010";
case '3': return "0011";
case '4': return "0100";
case '5': return "0101";
case '6': return "0110";
case '7': return "0111";
case '8': return "1000";
case '9': return "1001";
case 'a': return "1010";
case 'b': return "1011";
case 'c': return "1100";
case 'd': return "1101";
case 'e': return "1110";
case 'f': return "1111";
}
}
std::string hex_to_bin( std::string& hex)
{
std::string bin;
for (unsigned i = 0; i <8; ++i)
bin += hex2bin(hex[i]);
return bin ;
}
std::string dfg(const std::string resp[10])
{
std::string RS;
std::string a;
std::string tab[16];
for (int i = 4; i < 8; ++i)
RS += resp[i];
a = hex_to_bin(RS);
for (int i = 0; i != a.length(); ++i)
{
tab[i] = a[i];
}
StatusFlag();
for (int i = 0; i < 16; i++)
{
if (tab[i] == "1")
status[i].statu = true;
else
status[i].statu = false;
}
return RS;
}
int main()
{
std::string b;
std::string resp[10] = { "25", "9f", "63", " 25", "36", "42", "15", "EC", "5b", "a9" };
b = dfg(resp);
return 0;
} |