How to convert Hexadecimal to Decimal?
Only with the following line I could solve the issue!
fprintf(stderr, "Voltage: %.2f\n", value/100.00);
value was shown as HEX as I used to use %p! Then I changed to x! and as I need to have 124.06 not 12406, in other words 2 decimal points, I added .2f!
Thank you all!
How to convert numbers between hexadecimal and decimal
To convert from decimal to hex do...
string hexValue = decValue.ToString("X");
To convert from hex to decimal do either...
int decValue = int.Parse(hexValue, System.Globalization.NumberStyles.HexNumber);
or
int decValue = Convert.ToInt32(hexValue, 16);
C++: Converting Hexadecimal to Decimal
#include <iostream>
#include <iomanip>
#include <sstream>
int main()
{
int x, y;
std::stringstream stream;
std::cin >> x;
stream << x;
stream >> std::hex >> y;
std::cout << y;
return 0;
}
How to convert hexadecimal to decimal in C?
just edited your code a little bit,
and also the input is supposed to be from left to right
instead of right to left according to which I think you have coded.
Hence, for C6, first C then 6.
#include <stdio.h>
int main()
{
int i, k, j, N, power, result;
char array[50];
result = 0;
printf("how many charecters are there in your hexadecimal input?: \n");
scanf("%d", &N);
for(k=0; k<=N-1; k++)
{
printf("What is the %d . value of your hexadecimal input?: \n", k+1);
scanf(" %c", &array[k]);
}
for(i=0; i<=N-1; i++)
{
power = 1;
for(j=0; j<=N-2-i; j++)
{
power = power*16;
}
if((array[i] >= '0') && (array[i] <= '9'))
{
result = result + (array[i] - '0') * power;
}
else
{
result = result + (array[i] - 'A' + 10) * power;
}
}
printf("your result is %d", result);
return 0;
}
the takeaways
- your input taking is inefficeient. and in that too %c instead of %s in scanf.
if((array[i] >= 0) && (array[i] <= 9))
, suppose i=1 and array[1] = 7. Now, you are comparing ascii value of 7 which is 55, instead of 7.- same thing in the statement of if, the ascii values of array[i] are being used so I have subtracted accordingly.
Im trying to convert a hexadecimal number to decimal number in C++
#include <iostream>
#include <string>
using namespace std;
int main(){
string s;
cin>>s;//input string
int ans=0;//for storing ans
int p=1;
int n=s.size();//length of the string
//travarsing from right to left
for(int i=n-1;i>=0;i--){
//if the character is between 0 to 9
if(s[i]>='0'&&s[i]<='9'){
ans=ans+p*(s[i]-'0');
}
//if the character is between A to F
else{
ans=ans+p*(s[i]-'A'+10);
}
p=p*16;
}
cout<<ans;
return 0;
}
Convert a hexadecimal string to an integer efficiently in C?
You want strtol
or strtoul
. See also the Unix man page
How to convert hex to decimal in c#.net?
Console.Write("Enter HEX: ");
string hexValues = Console.ReadLine();
string[] hexValuesSplit = hexValues.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine("HEX = DECIMAL");
foreach (String hex in hexValuesSplit)
{
// Convert the number expressed in base-16 to an integer.
int value = Convert.ToInt32(hex, 16);
Console.WriteLine(string.Format("{0} = {1}", hex, Convert.ToDecimal(value)));
}
Console.ReadKey();
P.S. : The original code does not belong to me. For original codes please refer to MSDN
What do 48 and 87 values refer to when converting hexadecimal to decimal number in C?
48 is the ASCII code for '0'; the ASCII codes for 'A' and 'a' are 65 (55 = 65-10) and 97 (87 = 97 - 10) respectively.
Related Topics
Explain C++ Sfinae to a Non-C++ Programmer
I'Ve Heard I++ Isn't Thread Safe, Is ++I Thread-Safe
C++11 Lambda Implementation and Memory Model
Very Poor Boost::Lexical_Cast Performance
What Is the Meaning and Usage of _Stdcall
Is There a Linq Library for C++
Are Parentheses Around the Result Significant in a Return Statement
Does the C++ Volatile Keyword Introduce a Memory Fence
Why Can't I Capture This By-Reference ('&This') in Lambda
Compile the Python Interpreter Statically
Nested Templates with Dependent Scope
What Is Uint_Fast32_T and Why Should It Be Used Instead of the Regular Int and Uint32_T
Is Floating Point Addition Commutative in C++
Std::Optional Specialization for Reference Types
Why Does VS Not Define the Alternative Tokens for Logical Operators
Is Std::Pair<Int, Std::String> Ordering Well-Defined