Like Tree3Likes
  • 1 Post By laserlight
  • 1 Post By memcpy
  • 1 Post By laserlight

Conversion of string to integer array?

This is a discussion on Conversion of string to integer array? within the C Programming forums, part of the General Programming Boards category; Hi. I'm trying to make a program that reads an input of a barcode number, then validates the "check digit" ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    14

    Conversion of string to integer array?

    Hi.
    I'm trying to make a program that reads an input of a barcode number, then validates the "check digit" (the last digit) with an algorithm. I am stuck at the start. In order for the algorithm to work, the barcode must be in separate digits, not a 'number' so to speak.

    I need the input to be a char string because it may start with '0', which wouldn't count if it were an integer.

    Code:
    #include <stdio.h>#include <string.h>
    #include <ctype.h>
    
    
    #define SIZE 12
    
    
    int main() {
        char str[SIZE];
        int digit[SIZE];
    
        int i;
    
    
        
        fgets(str, 14, stdin);
    
    
        
        for(i = 0; i < 12; i++){
              str[i] = digit[i];
              }
              
        system("pause");
        return 0;
    
    
    }
    I know this is completely wrong, but it is all I have so far. I'm not worried about the algorithm or output, just the conversion of the string of numbers into an integer array.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,593
    The idea here is that you already have the individual digits, except that they encoded as characters of a character set rather than having the desired values in the range [0, 9]. The solution is to subtract '0' from each character that represents a digit, which works because it is guaranteed that the characters '0' to '9' will be contiguous and in order in the character set.
    jigamuffin likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Remove "system("pause")". No idea why this is so popular, but read this > SourceForge.net: Pause console - cpwiki

    Also, if anything, you have the order wrong:
    Code:
    str[i] = digit[i]
    This will be filling the string with whatever's in digit's memory. Most likely it won't be useful data.

    And even if you are trying to make your own "strtoul" function (hint hint), you need to actually convert them into digits:
    Code:
    str[i] - '0'
    Put this in the right place, and it will subtract the ascii number 0 from the character, turning it into the actual numeral you want.

    Also note that you should probably do some kind of error checking to make sure you're actually converting a digit.
    jigamuffin likes this.

  4. #4
    Registered User
    Join Date
    Mar 2012
    Posts
    14
    Thank you both. From what I gathered from you two, something like this should convert the digits.

    Code:
    #include <stdio.h>#include <string.h>
    #include <stdlib.h>
    
    
    #define SIZE 12
    
    
    int main() {
        char str[SIZE];
        int digit[SIZE];
        int i;
    
    
        
        fgets(str, 14, stdin);
    
    
        for(i=0; i<SIZE; i++){
                 str[i] - '0';
                 }
        
        for(i = 0; i < 12; i++){
              digit[i] = str[i];
              }
    
    
        
        
        printf("%d, %d, %d", digit[1],digit[2],digit[3]);
      
              
        getch();
        return 0;
    
    
    }
    the final printf outputs something like 50, 51, 52 every time?

    I think I have a misunderstanding of strings, or atleast unknown strings which are obtained from input. I thought say,
    Code:
    char str[81];    
            printf("Enter a string: "); /*user enters "hello"*/
            fgets(str, 81, stdin);
            
            printf("%s", str[2]); /*output would be "l"*/
    this would output "l" but it does not. what have I missed here?
    Last edited by jigamuffin; 03-19-2012 at 10:10 PM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,593
    This computes the digit's value, but you don't assign the result to anything:
    Code:
    str[i] - '0';
    jigamuffin likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,657
    @jigamuffin: A string can be further divided into characters, so the type of string foo and foo[i] are different. foo[i] is a character.

  7. #7
    Registered User
    Join Date
    Mar 2012
    Posts
    14
    Quote Originally Posted by laserlight View Post
    This computes the digit's value, but you don't assign the result to anything:
    Code:
    str[i] - '0';
    Haha I see now. I feel silly.

    You have helped me with several of my problems laserlight. I greatly appreciate your help. Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Byte Array / Integer conversion
    By AdLab in forum C Programming
    Replies: 2
    Last Post: 05-07-2008, 08:04 AM
  2. Hex String to byte array conversion
    By IfYouSaySo in forum C# Programming
    Replies: 3
    Last Post: 06-15-2006, 04:59 PM
  3. Replies: 5
    Last Post: 04-12-2006, 06:30 PM
  4. Replies: 2
    Last Post: 11-02-2003, 07:34 AM
  5. Conversion of character string to integer
    By supaben34 in forum C++ Programming
    Replies: 3
    Last Post: 10-30-2003, 03:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21