Я бы не додумался ТАК...
static vector<unsigned char> hexToBinary(const string& p_hex)
{
size_t size = p_hex.size() / 2;
vector<unsigned char> v(size);
for (size_t i = 0; i < size; i++)
{
v[i] = (hexToBinary(p_hex[i * 2]) << 4) | hexToBinary(p_hex[i * 2 + 1]);
}
return v;
}
static unsigned char hexToBinary(char p_digit)
{
switch(p_digit)
{
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
return p_digit - '0';
break;
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
return 10 + p_digit - 'A';
break;
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
return 10 + p_digit - 'a';
break;
default:
{
stringstream ss;
ss << "non hexidecimal digit " << p_digit << " encountered";
throw ServiceException(ss.str());
}
}
}
Здравствуйте, Alf, Вы писали:
static vector<unsigned char> hexToBinary(const string& p_hex)
{
size_t size = p_hex.size() / 2;
vector<unsigned char> v(size);
for (size_t i = 0; i < size; i++)
{
v[i] = (hexToBinary(p_hex[i * 2]) << 4) | hexToBinary(p_hex[i * 2 + 1]);
}
return v;
}
static unsigned char hexToBinary(char p_digit)
{
switch(p_digit)
{
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
return p_digit - '0';
break;
case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
return 10 + p_digit - 'A';
break;
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
return 10 + p_digit - 'a';
break;
default:
{
stringstream ss;
ss << "non hexidecimal digit " << p_digit << " encountered";
throw ServiceException(ss.str());
}
}
}
Здравствуйте, Alf, Вы писали:
Alf>...
Alf>static unsigned char hexToBinary(char p_digit)
Alf>{
Alf> switch(p_digit)
Alf> {
Alf> case '0': case '1': case '2': case '3': case '4':
Alf> case '5': case '6': case '7': case '8': case '9':
Alf> return p_digit - '0';
Alf> break;
Alf> case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
Alf> return 10 + p_digit - 'A';
Alf> break;
Alf> case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
Alf> return 10 + p_digit - 'a';
Alf> break;
Alf> default:
Alf> {
Alf> stringstream ss;
Alf> ss << "non hexidecimal digit " << p_digit << " encountered";
Alf> throw ServiceException(ss.str());
Alf> }
Alf> }
Alf>}
Alf>
Это ещё что — помнится мне показывали код в mkisofs, так там switch был по всем значениям char.