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?
How can I write java program code that do reverse, replace, remove char from string without...
How can I write java program code that do reverse, replace, remove char from string without using reverse, replace, remove method. Only string method that I can use are length, concat, charAt, substring, and equals (or equalsIgnoreCase).
Suppose I have a 32 bit register containing 0000AC12 in hex and I do a logical...
Suppose I have a 32 bit register containing 0000AC12 in hex and I do a logical shift >>2. What hex digits will result from the binary shift? Be sure to show all 32 bits in hex
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?
(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!
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT