Thread: Printing a int

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    167

    Printing a int

    it is ok if I do ?

    Code:
    fprintf(OUTPUT,"Source_Port=\t%d\n",((pkt_data[34])<<8)|pkt_data[35]);
    on what I am working it displays just fine... but I want to know if I would encounter any problems if this gets on another workstation

    Thankyou!

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Don't assume the byte order. Use hton[ls]() and ntoh[ls]() to "normalize" the data in the packet stream.

    gg

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Codeplug View Post
    Don't assume the byte order. Use hton[ls]() and ntoh[ls]() to "normalize" the data in the packet stream.
    It's not like "network order" is going to change. However, I also agree that ntohs() is a simpler way of converting it.

    Also, if the data order was little-endian instead (dumb, but possible) there is no standard function which converts it for you. ntohs() isn't even that standard anyway (not part of standard C, anyway).

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    167
    The stream that I use is from a pcap file (i use for processing winpcap) and the host is always a pc x86.. i belive that they have always the same endianness. I know that is a good practice to use the conversion function but in this case I can work with this.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by spank View Post
    The stream that I use is from a pcap file (i use for processing winpcap) and the host is always a pc x86.. i belive that they have always the same endianness. I know that is a good practice to use the conversion function but in this case I can work with this.
    Yes, x86 is guaranteed to stay little endian for the foreseeable future.

    Making it portable just means that if you end up re-using this bit of code for something else, it will then work "automagically" when someone decides to run the same thing on a different processor architecture.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory leak
    By aruna1 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2008, 10:28 PM
  2. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  3. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM
  4. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 07:34 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM