Advertisement
ALTracer

hex2dec.c

Dec 20th, 2018
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.45 KB | None | 0 0
  1. /*
  2.  * hex2dec.cxx
  3.  *
  4.  * Copyright 2018 Денис <altracer@AspireE15>
  5.  *
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or
  9.  * (at your option) any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; if not, write to the Free Software
  18.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  19.  * MA 02110-1301, USA.
  20.  *
  21.  *
  22.  */
  23.  
  24. #include <iostream>
  25. #include <math.h>
  26. #include <locale.h>
  27. using namespace std;
  28.  
  29. int main(int argc, char **argv)
  30. {
  31.     setlocale(LC_ALL,"RUS");
  32.     short l = 2;
  33.     char a[l+1]; // last symbol is line terminator '\0'
  34.     short b = 0;
  35.     short i,j;
  36.     cout << "Введите шестнадцатеричное число от 00h до FFh (без h):" << endl;
  37.     cin >> a;
  38.    
  39.     const char hex_alphabet[17] = "0123456789ABCDEF";
  40.     int dec_alphabet[16];
  41.     for(i=0;i<=15;i++) dec_alphabet[i]=i;
  42.    
  43.     for(i=0;i<=l-1;i++){
  44.         for(j=0;j<=15;j++){
  45.             if(a[l-1-i]==hex_alphabet[j]) b+=dec_alphabet[j]*pow(16,i);
  46.         };
  47.     };
  48.    
  49.     cout << b <<endl;
  50.     return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement