int hexCharToDec(char hexChar) { switch (hexChar) { case 'a': return 10; break; case 'b': return 11; break; case 'c': return 12; break; case 'd': return 13; break; case 'e': return 14; break; case 'f': return 15; break; default: return hexChar - 48; break; } }
int hexToDec(string hexString) { std::transform(hexString.begin(), hexString.end(), hexString.begin(), ::tolower); int power = 0, ret = 0; for (string::reverse_iterator rit = hexString.rbegin(); rit != hexString.rend(); ++rit) { ret += hexCharToDec(*rit) * pow(16, power); power++; } return ret; }
namespace znanya
{
class Program
{
static void Main(string[] args)
{
Random rnd = new Random();
int kol = 0;
int[] arr = new int[10];
for(int i=0;i<10;i++)
{
arr[i] = rnd.Next(-1000, 1000);
if (arr[i] % 5 == 0)
kol++;
Console.Write(arr[i] + " ");
}
Console.WriteLine();
Console.WriteLine("{0} чисел кратно 5", kol);
Console.ReadKey();
}
}
}
#include <string>
#include <math.h>
#include <algorithm>
using namespace std;
int hexCharToDec(char hexChar)
{
switch (hexChar) {
case 'a':
return 10;
break;
case 'b':
return 11;
break;
case 'c':
return 12;
break;
case 'd':
return 13;
break;
case 'e':
return 14;
break;
case 'f':
return 15;
break;
default:
return hexChar - 48;
break;
}
}
int hexToDec(string hexString)
{
std::transform(hexString.begin(), hexString.end(), hexString.begin(), ::tolower);
int power = 0, ret = 0;
for (string::reverse_iterator rit = hexString.rbegin(); rit != hexString.rend(); ++rit) {
ret += hexCharToDec(*rit) * pow(16, power);
power++;
}
return ret;
}
int main()
{
string s;
cout << "Строка: ";
cin >> s;
cout << endl << "ответ: " << hexToDec(s) << endl;
return 0;
}