473,324 Members | 2,417 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,324 software developers and data experts.

Need help converting a hexidecimal to integer

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<ctype.h>
  3.  
  4. main()
  5. {
  6.  
  7. int c;
  8.  
  9.  
  10. /*Function converts string of hexadecimal digits to int */
  11.  
  12. int htoi(char s[]){
  13.  
  14. int i = 0;
  15.  
  16. while((c == getchar()) != EOF) {
  17.  
  18.     if(isdigit(c) == '0')
  19.         /*then*/
  20.         {
  21.         if(c == 'x' || c == 'X')
  22.         /*then */
  23.         {
  24.         /*then test if it's a hexidecimal*/
  25.         if(isxdigit(c))
  26.  
  27.         s[i] = c;
  28.         i++;    
  29.         }
  30.  
  31.     else if(!isdigit(c))
  32.     return c;
  33.     }
  34. printf("Integer: %d", s);
  35. }/*end of for loop */
  36.  
  37.  
  38. /*End of Function */
  39. }
  40.  
  41. htoi(c);
  42.  
  43.  
  44. /*End of htoi */
  45. }
  46.  
  47.  
Aug 15 '15 #1
6 1341
weaknessforcats
9,208 Expert Mod 8TB
Change your function to have a string argument.

So a string of ABC would process as C x 1 + B x 16 + A x 256.

You can't tell that you need A x 256 because A is the first character of the string. So you go to the end of the string and work backwards to the beginning.

Use a case statement to decode. You can have cases for A, B and C. For anything else you can subtract 49 from the ASCII char value to get the integer equivalent.

Start your multiplier at 1 and your total at zero..

The loop cycle:
1) decode character (using the case statement)
2) multiply character integer value by multiplier
3) add this to the total
4) back up one character in the string
5) multiply the multiplier by 16

repeat until your position in the string is -1. Your total will be the decoded hex number.
Aug 16 '15 #2
Cant I just use isxdigit() to test the string?
Aug 16 '15 #3
I have not used case and switch or pointers just yet. The headers I've only used are string.h,stdio.h,and ctype.h.
Aug 16 '15 #4
weaknessforcats
9,208 Expert Mod 8TB
isxdigit does not give you the integer value of the hex digit. All it does is tell you if the character is a hex digit.

As a beginner, I wouldn't worry about whether the data is valid or not. Just concentrate on converting the value of the hex string to an integer.

A case statement is just another way of writing if/else logic.

Expand|Select|Wrap|Line Numbers
  1. if (data == 'A')
  2. {
  3.      etc...
  4. }else
  5. {
  6.    if (data =='B')
  7.    {
  8.  
  9.       etc.....
  10.    )
  11.  
  12.     here data is not A or B
  13. }
is the same as writing

Expand|Select|Wrap|Line Numbers
  1. switch (data)
  2. {
  3.   case 'A'
  4.         { 
  5.           data is A
  6.         }
  7.         break;    leave the switch statement
  8.  
  9.    case 'B':
  10.         { 
  11.           data is B
  12.         }
  13.         break;    leave the switch statement
  14.  
  15.    default:
  16.  
  17.         data is not A or B
  18.  
  19. }
You shouldn't need to use explicit pointers. Your function can return the converted value.

You can use array notation in your hex string,
like data[i] and avoid pointer notation.
Aug 16 '15 #5
Well I know I wasnt specific but the exercise for chapter does not use use switch and case yet. So id like do it without case.
Aug 16 '15 #6
weaknessforcats
9,208 Expert Mod 8TB
Just write a nested if/else statement.
Aug 16 '15 #7

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

Similar topics

2
by: Ken Fettig | last post by:
I have a quick simple question. How do you convert an integer to a string in Python? Thanks Ken Fettig
6
by: Newbee Adam | last post by:
I have been reading in help how I need to use decimal becuase currency does not exist like I used in vb6. I had a difficult time on google and msdn finding how or if I can take the value of text...
15
by: jaks.maths | last post by:
How to convert negative integer to hexadecimal or octal number? Ex: -568 What is the equivalent hexadecimal and octal number??
3
by: news.microsoft.com | last post by:
Hello, I'm a VB.NET guy converting a C# app to VB.NET and ran into an issue. Steping through both the C# and VB.NET apps i get identical results but for one section. Notes: bit_buffer = 360...
9
by: hapa | last post by:
I have a problem in this C++ program That i have written. help me with the problem that i have mentioned below I have written this program to convert a integer to binary digit problem one is that...
1
by: sake | last post by:
This problem seems to have a fairly simple solution in C, and google seems to love to pull up solutions for C. However, C++ just seems have the exact opposite luck. So pretty much, I want to...
7
by: tragic54 | last post by:
Alright so i've done this in some of my recent programs with option strict on and for some reason When i debug and select the option from the combo box, it crashes and gives me a 'Input String was...
4
by: geeta719 | last post by:
void main() { char ch=""; char c; int k=0,l,i; printf("Enter a number:"); scanf("%d",&i); while(i!=0) //reversing a numbr { k=k*10+i%10; i=i/10;}
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.