char string to long conversion

This is a discussion on char string to long conversion within the C Programming forums, part of the General Programming Boards category; Hello, I have the following question: I am trying to fit a long variable into char string and then convert ...

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    4

    char string to long conversion (more problems)

    Hello, I have the following question:

    I am trying to fit a long variable into char string and then convert it back to long (need this to send that string over the network)

    char cs[4];
    long lv1=12345;
    long lv2;

    memcpy(cs, &lv1, 4);
    lv2=(long)(tsC);

    printf("lv2: %lu ", lv2);

    for some reason this doesn't work

    Thanks in advance,
    Bill
    Last edited by rastan; 11-04-2003 at 06:19 PM.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    To change a long into a string, use sprintf. To change a string back into a long, use strtol. Nice and easy.
    My best code is written with the delete key.

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    What about using sscanf( ) instead of strtol?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    4
    Thanks guys, seems to be working, just because of cuoriosity why the memcpy solution didn't work?

    Thanks,
    Bill

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by rastan
    Thanks guys, seems to be working, just because of cuoriosity why the memcpy solution didn't work?

    Thanks,
    Bill
    Perhaps because...
    Code:
    lv2=(long)(tsC);
    You have no 'tSC' variable in your example?

    Oh, and because numbers aren't stored as strings. And if they were, the '1' in your example wouldn't be the first cell of your array, it would be the last to make math easier. Each digit wouldn't be a seperate character.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Nov 2003
    Posts
    4
    sorry I made a mistake while posting the code, actually I have "cs"
    not "tsC"

    Anyway I found the answer:
    lv2=*(long*)&(cs);

    Thanks to everybody,
    Bill
    Last edited by rastan; 11-05-2003 at 10:17 AM.

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    51
    with a few changes ur original code works

    Code:
    char cs[4];
    long lv1=12345;
    long *lv2;
    
    memcpy(cs, &lv1, 4);
    lv2=(long*)(cs);
    
    printf("lv2: %lu ", *lv2);

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: char string to long conversion (more problems)

    Originally posted by rastan
    I am trying to fit a long variable into char string and then convert it back to long (need this to send that string over the network)
    Is this any good for you:
    http://www.ecst.csuchico.edu/~beej/g...t_old/#convert
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User
    Join Date
    Nov 2003
    Posts
    4
    Hammer, this is usefull staff but unfortunatelly not related to the question. Those functions convert byte orders in uint32_t /uint16_t variables but hey do not convert char[] to longs

    Thanks,
    Bill

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Hammer's post was trying to help you further along the line since your question about string to long conversion was adequately answered previously.
    My best code is written with the delete key.

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>long variable into char string and then convert it back to long (need this to send that string over the network)<<
    This suggests to me your converting from a long to a char[] so that when you send it to the remote machine, it will receive a character based representation of a number. It will then convert it back to a long in its native form. Am I understanding you right? If so, the ntohl() type functions will take care of all that for you.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 09:33 PM
  2. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2003, 11:03 PM
  3. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 12:18 AM
  4. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM

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