Thread: Question about strings in c

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    60

    Question about strings in c

    Hey,
    this is my code
    Code:
    for (k = 1; k < 15; k++) {		/*convert to binary*/
                putchar(number & displayMask ? '1' : '0');
                number <<= 1;
    }
    where number is an integer.
    now this would display the binary form of number to stdout.
    How would I assign that value to a string instead of displaying it?

    thanks

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What do you think? No, I'm not asking to be faecetious, but rather because if you think about how you'd do it, you learn something more than "how to copy text from a post on a forum" - something I'm sure you are already capable of doing.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I would have modified your code and just given you the answer had your code been correct to begin with.

  4. #4
    Hacker MeTh0Dz's Avatar
    Join Date
    Oct 2008
    Posts
    111
    You reap the benefits of my boredom...

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    void Parse(int numb, char ** string);
    
    int main()
    {
    	char * string = (char*)malloc(sizeof(char) * 50);
    	if (!string) return 0;
    	
    	int number = 9784;
    	
    	Parse(number, &string);
    	printf("&#37;s\n", string);
    	
    	free (string);
    	return 0;
    }
    
    void Parse(int numb, char ** string)
    {
    	int i;
    	int j = sizeof(numb) * 8;
    	for (i = 0; i < sizeof(numb) * 8; i++, j--)
    		(*string)[i] = (char)(((numb >> (j - 1)) & 1) | 0x30);
    	(*string)[i] = '\0';
    	printf("%d\n", i);
    	return;
    }

  5. #5
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Quote Originally Posted by MeTh0Dz View Post
    You reap the benefits of my boredom...
    Lol, hey if ur bored prob ud wanna check my assignment i complete against the requirements and see how much marks ud give me if u were the prof. Its working, not asking for fixing anything, but for a grading mark
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


  6. #6
    Hacker MeTh0Dz's Avatar
    Join Date
    Oct 2008
    Posts
    111
    What is your assignment?

  7. #7
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Quote Originally Posted by MeTh0Dz View Post
    What is your assignment?
    Deals with circular linked lists, simple database
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


  8. #8
    Hacker MeTh0Dz's Avatar
    Join Date
    Oct 2008
    Posts
    111
    Oh cool. I'm assuming it's a university assignment?

    If it's up somewhere I'll take a look at it, see if I notice anything blatant.

  9. #9
    POeT GuY Matus's Avatar
    Join Date
    Feb 2008
    Location
    Bz
    Posts
    235
    Quote Originally Posted by MeTh0Dz View Post
    Oh cool. I'm assuming it's a university assignment?

    If it's up somewhere I'll take a look at it, see if I notice anything blatant.
    Hehe, not at U level yet, umm junior college. almost there, was gona pm it or mail if its ok. i dont have it posted anywhere, got some classemates who arent done yet.
    PoEms R InsPiRatiOns of LIfE ExpErienCes!!


  10. #10
    Hacker MeTh0Dz's Avatar
    Join Date
    Oct 2008
    Posts
    111
    Alright well...
    Just PM it.

    I'll take a look when I get a chance (probably now lol).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question About Strings
    By spanker in forum C++ Programming
    Replies: 1
    Last Post: 07-13-2008, 05:09 AM
  2. strings question
    By cstudent in forum C Programming
    Replies: 4
    Last Post: 04-18-2008, 07:28 AM
  3. Functions and Strings Question
    By StrikeMech in forum C Programming
    Replies: 4
    Last Post: 07-18-2007, 06:07 AM
  4. Strings question
    By kimimaro in forum C Programming
    Replies: 10
    Last Post: 03-15-2005, 12:14 AM
  5. question about arrays of strings
    By lakai02 in forum C Programming
    Replies: 14
    Last Post: 12-25-2002, 09:11 PM