Thread: Help with loop to convert string to int

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    33

    Help with loop to convert string to int

    trying to a string variable named hexnumber to convert each character (a through f ) to corresponding integer (10-15):
    Code:
    int input[100];//array of ints to store user's input digits
    	int i;
    	char j='a',k='A';
    	int u=10;//to convert a to 10 and increment when incrementing letters
    	//converting lowercase letters their corresponding integer of type int
    	while(j<='f')
    	{
    		i=0;
    	for(i=0;i<=(hexnumber.size()-2);i++)
    	{ 
    		if(hexnumber[2+i]==j||hexnumber[2+i]==k)
    		{input[i]=u;}
    		
    	   i++;
    	}
    	   u++;
    	   j++;
    	   k++;
    
    	}
    	
    	cout<<input[0]<<input[1];

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    This is covered on the FAQ.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    33
    But I'm not allowed to use any of that stuff (this is for school).

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Here is a hint:

    Code:
    char input[] = "af";
    
    toupper(input[0])-'A' = 0
    toupper(input[1])-'A' = 5

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    33
    Thanks. I think the method that I used also works ( haven't checked it completly, though). I just noticed that there is an unecessary incrementation of the variable i in the for loop. The outupt is coming out like I expect it too now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 05:34 PM
  2. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  3. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  4. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM