473,387 Members | 3,820 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Hexadecimal Addition

11
I want to be able to add two hexadecimal numbers . Below is the code. This code is for converting from hexadecimal to decimal, so what I want is that when I enter the second Hexadecimal value, it converts to Decimal and then adds both and then converts back to Hexadecimal and displays the added Hexadecimal value.


Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. #define _CRT_SECURE_NO_WARNINGS            //For scanf Errors
  4.  
  5. #include<stdio.h>
  6. #include<conio.h>
  7. #include<string.h>
  8. #include <stdlib.h>                        //For Clear Screen Errors
  9.  
  10.  
  11.  void main()
  12.  
  13. {
  14.          int i,a[20];
  15.          unsigned long int h=0,m=1;
  16.          char s[20];
  17.          system("cls");
  18.  
  19.         // printf("Convertion from Hexadecimal to Decimal");
  20.          printf("Enter 1st Hexadecimal number");
  21.          scanf("%s",s);
  22.         // printf("Enter 2nd Hexadecimal number");
  23.         // scanf("%s",q);
  24.         // printf("Decimal Equivalent=");
  25.          printf("Addition is = ");
  26.  
  27.          for(i=0;s[i]!=NULL;i++)
  28.          {
  29.  
  30.           switch(s[i])
  31.            {
  32.             case '0':
  33.              a[i]=0;
  34.              break;
  35.             case '1':
  36.              a[i]=1;
  37.              break;
  38.             case '2':
  39.              a[i]=2;
  40.              break;
  41.             case '3':
  42.              a[i]=3;
  43.              break;
  44.             case '4':
  45.              a[i]=4;
  46.              break;
  47.             case '5':
  48.              a[i]=5;
  49.              break;
  50.             case '6':
  51.              a[i]=6;
  52.              break;
  53.             case '7':
  54.              a[i]=7;
  55.              break;
  56.             case '8':
  57.              a[i]=8;
  58.              break;
  59.             case '9':
  60.              a[i]=9;
  61.              break;
  62.             case 'a':
  63.             case 'A':
  64.              a[i]=10;
  65.              break;
  66.             case 'b':
  67.             case 'B':
  68.              a[i]=11;
  69.              break;
  70.             case 'c':
  71.             case 'C':
  72.              a[i]=12;
  73.              break;
  74.             case 'd':
  75.             case 'D':
  76.              a[i]=13;
  77.              break;
  78.             case 'e':
  79.             case 'E':
  80.              a[i]=14;
  81.              break;
  82.             case 'f':
  83.             case 'F':
  84.              a[i]=15;
  85.              break;
  86.             default:
  87.              printf("Entered number is not Hexadecimal.Printed value is notcorrect.");
  88.              break;
  89.  
  90.            }
  91.          }
  92.          i--;
  93.          for(;i>=0;i--)
  94.          {
  95.           h=h+a[i]*m;
  96.           m=m*16;
  97.          }
  98.  
  99.           printf("%ld ",h);
  100.  
  101.           getch();
  102.  
  103. }
  104.  
Jan 15 '12 #1

✓ answered by donbock

I see this same kind of confusion over and over again in this forum. Bil M, please don't be upset over this reply; it is prompted more by the many other folks who posed this question than it is by what you wrote.

The experts get confused when you say you want to add hexadecimals, or you want to add decimal numbers, or you want to add some other kind of numbers, because the computer only recognizes one kind of number: binary.

What you want to do is
  • Allow the operator to enter a number that is expressed in some format. Either the format is implicit ('my program only accepts hexadecimal') or it is explicit ("0x" prefix means hexadecimal, etc).
  • Store the operator's entered number into a string.
  • Convert that string to a number.
  • Perform some operation on that number.
  • Convert the result number into a string representation according to some desired format.
  • Print the string representation of the result.
Which of these steps are you having trouble with?

10 26628
whodgson
542 512MB
Wonder why you would want to do that?
hexadecimal is just another format. You can work in hex format just as you can work in dec format
if x=2b4 (692) and y=3c7 (967)
x+y=67b (1659)
Jan 16 '12 #2
Bil M
11
This is actually a Hexa-to-deci converting code...so I've come up with the solution to convert two hexa to deci and add them together but im getting answer in decimal form...so how do I convert decimal form to hexadecimal form with the above codes and without adding whole new sets of codes
Jan 16 '12 #3
donbock
2,426 Expert 2GB
I see this same kind of confusion over and over again in this forum. Bil M, please don't be upset over this reply; it is prompted more by the many other folks who posed this question than it is by what you wrote.

The experts get confused when you say you want to add hexadecimals, or you want to add decimal numbers, or you want to add some other kind of numbers, because the computer only recognizes one kind of number: binary.

What you want to do is
  • Allow the operator to enter a number that is expressed in some format. Either the format is implicit ('my program only accepts hexadecimal') or it is explicit ("0x" prefix means hexadecimal, etc).
  • Store the operator's entered number into a string.
  • Convert that string to a number.
  • Perform some operation on that number.
  • Convert the result number into a string representation according to some desired format.
  • Print the string representation of the result.
Which of these steps are you having trouble with?
Jan 17 '12 #4
whodgson
542 512MB
If you stick with donbock all will be made clear.
Before I go though.... in line 21 your format specifier %s should be %x if you intend to provide a number in hex format. In other words you are providing a string representation of a hex number not a number in hex format.
So if you have:
Expand|Select|Wrap|Line Numbers
  1. scanf("%x",x);//if x = Ox2b4
  2. scanf("%x",y);//if y = Ox3c7
  3. printf("%x",x+Y)//prints Ox67b...I think
Jan 17 '12 #5
donbock
2,426 Expert 2GB
Do you want to support both positive and negative input values?
Are you required to write your own string-to-number conversion or can you use a standard library function? Take a look at strtol or strtoul.

These are the functions I would use in a C program. There may be some slick magical >> alternative if you're using C++. We'll see what the C++ experts have to say.

I'm not a big fan of scanf. I think it is easier to trap illegal input if you separate input from parsing; but that's more of a personal idiosyncracy than a hard and fast rule.

By the way, the main function always returns an int, never void. You should get in the habit of always writing it that way.
Jan 17 '12 #6
Bil M
11
Thanks donbock ....
My task asks for the conversion of Binary to Hexadecimal then the next step to add two hexadecimal numbers and subtract to hexadecimal numbers...so when I add two hexadecimal numbers...i convert them to decimal form and then add them ...but I am lost in converting the decimal number back to Hexadecimal value..

The values should only be positive....
So what is the other option instead of scanf?...

so for example if I provide E as first value and E as second and add them together. ..the result should show 1C...not 28...
Jan 19 '12 #7
Bil M
11
ive updated the code...in the code if you run it ...it will ask for two hexadecimal values...and the answer will be in decimal form..whereas I want it to be in Hexadecimal form
Jan 19 '12 #8
Bil M
11
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. #define _CRT_SECURE_NO_WARNINGS            //For scanf Errors
  4.  
  5. #include<stdio.h>
  6. #include<conio.h>
  7. #include<string.h>
  8. #include <stdlib.h>     
  9.  
  10. int main()
  11.  
  12. {
  13.              int i,a[20];
  14.          unsigned long int h=0,m=1,j=0,k=0;
  15.          char s[20],q[20];
  16.          system("cls");
  17.  
  18.         // printf("Convertion from Hexadecimal to Decimal");
  19.          printf("Enter 1st Hexadecimal number");
  20.          scanf("%s",s);
  21.         printf("Enter 2nd Hexadecimal number");
  22.         scanf("%s",q);
  23.         // printf("Decimal Equivalent=");
  24.          printf("Addition is = ");
  25.  
  26.          for(i=0;s[i]!=NULL;i++)
  27.          {
  28.  
  29.           switch(s[i])
  30.            {
  31.             case '0':
  32.              a[i]=0;
  33.              break;
  34.             case '1':
  35.              a[i]=1;
  36.              break;
  37.             case '2':
  38.              a[i]=2;
  39.              break;
  40.             case '3':
  41.              a[i]=3;
  42.              break;
  43.             case '4':
  44.              a[i]=4;
  45.              break;
  46.             case '5':
  47.              a[i]=5;
  48.              break;
  49.             case '6':
  50.              a[i]=6;
  51.              break;
  52.             case '7':
  53.              a[i]=7;
  54.              break;
  55.             case '8':
  56.              a[i]=8;
  57.              break;
  58.             case '9':
  59.              a[i]=9;
  60.              break;
  61.             case 'a':
  62.             case 'A':
  63.              a[i]=10;
  64.              break;
  65.             case 'b':
  66.             case 'B':
  67.              a[i]=11;
  68.              break;
  69.             case 'c':
  70.             case 'C':
  71.              a[i]=12;
  72.              break;
  73.             case 'd':
  74.             case 'D':
  75.              a[i]=13;
  76.              break;
  77.             case 'e':
  78.             case 'E':
  79.              a[i]=14;
  80.              break;
  81.             case 'f':
  82.             case 'F':
  83.              a[i]=15;
  84.              break;
  85.             default:
  86.              printf("Entered number is not Hexadecimal.Printed value is notcorrect.");
  87.              break;
  88.  
  89.            }
  90.          }
  91.          i--;
  92.          for(;i>=0;i--)
  93.          {
  94.           h=h+a[i]*m;
  95.           m=m*16;
  96.           j=j+a[i]*m;
  97.           k=(h+j)/8.5;
  98.          }
  99.  
  100.           printf("%ld ",k);
  101.         //  printf("%ld ",(h+j));
  102.  
  103.           getch();
  104.  
  105. }
  106.  
  107.  
  108.  
  109.  
  110.  
Jan 19 '12 #9
donbock
2,426 Expert 2GB
Line 26. NULL is the null pointer value. You want to loop until you find a null character in the string. The null character is '\0' (sometimes casually referred to as 0). These are different things that just happen to both be equal to zero.

You store the second hexadecimal number string into q, but you construct the number itself (j) from digits in a, which only come from string s, which holds the first hexadecimal number.

I'll say it again, lines 26-98 (except line 97) could be replaced by two calls to strtoul.

Lines 100-101. As whodgson indicated in reply #5, the %ld conversion specifier prints the value of a signed long integer as a decimal number, but the %lx conversion specifier prints the value of an unsigned long integer as a hexadecimal number. (By the way, you should use %lu to print the value of an unsigned long integer as a decimal number.)
Jan 19 '12 #10
Bil M
11
I am a little confused . Can you please correct the code and post it again if its not to much trouble. I will look at the code and understand the explanation you have provided. I would really appreciate it.
Jan 19 '12 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Ensoul Chee | last post by:
I used #include <iostream.h> int m; cout << "Hexadecimal == 0x" << hex << m << endl; to print value of m in hexadecimal mode. But I got the compile error like this couttest.cpp:20 `hex'...
4
by: James DeClerk | last post by:
Hi everyone. I'm new to C++ and I've got a seemingly tough problem to tackle. I have a union. This union needs to be converted into hexadecimal format. The hexadecimal number is then...
10
by: pavithra.eswaran | last post by:
Hi, I would like to convert a single precision hexadecimal number to floating point. The following program seems to work fine.. But I do not want to use scanf. I already have a 32 bit hexadecimal...
6
by: MrKrich | last post by:
I want to convert Hexadecimal or normal integer to Binary. Does VB.Net has function to do that? I only found Hex function that convert normal integer to Hexadecimal.
1
by: Fernando Barsoba | last post by:
Hi all, First of all, I'd like to thank you "Skarmander" and "Flash Gordon" for the help they provided me: Skarmander's algorithm and Flash's modifications helped me a lot. Here's the problem...
8
by: Vijay | last post by:
Hi , I am doing a small project in c. I have a Hexadecimal file and want to convert into ascii value. (i.e., Hexadecimal to Ascii conversion from a file). Could anyone help me? Thanks in...
7
by: elliotng.ee | last post by:
I have a text file that contains a header 32-bit binary. For example, the text file could be: %%This is the input text %%test.txt Date: Tue Dec 26 14:03:35 2006...
14
by: abhishekkarnik | last post by:
Hi, I am trying to read an exe file and print it out character by character in hexadecimal format. The file goes something like this in hexadecimal 0x4d 0x5a 0x90 0x00 0x03 .... so on When I...
6
by: Andrea | last post by:
Hi, suppose that I have a string that is an hexadecimal number, in order to print this string I have to do: void print_hex(unsigned char *bs, unsigned int n){ int i; for (i=0;i<n;i++){...
6
by: sweeet_addiction16 | last post by:
hello Im writin a code in c... can sum1 pls help me out in writing a c code to convert decimalnumber to hexadecimal number.The hexadecimal number generated has to be an unsigned long.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.