Store combined data types into string?

This is a discussion on Store combined data types into string? within the C Programming forums, part of the General Programming Boards category; I want to parse two variables, an integer and a character into a string (e.g. 20d, 1e, 72a, etc.). How ...

  1. #1
    843
    843 is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    135

    Store combined data types into string?

    I want to parse two variables, an integer and a character into a string (e.g. 20d, 1e, 72a, etc.). How should I do this?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,409
    That depends on what is the exact format. Is there really only supposed to be one character at the end? If so, you can access that last character and store it in a char variable, then replace that character with a null character, then use strtol (or more simply but with no error checking, atoi) to convert the string into an integer.
    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
    843
    843 is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    To be exact, I want to store the string in a variable, as such:

    Code:
    str = num, ch;
    I'm not sure if that syntax is correct, but I can't experiment if the code can't be compiled.

    I have got the values of num and ch in their respective data types, but I can't figure out how to parse them together.

    I would like to convert them into a string, not the other way around, so atoi wouldn't work.
    Last edited by 843; 12-01-2010 at 10:50 AM.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    Parsing is usually the reverse process. Taking a string like "20d" and turning it into an integer (20) and a character ('d'). If you already have the separate variables then you can just use sprintf() to join them in a string. sprintf(stringbuffer, "%d%c", myint, mychar);
    If you understand what you're doing, you're not learning anything.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,409
    Oh, what I did not say is that you should first read into a string, and then parse that string as I described (assuming I guessed the format correctly).

    EDIT:
    Oh wait, this did not register:
    Quote Originally Posted by 843
    I would like to convert them into a string, not the other way around, so atoi wouldn't work.
    Yeah, itsme86 is right. You want to create a string from them, not parse a string.
    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
    843
    843 is offline
    Registered User
    Join Date
    Oct 2010
    Posts
    135
    My bad for using the wrong term. Thanks for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polymorphism and generic lists
    By Shibby3 in forum C# Programming
    Replies: 9
    Last Post: 07-26-2010, 05:27 AM
  2. Memory Leak in AppWizard-Generated Code
    By jrohde in forum Windows Programming
    Replies: 4
    Last Post: 05-19-2010, 04:24 PM
  3. store string data as a short or other data type
    By robin2aj in forum C Programming
    Replies: 5
    Last Post: 04-07-2010, 11:02 AM
  4. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM

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