Thread: Help with data format

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    76

    Help with data format

    I have an int that has dashes in. It has to remain int and I cant use any arrays. For example, I need 34-2345-21 to be 342345-21. Any help on a way to achieve this?

  2. #2
    Registered User Sake's Avatar
    Join Date
    Jan 2005
    Posts
    89
    Can you be more specific about how you're going to use it?
    Kampai!

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    76
    Ooops, I did that wrong. I meant 34234521. Its a product number that I am storing in a structure. It has be an int.

  4. #4
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    The only way i can think of is to store the number in a character array , use strtok() to get rid of the dashes and finally strtol() to convert it to a long number.

    But you said you can't use arrays...
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  5. #5
    Registered User Sake's Avatar
    Join Date
    Jan 2005
    Posts
    89
    >>Its a product number that I am storing in a structure. It has be an int.
    Do you have to put the dashes back at any point? Are product numbers consistent in the placement of dashes? Removing the dashes is pretty simple, but if need to put them back then you have to have a way to determine where they were to begin with.
    Kampai!

  6. #6
    Registered User
    Join Date
    Oct 2004
    Posts
    76
    Yes, the dashes must be placed back. Also the product number is the same all the time, 2 digits, dash, 4 digits, dash, 2 digits.

  7. #7
    Registered User Sake's Avatar
    Join Date
    Jan 2005
    Posts
    89
    Something like this?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    long int pctoi(const char *pc)
    {
      long int ret = 0;
      char *end;
    
      ret = strtol(pc, &end, 0);
      ret = (ret * 10000) + strtol(end + 1, &end, 0);
      ret = (ret * 100) + strtol(end + 1, &end, 0);
    
      return ret;
    }
    
    void printpc(long int pc)
    {
      printf("%2d-%4d-%2d\n", pc / 1000000, pc / 10000, pc % 100);
    }
    
    int main(void)
    {
      const char *from = "34-2345-21";
      long int to = 34234521;
    
      printf("%ld\n", pctoi(from));
      printpc(to);
    
      return 0;
    }
    Kampai!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about a working linked list
    By cold_dog in forum C++ Programming
    Replies: 23
    Last Post: 09-13-2006, 01:00 AM
  2. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  3. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  4. C diamonds and perls :°)
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 05-16-2003, 10:19 PM
  5. C Programming Question
    By TK in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 07-04-2002, 07:11 PM