Type Conversions

This is a discussion on Type Conversions within the C++ Programming forums, part of the General Programming Boards category; Im pulling in data through a winsock. The data im pulling in in of type Unsigned Char , Ive see ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    10

    Type Conversions

    Im pulling in data through a winsock. The data im pulling in in of type Unsigned Char , Ive see an example that uses the following to displayed the data correctly

    Code:
    printf("Flags=%d\n",htons(pTcpheader->Flags));
    Is there any other way to do this, possibly using cout? I also want to convert the data, and then compare it in a switch. Any ways to convert it besides that printf() would be appricated. Thanks.

  2. #2
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    The conversion is being done by htons, not by printf, so:

    Code:
    short value = htons(pTcpheader->Flags);
    
    cout << "Flags = " << value;
    
    switch (value) {
        // do whatever ...
    }
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    If it's only unsigned char then you don't need htons(). You only need to worry about endianness when the data is more than 1 byte.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 02:49 PM
  2. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 03:44 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

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