Conversion(?)

This is a discussion on Conversion(?) within the C Programming forums, part of the General Programming Boards category; I know how to convert the digit to binary but how can i convert string to binary and vice versa?...

  1. #1
    Registered User planet_abhi's Avatar
    Join Date
    Oct 2002
    Posts
    92

    Conversion(?)

    I know how to convert the digit to binary but how can i convert string to binary and vice versa?
    AbHHinaay

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    What exactly do you mean by digit and string, and for that matter, binary? Do you plan to store your binary number as a series of 48s and 49s (1s and 0s) in a string? In that case, there's no need to convert from string to binary, but maybe that's not what you want.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    Registered User planet_abhi's Avatar
    Join Date
    Oct 2002
    Posts
    92
    The binary equvalent of
    5=101 now i want the binary equivalent of manasi?
    whtaz that?
    AbHHinaay

  4. #4
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>5=101 now i want the binary equivalent of manasi?
    Like this?
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <limits.h>
    
    static char *dectobin(int dec, char buf[])
    {
      int i = CHAR_BIT - 1;
    
      memset(buf, '0', CHAR_BIT);
      buf[CHAR_BIT] = '\0';
    
      while (dec != 0 && i >= 0)
      {
        buf[i--] = (char)((dec & 1) + '0');
        dec >>= 1;
      }
    
      return buf;
    }
    
    int main(void)
    {
      int i;
      char buf[CHAR_BIT + 1];
      char str[] = "manasi";
    
      for (i = 0; str[i] != '\0'; i++)
      {
        printf("%s ", dectobin(str[i], buf));
      }
    
      printf("\n");
    
      return 0;
    }
    *Cela*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 10:31 PM
  4. Do I have a scanf problem?
    By AQWst in forum C Programming
    Replies: 2
    Last Post: 11-26-2004, 05:18 PM
  5. Creation of Menu problem
    By AQWst in forum C Programming
    Replies: 8
    Last Post: 11-24-2004, 08:44 PM

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