Thread: itoa in C++

  1. #1
    Registered User Dave++'s Avatar
    Join Date
    Jun 2007
    Location
    Where the Buffalo Roam
    Posts
    40
    What is the best way to implement IOTA in CPP ? Using CPP Standards?

    Given the following...I'd like to return a character version of a library call.
    Code:
    std::string afunctionthatcallsanunchangableLib(){
         returnstring = convertitsimple(somelibraryfunc());
         return(returnstring);
    }
    Thanks,
    Dave

    <<split from https://cboard.cprogramming.com/showthread.php?t=86470>>
    Last edited by Dave_Sinkula; 06-17-2007 at 03:53 PM. Reason: better explaination <<split thread>>

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User Dave++'s Avatar
    Join Date
    Jun 2007
    Location
    Where the Buffalo Roam
    Posts
    40
    Also,
    This code...could it be made to work with integers easily?
    I am trying to go from an integer to binary to a string.

    http://faq.cprogramming.com/cgi-bin/...&id=1044780608

    Regards,
    Dave

    to Sinkula:
    thanks for the link...can you help with a better understanding of the overloading operator?
    Code:
    inline double convertToDouble(const std::string& s)
     {
       std::istringstream i(s);
       double x;
       if (!(i >> x))
         throw BadConversion("convertToDouble(\"" + s + "\")");
       return x;
     }
    and in
    Code:
    template <typename T>
    T rev_bits ( T val )
    {
      T ret = 0;
      unsigned int n_bits = sizeof ( val ) * CHAR_BIT;
    
      for ( unsigned i = 0; i < n_bits; ++i ) {
        ret = ( ret << 1 ) | ( val & 1 );
        val >>= 1;
      }
    
      return ret;
    }
    Last edited by Dave++; 06-17-2007 at 04:30 PM. Reason: added examples

  4. #4
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    If you want to get a the binary value of an integer, as a string:
    Code:
    #include <stdio.h>
    #define size 100
    
    void DecToBin(int num, char *str)
    {
        short num_bits = 32;    
        short start=0; 
        short i, bit;
        
        for(i=num_bits-1; i>=0; i--)
        {
            bit=(num>>i) & 1;   
            if(start == 0) if(bit != 0) start=1;       
            if(start != 0)
            {    
                str[start-1]=bit+'0';
                if((i%8) == 0)
                {    
                     str[start]=' ';
                     start++;
                }
                start++;
            }  
        }  
        str[start-1]='\0';          
    }      
    
    int main()
    {
        int num;
        char binstring[size];
        printf("Enter a whole number: ");
        scanf("%i", &num);
        DecToBin(num, binstring);
        printf("%s", binstring);      
        getchar(), getchar();
    }

  5. #5
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    You could always use a bitset, too, but that might be overkill.
    Last edited by CodeMonkey; 06-18-2007 at 10:26 PM. Reason: wrong class
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> If you want to get a the binary value of an integer, as a string:
    Use std::bitset rather than coding a long implementation yourself.

    >> can you help with a better understanding of the overloading operator?
    Which overloaded operator?

    Also note that if you have boost, then lexical_cast is preferable to your own version (like the example from the FAQ Dave_Sinkula linked to).

  7. #7
    Registered User Dave++'s Avatar
    Join Date
    Jun 2007
    Location
    Where the Buffalo Roam
    Posts
    40
    Mike,
    Thanks for the code. I ran it on my end with no problems and it will integrate well into my application.

    dave.thanks(10^6);
    PS: found an error...won't work for zero
    So added this at the very begining (other mods were made to handle the early return)
    Code:
      if(num == 0){
         str[0]='0';
         str[1]='\0';
         return(str);
      }
    Daved,
    > Which overloaded operator?
    I don't know what this is saying.
    val >>= 1;
    Last edited by Dave++; 06-18-2007 at 07:38 PM. Reason: updates and learning

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It left-shifts val by one 1 bit and assigns the result to val (as any operator combined with = does). In mathematical terms that would be the equivalent of dividing by 2 (for unsigned integers).

    As to overloading is concerned: you have probably met the >> operator with cin. Well, there it has been overloaded to be an insertion operator, but originally it is a bitwise operator.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Right shift, not left shift.

    In addition, there are two different types of shifts:

    1. Logical
    2. Arithmetic


    Logical is what when you want the entire number to be shifted over and treated as an unsigned number. Arithmetic is used when you want to keep the sign bit in place and preserve the overall "signess" or the number. The x86 processor has two instructions for each kind of shift, obviously one left and one right.

    In C and C++, the decision to shift logically vs arithmetically is determined by whether or not the number to be shifted is signed or unsigned. Down at the assembly and hardware level, there is no such thing as signed and unsigned numbers except what the programmer does to interpret and use them as such.
    Last edited by MacGyver; 06-19-2007 at 01:18 AM.

  10. #10
    Registered User Dave++'s Avatar
    Join Date
    Jun 2007
    Location
    Where the Buffalo Roam
    Posts
    40
    > but originally it is a bitwise operator

    Thanks to all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem using itoa
    By g_p in forum C Programming
    Replies: 2
    Last Post: 05-03-2008, 06:38 AM
  2. Problem with itoa() perhaps?
    By TheSquid in forum C++ Programming
    Replies: 5
    Last Post: 05-08-2006, 02:04 AM
  3. Really Weird itoa Problem
    By Grantyt3 in forum C++ Programming
    Replies: 8
    Last Post: 12-20-2005, 12:44 AM
  4. itoa
    By coldcoyote in forum Linux Programming
    Replies: 4
    Last Post: 02-13-2003, 09:28 AM
  5. itoa
    By Thantos in forum C Programming
    Replies: 2
    Last Post: 09-18-2001, 02:23 PM