Thread: Placing an integer into a character

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    110

    Placing an integer into a character

    Okay, I am creating a socket based program, ( a message program of sorts ), and as part of a custom header to the packet, I need to place an integer value, one that has been calculated from other values, into one a position on the character array ( this array is what will hold the packet being sent. )

    So my question is how to get an integer value into the character array, and not have it display as the ascii code.

    I know there is a way to do this though it has just slipped my mind as of late.

    Latter,
    WebmasterMattD
    WebmasterMattD.NET

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    depends how big you want the value to be. You can fit it into a byte if it can be small. in that case:

    // ptr is a char * somewhere in the array
    *ptr = theint;

    If you want to use the whole int size, you can stick it into the bytes as follows

    *((int*)ptr) = theint;

    The easiest way, since you are talking about a header is to define a structure that you can send:

    memcpy(ptr,&theheader, sizeof(theheader));
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Registered User
    Join Date
    Sep 2001
    Location
    Fiji
    Posts
    212
    you could use sprintf, or typecast.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    110
    Thanks for the ideas, I will try implementing them once my brain wakes up.

    Later,
    WebmasterMattD
    WebmasterMattD.NET

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    110
    Also, with the structure. Do you know if you can place the memcopy straight into the send and recv commands such as follows?

    send( sockfd, memcpy(ptr, &(theheader), sizeof( theheader ) ), sizeof( theheader ), 0 );

    recv( sockfd, memcpy( ptr, &(theheader), sizeof( theheader ) ), sizeof( theheader ), 0 );

    thanks for your help.
    WebmasterMattD
    WebmasterMattD.NET

  6. #6
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    I suppose you could
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  7. #7
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    but if you're going to do that you might as well just send the structure directly with no buffer:

    send( sockfd, (char*)&theheader, sizeof( theheader ), 0 );

    The copy was only a way to get it into a buffer cause that's what you asked.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    110
    Gues so, anyway that is what I tried, and since the prototype for send has the buffer as type (const void *) then I just passed the structure directly in, guess I should fiddle around a bit more before I start crying wolf.

    Anyway, when using recv, it requires a value for the size of the structure, though that would varie as the structure currently contains, two strings and one integer.

    Any suggestions as to what it is that I would pass into this?

    Thanks for your help.
    WebmasterMattD
    WebmasterMattD.NET

  9. #9
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    realistically you are going to have to define a protocol that will remain constant. If the protocol starts off with an integer, fine That's easy you just know the size of the integer that you send first every time. The strings as variable length entities will have to have a protocol syntax the same way we have them in the world of C++. a buffer that starts with the size of the string perhaps? Just rely on null termination perhaps? I would prefer the size of the string at the beginning just so you know how much to recv on the other end.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. mygets
    By Dave_Sinkula in forum C Programming
    Replies: 6
    Last Post: 03-23-2003, 07:23 PM