Help using sprintf, wanting to make 2 byte lengths and 4 byte lengths to hex string?

Discussion in 'C Programming' started by jt, May 18, 2005.

  1. jt

    jt Guest

    Here is my code:

    unsigned long listenip;
    short tlen;

    sprintf(tmp,"len:%x listenip:%x",htonl(tlen),listenip);

    If the value on "tlen" is less than 2 bytes and the value on "listenip" is
    less than 4 bytes, how can I make it fill with preciding 0 zeros?

    Thanks,
    JT
     
    jt, May 18, 2005
    #1
    1. Advertising

  2. jt

    baumann@pan Guest

    jt wrote:
    > Here is my code:
    >
    > unsigned long listenip;
    > short tlen;
    >
    > sprintf(tmp,"len:%x listenip:%x",htonl(tlen),listenip);

    since tlen is less than 2 bytes, you should use htons() instead.

    sprintf(tmp,"len:%02x listenip:%04x",htonl(tlen),listenip);
    above should work for your purpose.


    >
    > If the value on "tlen" is less than 2 bytes and the value on

    "listenip" is
    > less than 4 bytes, how can I make it fill with preciding 0 zeros?
    >
    > Thanks,
    > JT
     
    baumann@pan, May 18, 2005
    #2
    1. Advertising

  3. On 17 May 2005 18:03:40 -0700, "baumann@pan" <>
    wrote:

    >
    > jt wrote:
    > > Here is my code:
    > >
    > > unsigned long listenip;
    > > short tlen;
    > >
    > > sprintf(tmp,"len:%x listenip:%x",htonl(tlen),listenip);

    > since tlen is less than 2 bytes, you should use htons() instead.
    >

    is _exactly_ 2 bytes, on "normal" systems with 8-bit byte and 16-bit
    short, which is not absolutely everywhere but does include the places
    sockets functions like these (and maybe Unix I/O in general) are
    likely to be available and work as expected.

    > sprintf(tmp,"len:%02x listenip:%04x",htonl(tlen),listenip);
    > above should work for your purpose.
    >

    htons, as you said; and %04lx (ell ecks) for the listenip; x with no
    modifier expects and prints unsigned int which can be as little as 16
    bits (2 bytes) and is on some systems, at least one of them socketed.

    - David.Thompson1 at worldnet.att.net
     
    Dave Thompson, May 23, 2005
    #3
  4. Re: Help using sprintf, wanting to make 2 byte lengths and 4 bytelengths to hex string?

    Dave Thompson <> writes:
    > On 17 May 2005 18:03:40 -0700, "baumann@pan" <>
    > wrote:

    [...]
    >> since tlen is less than 2 bytes, you should use htons() instead.
    >>

    > is _exactly_ 2 bytes, on "normal" systems with 8-bit byte and 16-bit
    > short, which is not absolutely everywhere but does include the placesn
    > sockets functions like these (and maybe Unix I/O in general) are
    > likely to be available and work as expected.


    I've used Unix systems where short is 32 bits, and others where it's
    64 bits (both Crays). Along with CHAR_BIT==8 and the lack of extended
    integer types, this implies that there is no 16-bit integer type,
    which makes some of the network stuff interesting (it uses bit
    fields).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, May 23, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    6,407
    Neredbojias
    Aug 19, 2005
  2. Bengt Richter
    Replies:
    6
    Views:
    534
    Juha Autero
    Aug 19, 2003
  3. sprintf syntax for string to hex

    , Mar 24, 2005, in forum: C Programming
    Replies:
    3
    Views:
    799
    CBFalconer
    Mar 24, 2005
  4. Neville Franks
    Replies:
    2
    Views:
    97
    Neville Franks
    Feb 8, 2007
  5. Bharath Kummar
    Replies:
    76
    Views:
    756
    Tim Chase
    Nov 28, 2013
Loading...

Share This Page