Question

In: Computer Science

How do I covert a char from a sting that is made up of hex values...

How do I covert a char from a sting that is made up of hex values into a long int, without using istringstream and istring in c++?

Solutions

Expert Solution

Solution:

c++ code

#include<bits/stdc++.h>
using namespace std;
long fun(string s){

long res=16,k=0,ans=0;

for(int i=s.length()-1;i>=0;i--){

//we are traversing string from right to left

if(s[i]>='0'&&s[i]<='9'){

ans+=pow(res,k)*(s[i]-'0');

}

else if(s[i]=='a'||s[i]=='b'||s[i]=='c'||s[i]=='d'||s[i]=='e'||s[i]=='f'){

//ascii value of W = 87 and a in hexadecimal is 10 and

//ascii value of a =97

ans+=pow(res,k)*(s[i]-'W');

}

else if(s[i]=='A'||s[i]=='B'||s[i]=='C'||s[i]=='D'||s[i]=='E'||s[i]=='F'){

//ascii value of char 7 is 55 and

//ascii value of char A is 65

ans+=pow(res,k)*(s[i]-'7');

}

k++;

}
}

int main(){

string s;

cout<<"Enter a hexadecimal string"<<endl;

cin>>s;//taking input

//calling fun function with argument input string

long res=fun(s);

cout<<"long int value is "<<res<<endl;
}

Screenshot of the code and output:

NOTE:

If you are satisfied with my answer please do upvote and if you have any kind of doubts please post in the comment section. I'll surely help you there.
Thank You:)


Related Solutions

in verilog if i have a hex number how do i get a specific 4 bits?...
in verilog if i have a hex number how do i get a specific 4 bits? for exampe if i have a hex number 5055 how do i get the last four bits which would be 0101?
Follow up question to the recommendations made to Jerry , how do I get to $44.96?...
Follow up question to the recommendations made to Jerry , how do I get to $44.96? I'm confused
1) Covert the following binary values to decimal. Do this interpreting the binary as unsigned and...
1) Covert the following binary values to decimal. Do this interpreting the binary as unsigned and signed. a. 0111 1001 b. 1000 0000 c. 1111 1111 PLEASE EXPLAIN IT IN DETAIL
how do I write a class PostFix that has one method covert that converts an infix...
how do I write a class PostFix that has one method covert that converts an infix expression (as a string input) to postfix in java?
(In C language) Given a two-dimensional char array, how do I encode it into a one...
(In C language) Given a two-dimensional char array, how do I encode it into a one dimensional integer array? For example: char arr [8][8] = {{1,1,1,1,1,1,1,1}, {1,0,0,0,1,0,0,1}, {1,0,1,0,1,1,0,1}, {1,0,1,0,0,0,0,1}, {1,0,1,1,1,1,0,1}, {1,0,0,0,0,0,0,1}, {1,0,1,0,1,0,1,1}, {1,1,1,1,1,1,1,1}} into int arr2 [8] I know this problem requires bit-shifting but I am unsure how. It also needs to be as efficient as possible. Thanks!
When already given tool values, how do I create cut-off values from scratch for a screening...
When already given tool values, how do I create cut-off values from scratch for a screening tool?
I have to do this "interview", I just need truthful/made-up answers to these questions (Include the...
I have to do this "interview", I just need truthful/made-up answers to these questions (Include the position you hold and why someone in that position can give a better feel for how someone might use accounting someday) 1. I would like to understand if and how financial reporting affects you and your work. Can you give me any examples of how accounting affects your day-to-day responsibilities or decisions? 2. What do other managers and leaders in your company use your...
I have to do this "interview", I just need truthful/made-up answers to these questions (Include the...
I have to do this "interview", I just need truthful/made-up answers to these questions (Include the position you hold and why someone in that position can give a better feel for how someone might use accounting someday) 1.      I would like to understand if and how financial reporting affects you and your work. Can you give me any examples of how accounting affects your day-to-day responsibilities or decisions? 2.      What do other managers and leaders in your company use your...
how do i construct a scatterplot if L1 lists negative values and L2 has values such...
how do i construct a scatterplot if L1 lists negative values and L2 has values such as 0.00339156, 0.00326318, 0.00313725 ? Do i enter the L2 values as exponents??
I have the solution.. I just do not know how they came up with some of...
I have the solution.. I just do not know how they came up with some of the answers. e.g. sales return and allowande. Exercise 5-11 Calculating income statement components L01,5 Referring to Exhibit 5.15 calculate the missing amounts (round to two decimal places). Company A Company B 2020 2019 2020 2019 Sales     $263,000.00 $187,000.00 ? 114200.00 $48,500.00 Sales Discount $2,630.00 ? 1350.00 $1,200.00 $570.00 Sales Return and Allowance ? 51570 $16,700.00 $6,200.00 ? 2430.00 Net Sales ? 208800.00 $168,950.00...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT