16진수를 입력받아 주어진 16진수를 10진수로 변환하는 작업입니다.
컴퓨터의 16진수는 16진수로 표시되고 10진수는 10진수로 표시되고 0-9 값으로 표시되는 반면 16진수는 0-15부터 시작하는 숫자를 가지며 10은 A, 11은 B, 12는 C, 13은 D, 14는 E, 15는 F입니다.
16진수를 10진수로 변환하려면 다음 단계를 따르십시오 -
- 오른쪽에서 왼쪽으로 나머지를 통해 숫자를 추출한 다음 0에서 시작하는 거듭제곱을 곱하고 (자릿수) – 1이 될 때까지 1씩 증가합니다.
- 16진법에서 2진법으로의 변환을 수행해야 하기 때문에 8진법은 16진법이므로 거듭제곱의 기수는 16이 됩니다.
- 주어진 입력의 자릿수에 밑수와 거듭제곱을 곱하고 결과를 저장합니다.
- 곱한 값을 모두 더하여 십진수가 되는 최종 결과를 얻습니다.
다음은 16진수를 10진수로 변환한 그림입니다.

예
Input-: ACD A(10) will be converted to a decimal number by -: 10 X 16^2 = 2560 C(12) will be converted to a decimal number by -: 12 X 16^1 = 192 D(13) will be converted to a decimal number by -: 13 X 16^0 = 13 Output-: total = 13 + 192 + 2560 = 2765에 의해 10진수로 변환됩니다.
알고리즘
Start Step 1-> declare function to convert hexadecimal to decimal int convert(char num[]) Set int len = strlen(num) Set int base = 1 Set int temp = 0 Loop For int i=len-1 and i>=0 and i— IF (num[i]>='0' && num[i]<='9') Set temp += (num[i] - 48)*base Set base = base * 16 End Else If (num[i]>='A' && num[i]<='F') Set temp += (num[i] - 55)*base Set base = base*16 End return temp step 2-> In main() declare char num[] = "3F456A" Call convert(num) Stop
예
#include<iostream>
#include<string.h>
using namespace std;
//convert hexadecimal to decimal
int convert(char num[]) {
int len = strlen(num);
int base = 1;
int temp = 0;
for (int i=len-1; i>=0; i--) {
if (num[i]>='0' && num[i]<='9') {
temp += (num[i] - 48)*base;
base = base * 16;
}
else if (num[i]>='A' && num[i]<='F') {
temp += (num[i] - 55)*base;
base = base*16;
}
}
return temp;
}
int main() {
char num[] = "3F456A";
cout<<num<<" after converting to deciaml becomes : "<<convert(num)<<endl;
return 0;
} 출력
위의 코드를 실행하면 다음 출력이 생성됩니다.
3F456A after converting to deciaml becomes : 4146538