Problem in Converting Unsigned long to String

This is a discussion on Problem in Converting Unsigned long to String within the C Programming forums, part of the General Programming Boards category; Hi All, I am facing the Problem while converting from unsigned long to string. can any body help me out? ...

  1. #1
    Registered User cprogrammer_18's Avatar
    Join Date
    Dec 2005
    Posts
    9

    Problem in Converting Unsigned long to String

    Hi All,
    I am facing the Problem while converting from unsigned long to string. can any body help me out?

    The thing is i have an unsigned long. I have to convert that to string.

    Consider,
    unsigned long type=0x11;

    The unsigned long will take 4 bytes. I want to convert this to string of 4 Bytes.

    Again if I convert the string of 4 Bytes to unsigned long I have to get the origianal value.

    i tried using sprintf(str, "%lu", sizeof(unsigned long));

    When I tried to get the first four bytes and convert it back to unsigned long it is not giving the original value.

    Can anybody help me out.. Please?

    Thanks in Advance.

    - Maddy.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    Assuming your value is 9999 or less, the approach is
    Code:
    char buffer[5];
    sprintf(buffer, "%lu", value);
    This will produce a string of maximum length 4 (plus an additional zero byte).

    If the value is 10000 or more (i.e. 5 or more digits) it won't squeeze into a string of length 4 in a decimal format.

  3. #3
    Registered User cprogrammer_18's Avatar
    Join Date
    Dec 2005
    Posts
    9
    Thanks Grumpy...

    But My Problem is that if i give

    unsigned long type = 1;

    I have to get the string which occupies the whole 4 bytes. I mean I have to get the string like

    0001

    And if
    unsigned long type=12345;

    I have to get this also in 4 Bytes string.

    The unsigned long stores the 12345 in 4 bytes. So if i convert that also in to a string i have to get in 4 bytes.

    Can any body help me out..

    Thanks in Advance.
    -Maddy

  4. #4
    Registered User Afrinux's Avatar
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    46
    Code:
    sprintf(str, "%lu", sizeof(unsigned long));
    here you will get 4 in str.
    unsigned long type = 1;
    I have to get the string which occupies the whole 4 bytes. I mean I have to get the string like
    0001
    And if
    unsigned long type=12345;
    I have to get this also in 4 Bytes string.
    In that case, you should be using
    Code:
    sprintf(str, "%lu", type );
    instead of
    Code:
    sprintf(str, "%lu", sizeof(unsigned long));

  5. #5
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by cprogrammer_18
    unsigned long type = 1;
    I have to get the string which occupies the whole 4 bytes. I mean I have to get the string like
    0001
    And if
    unsigned long type=12345;
    I have to get this also in 4 Bytes string.
    Try this
    Code:
    unsigned long type=1;
    sprintf(str, "%04lu", type );
    This will output 0001.
    But if you want to squeeze a 5 digit number into 4 chars then you are out of luck.
    if you want the value 12345 output only 1234 or maybe 2345 then you have to write your own function that handles truncation the way you want it. (but then there is no way to restore the original value from the string )
    Maybe you should tell us again what you try to accomplish.
    Fact is that not every unsigned int fits into a string with the length of 4. storing it as hex will need a 9 char buffer. decimal 11 ( including the '\0' terminator ).

    BTW if what you want is pass an unsigned int to a function like fwrite() let us know.
    Kurt

  6. #6
    Registered User cprogrammer_18's Avatar
    Join Date
    Dec 2005
    Posts
    9
    Thanks Zuk..

    Well.. I will explain in detail...
    I have a structure

    Code:
    struct payload{
    unsigned long type;
    unsigned long length;
    unsigned char value[1024];
    };
    I have to read the structure element and concatenate in to string. I have to pass this string in to a socket. On the other end I have to read it.

    So I know the unsigned long occupies 4 Bytes and while I read on the other end I have to read the first 4 bytes and convert back in to unsigned long.. and the next bytes and so on...

    So how to do this? Any Ideas...

  7. #7
    Registered User cprogrammer_18's Avatar
    Join Date
    Dec 2005
    Posts
    9
    Contd...

    I dont want to use any delimiters while concatenating..

    -Maddy

  8. #8
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    That's what I thought.
    You don't have to convert your struct to a string you just cast it's address to a char *
    for sending you do
    Code:
    payload p;
    send( (char*)&p, sizeof(p));
    at the receiving end you do

    Code:
    char  buffer [256];
    payload * pp;
    recv( buffer, sizeof(payload) );
    pp = ( payload *)buffer;
    // you access the fields like this
    printf("type = %lu\n", pp->type );
    Hope that is what you want.
    Kurt

  9. #9
    Registered User cprogrammer_18's Avatar
    Join Date
    Dec 2005
    Posts
    9
    Thanks a lot...

    I got the solution...



    Maddy

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting using pointer to pointer not working
    By eager2no in forum C Programming
    Replies: 17
    Last Post: 09-21-2008, 12:52 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. Need Help Please
    By YouShallNotPass in forum C++ Programming
    Replies: 3
    Last Post: 08-22-2006, 11:22 AM
  4. can someone check this out and let me know ?
    By javaz in forum C Programming
    Replies: 5
    Last Post: 01-21-2002, 01:13 PM

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