Thread: decode isbn help

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    60

    decode isbn help

    Hi guys I am trying to decode an isbn. My function looks like this:int decode
    (FILE* fp, const char str[], char area[], char publisher[], char title[]); This function gets a file pointer that contains a prefix table, a 10 digit isbn in str and area[], publisher[] and title[] are empty but if this function is successful it will break the isbn up into pieces were those 3 string will be filled and return 1, if not 0.


    So basically how I was attempting this problem was by using sscanf to convert the string to a double, then to an into to break the numbers up. So I am able to isolate the numbers individually but I want to be able to break them up in smaller digits.

    For example ISBN= 9070002043

    I want to be able to check is the area is 9, 90, 907,9070 or 90700. If none of those are valid then I know its an invalid ISBN. If one of those work for example the 90. Know I want to get if 7,70,700 is in the range of the publisher etc..


    I am not sure If I approached this program the totally wrong way so if any sees an easier way and could point me in the right direction it would really be great.

    For reference some of the file looks like this. The first digit is the area code, the second and third is the range that the publisher is between.

    88 00 19
    88 200 599
    88 6000 8499
    88 85000 89999
    88 900000 999999



    Code:
                    firstdigit=tempstring;
    		
    		ifirstdigit=(int)firstdigit%10; 
                    
                    
    		seconddigit=(value/10); //digit 9
    		
    		
    		iseconddigit=(int)seconddigit%10;   
    		     
    
    		thirddigit=(value/100); //digit 8
    		
                   
    		ithirddigit=(int)thirddigit%10;	
    		  	  	     
    
    		fourthdigit=(value/1000); // digit 7
    		
    		
                    ifourthdigit=(int)fourthdigit%10;        
    
    		fifthdigit=(value/10000); // digit 6
    		
    		
                    ififthdigit=(int)fifthdigit%10;      
    	 			
    		sixthdigit=(value/100000); // digit 5
    		
    		
    		isixthdigit=(int)sixthdigit%10;        
    				
    		seventhdigit=(value/1000000); // digit 4
    		
                    iseventhdigit=(int)seventhdigit%10;        
    				
    		eighthdigit=(value/10000000); // digit 3
    		
                    ieighthdigit=(int)eighthdigit%10;        
    				
    		ninethdigit=(value/100000000); // digit 2
    		
    		ininethdigit=(int)ninethdigit%10;
    				
    		tenthdigit=(value/1000000000); //digit 1
    		
    		itenthdigit=(int)tenthdigit%10;

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why don't you just subtract '0' from each character in the array as you need it?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    60
    Quote Originally Posted by quzah View Post
    Why don't you just subtract '0' from each character in the array as you need it?


    Quzah.

    so str[i]-'0'?

    What would that do. I am sorry I am new to this and not really sure what your getting at.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Thegame16 View Post
    so str[i]-'0'?

    What would that do. I am sorry I am new to this and not really sure what your getting at.
    I'd take a step back and look at it anew. You have an ISBN number or string. First thing, is make variables for the significant parts of it: zip, pubNum, etc. I would not make a variable for every digit, however.

    Then set up your logic using strings, first. Then change the string parts, into numbers, if it helps. A character digit, when subtracted by the char '0', becomes the char equivalent of an integer:

    '9' - '0' becomes 9 (a TAB on ASCII). If you use %d format for it, it will print up a 9. If you use a %c format in printf(), it will try to tab over your cursor. A nine in ASCII is 57, IIRC. If it's all confusing, download or consult an ascii table. (Handy when programming, anyway.)

    If you work with this problem a bit more by hand, you'll find a simpler way to do this, on the computer - us humans are lazy! Here, you can use that to your advantage.

    Get to know the problem better, and you'll see a simpler answer that you like.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    60
    Quote Originally Posted by Adak View Post
    I'd take a step back and look at it anew. You have an ISBN number or string. First thing, is make variables for the significant parts of it: zip, pubNum, etc. I would not make a variable for every digit, however.

    Then set up your logic using strings, first. Then change the string parts, into numbers, if it helps. A character digit, when subtracted by the char '0', becomes the char equivalent of an integer:

    '9' - '0' becomes 9 (a TAB on ASCII). If you use %d format for it, it will print up a 9. If you use a %c format in printf(), it will try to tab over your cursor. A nine in ASCII is 57, IIRC. If it's all confusing, download or consult an ascii table. (Handy when programming, anyway.)

    If you work with this problem a bit more by hand, you'll find a simpler way to do this, on the computer - us humans are lazy! Here, you can use that to your advantage.

    Get to know the problem better, and you'll see a simpler answer that you like.

    thanks for your suggestion. I just have a problem when I convert with the -'0' its not printing properly. For example if the ISBN "9070002043".

    I would want to check if the area is 9. The the publisher could be 0,07,070,0700,07000,070002 but when i convert i only get 0,7,700,7000,70002. Is there any way to fix this.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    60
    ok i still wasnt able to figure out the leading 0 problem but other than that I think im ok.

    I just have one more problem. I have now figured out the pieces that will go in the area, the publisher and the title but I have these pieces in an int variable when i did for example
    area1=str[i]-'0'
    Now I want to take that piece and put it in my char array, how do i do that?

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Thegame16 View Post
    ok i still wasnt able to figure out the leading 0 problem but other than that I think im ok.

    I just have one more problem. I have now figured out the pieces that will go in the area, the publisher and the title but I have these pieces in an int variable when i did for example
    area1=str[i]-'0'
    Now I want to take that piece and put it in my char array, how do i do that?
    The first problem (the leading 0 being dropped), means that you need to handle that bit of data, as a string, rather than just converting it to an int, right away.

    perhaps itoa(): (from Borland's help file)

    itoa Converts an integer to a string.


    Syntax:
    char *itoa(int value, char *string, int radix);

    Prototype in:
    stdlib.h

    Remarks:
    itoa converts value to a null-terminated
    string and stores the result in string. With
    itoa, value is an integer.

    radix specifies the base to be used in
    converting value. radix must be between 2 and
    36, inclusive.

    If value is negative and radix is 10, the
    first character of string is the minus sign
    (-).

    The space allocated for string must be large
    enough to hold the returned string, including
    the terminating null character (\0). itoa can
    return up to 17 bytes.

    Return Value:
    Returns a pointer to the target string.

    Portability:
    itoa is unique to DOS (works in text console on Windows)

    See Also:
    ltoa ultoa

    Example:
    Code:
     #include <stdlib.h>
     #include <stdio.h>
    
     int main(void)
     {
        int number = 12345;
        char string[25];
    
        itoa(number, string, 10);
        printf("integer = %d string = %s\n", number, string);
        return 0;
     }

  8. #8
    Registered User
    Join Date
    Oct 2010
    Posts
    60
    Quote Originally Posted by Adak View Post
    The first problem (the leading 0 being dropped), means that you need to handle that bit of data, as a string, rather than just converting it to an int, right away.

    perhaps itoa(): (from Borland's help file)

    itoa Converts an integer to a string.


    Syntax:
    char *itoa(int value, char *string, int radix);

    Prototype in:
    stdlib.h

    Remarks:
    itoa converts value to a null-terminated
    string and stores the result in string. With
    itoa, value is an integer.

    radix specifies the base to be used in
    converting value. radix must be between 2 and
    36, inclusive.

    If value is negative and radix is 10, the
    first character of string is the minus sign
    (-).

    The space allocated for string must be large
    enough to hold the returned string, including
    the terminating null character (\0). itoa can
    return up to 17 bytes.

    Return Value:
    Returns a pointer to the target string.

    Portability:
    itoa is unique to DOS (works in text console on Windows)

    See Also:
    ltoa ultoa

    Example:
    Code:
     #include <stdlib.h>
     #include <stdio.h>
    
     int main(void)
     {
        int number = 12345;
        char string[25];
    
        itoa(number, string, 10);
        printf("integer = %d string = %s\n", number, string);
        return 0;
     }
    Thanks for the information about this function but I am running the program on a linux machine using cstdlib and it doesnt appear to work. Is there any other way to deal with the leading 0 and also copy my int value into a char array?

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Thegame16 View Post
    Thanks for the information about this function but I am running the program on a linux machine using cstdlib and it doesnt appear to work. Is there any other way to deal with the leading 0 and also copy my int value into a char array?
    Linux - has it's own function for this, but I don't know the name. Check your man pages or Google that one.

    The way to deal with the leading 0 being dropped is to handle it FIRST as a string (take it in as a string and deal with it, before you change anything into a number.

    Code:
    if(myString[0]=='0')
       //handle it here
    else
      //go to your number stuff here
    You can write your own version of it, of course.
    while(number > 0) { is a good start, then:

    ....Use var = number % 10 to get the right most digit on the number, into var.

    ....Then number /= 10, to reduce the number by 10 (cutting off the right hand digit you just got from using the % operator.
    end of while

    put your var into the string by first, adding it with it's char offset: digit + '0', puts any digit into it's correct ascii value, for a string. Note the single quotes around the zero.

    The awkward thing is, you have to work from right to left doing this, so you need to allow plenty of room, so you don't run off the end of the string array on the left side.

    Then close the digits down, shuffling them to the leftmost (lowest) position in the array(viewing it from left to right on the screen). It's a bit of a pain, but isn't it usually that way when you "roll your own"?

    I'll post up some digit shuffle code when I return.

  10. #10
    Registered User
    Join Date
    Oct 2010
    Posts
    60
    Quote Originally Posted by Adak View Post
    Linux - has it's own function for this, but I don't know the name. Check your man pages or Google that one.

    The way to deal with the leading 0 being dropped is to handle it FIRST as a string (take it in as a string and deal with it, before you change anything into a number.

    Code:
    if(myString[0]=='0')
       //handle it here
    else
      //go to your number stuff here
    You can write your own version of it, of course.
    while(number > 0) { is a good start, then:

    ....Use var = number % 10 to get the right most digit on the number, into var.

    ....Then number /= 10, to reduce the number by 10 (cutting off the right hand digit you just got from using the % operator.
    end of while

    put your var into the string by first, adding it with it's char offset: digit + '0', puts any digit into it's correct ascii value, for a string. Note the single quotes around the zero.

    The awkward thing is, you have to work from right to left doing this, so you need to allow plenty of room, so you don't run off the end of the string array on the left side.

    Then close the digits down, shuffling them to the leftmost (lowest) position in the array(viewing it from left to right on the screen). It's a bit of a pain, but isn't it usually that way when you "roll your own"?

    I'll post up some digit shuffle code when I return.

    thanks so much for all of your help. I will just briefly describe what I want to do. So i have the char str which has my ten digit isbn. I made and int variable for each of the test pieces like below. Now I want to take that number checkarea1 and put it in my char area array. So you are saying do something like checkarea1=checkarea1+'0' to get it back to ascii and then put it back in.

    Anyways I will wait to see the digit shuffle code when you return. And thanks again.

    Code:
    checkarea1 = checkarea1 * 10 + (str[1] - '0'); // i have multiple variables with the different pieces
    
    //check area 1 would be for example 972 and i want that to go in my char area[] array.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Meet Mr. Shuffles

    Code:
    /* peels off the digits from a number, and puts them into a char array,
       shuffling them over to the low index, as needed.
    */
    
    #include <stdio.h>
    #define SIZE 14
    
    int main() {
      int i, j, n=12345;
      char s[SIZE]={""};
    
      printf("\n\n");
    
      /* peel off the digits of n, and put them into s2[] */
      i=SIZE-1;
      s[i]='\0';
      while(n>0) {
        j = n%10;
        n /= 10;
        s[--i -1]=j+'0';
      }
      printf("\nS=");
      for(i=0;i<SIZE;i++)
        putch(s[i]);
    
      /* shuffle the digits over to the left */
      i=0; 
      while(s[i++] < '0'); //get the offset to shuffle over
      --i; 
      for(j=0;j<SIZE;j++)
        s[j] = s[j+i];
    
      printf("\nS=%s\n", s);
      printf("\n\n\t\t\t     press enter when ready");
      (void) getchar(); 
      return 0;
    }
    Last edited by Adak; 02-13-2011 at 01:41 AM.

  12. #12
    Registered User
    Join Date
    Oct 2010
    Posts
    60
    thanks so much for all your help.

    I just have 1 more question with the leading 0's. When Im checking my sin for example 0003194876 and how I would want to check it would be putting this string into int variables, so I would check the area and see if it's 0 and that would work. But when I went to check 00 it would just show up as 0. and if I wanted to check 0003 it would just show up as 3. Im just not sure how I would no to change it and put it into a string and still check it against an int that I am scanning in the file. And also not all the numbers are like this so some might work perfectly fine.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You have a string of digits. √

    You need to test these digits, as a string first √√

    and THEN check them as a number √√√

    The number could be discarded or unused, if the string tests show that it's really a case that must be tested only as a string.

    And you're welcome, of course.

  14. #14
    Registered User
    Join Date
    Oct 2010
    Posts
    60
    Quote Originally Posted by Adak View Post
    You have a string of digits. √

    You need to test these digits, as a string first √√

    and THEN check them as a number √√√

    The number could be discarded or unused, if the string tests show that it's really a case that must be tested only as a string.

    And you're welcome, of course.
    i did something like my code below. As you can see some will be wrong like checkarea2,3 and 4. Now I was thinking if could do a count on the incorrect 1's to check if there incorrect and if they were I could put the leading 0's in a temp int array. But if I do that I dont know how I could check it would my scanned in number. For example if I scanned my my file and that area was 000 how would I compare my filled char string with that number. I could only do it digit by digits not all 3 0's.
    Is there any way to get around this?



    Code:
    str[40]="0003194876";
    //now im converting to int
    checkarea1 = checkarea1 * 10 + (str[0] - '0');//gives me 0 which is correct
    checkarea2= checkarea1* 10 + (str[1] - '0'); //gives me 0, should be 00
    checkarea3 =  checkarea2* 10 + (str[2] - '0');// gives me 0, should be 000
    //next would be 0003 but gives me 3 etc...
    Last edited by Thegame16; 02-14-2011 at 04:24 PM.

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Normally, my advice is to work on it on paper, and then repeat it until you can recognize the most basic steps of logic that YOU use to do the job, and then write them down, step by step. Always remembering how stupid silicone can be.

    That is the backbone of your logic then, for the function or program.

    But in this case...


    Post up 3-10 of the ISBN's that are causing you the difficulty, and what you want each one to break down into, for your program.

    Please don't refer me to post #1, because I can't find a bit of reason in it, before my eyes glaze over, or my head start shaking side to side.

    Input and output, for the troublesome one's. Short and sweet.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My Incomplete Book list
    By abachler in forum General Discussions
    Replies: 2
    Last Post: 11-01-2009, 09:28 AM
  2. need help reading a file (isbn program)
    By bigmac(rexdale) in forum C Programming
    Replies: 2
    Last Post: 05-16-2008, 10:56 PM
  3. isbn number.
    By xxbleh in forum C Programming
    Replies: 4
    Last Post: 11-12-2007, 06:44 AM
  4. Processing a book ISBN
    By richdb in forum C Programming
    Replies: 13
    Last Post: 08-11-2006, 11:14 PM
  5. validate isbn
    By sal817 in forum C Programming
    Replies: 14
    Last Post: 02-27-2006, 09:39 AM