Thread: having a problem with converting

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    3

    having a problem with converting

    I have this function in a file:
    Code:
    int fill(char *msg, int size){
    char c[50];
    
    //need to populate c here with msg
    
    }
    Now i want to call "fill" passing msg and the size of the message as paramenters. However the message will not always be made up of characters... That is the function can be called for example this way:

    Code:
    int msg = 9999;
    fill((char *)msg,4);
    I managed to do the code for fill function whenever it is passed only strings which is by using memcpy... however i cant make it work for both cases :s

    Thanks in advance

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    So you're saying you want to fill c with the string "9999"? You can't do it this way at all. You'll have to do something like:
    Code:
    int num = 9999;
    char msg[50];
    
    sprintf(msg, "%d", num);
    fill(msg, strlen(msg));
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    12
    but can you recover back the integer afterwards ?
    by reversing it

  4. #4
    Registered User
    Join Date
    Mar 2007
    Posts
    12
    I think his actual problem is that he wants to convert any data type to char is it so Mr ?

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    3
    yeah thats something like it... however i prefer casting the way I asked for and then manipulating between "char to char" or "int to char" inside the function itself....

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    "casting" is not the same as "converting". The byte structure for the integer 9999 is way different than the string "9999".
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User
    Join Date
    Mar 2007
    Posts
    12
    itsme86 he wants to access the integer as an array of chars but he doesnt care of the data he needs it for transmission purposes

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    3
    ok i understand maybe i'm using the incorrect terms in here... however is there a way to call the function as I asked in the first post (or something similar)? and then saving 9999 into c? by for example traversing msg character by character and copying it to c[i]?

    something that will look similar to:

    Code:
     for(i=0;i<size;i++){
        c[i] = msg[i];
     }

    Thanks

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    When you first asked this question it sounded like you wanted a function which fills an area of memory with any data, bit by bit. And the very same function should work like strcpy() too. Most people feel comfortable keeping these as two separate routines.

    C is not type strict, but isn't type neutral either; more like somewhere in the middle, which makes C gurus happy. What I gather is that you want a string to contain the exact same bit pattern as any number, like 9999. While, even if you did do something like this, the data will not be interpreted the same way. Remember that a pointer aligns the memory it points to for a specific expected type: I wager that you would end up with something not human-readable and useless(?). If you wanted a string "9999" from the number 9999, that's fairly simple.

    Please be sure that you really know what you are asking.

    In the meantime, work on bitwise operators. After you learn how to access individual nibbles in a bit pattern, you can write to a variable whatever you want. But don't expect it to always be safe or mean anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  2. Problem converting std::vector into a reference
    By drrngrvy in forum C++ Programming
    Replies: 18
    Last Post: 10-12-2006, 09:31 PM
  3. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  4. Problem in Converting Unsigned long to String
    By cprogrammer_18 in forum C Programming
    Replies: 8
    Last Post: 01-14-2006, 08:57 AM
  5. Not sure why converting from color_t* to float is a problem
    By Shadow12345 in forum C++ Programming
    Replies: 3
    Last Post: 11-20-2002, 07:05 PM