Thread: 2 bytes to short

  1. #1
    C++ Programmer
    Join Date
    Aug 2005
    Posts
    39

    2 bytes to short

    Hi, I'm trying to create a game query program, which sends a query to the server through UDP, and the server sends a response back, with some info about the server.

    The response of the server is stored in char * data; But now my question, how do I convert to bytes (actually 2 char's) to a short value?

  2. #2
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    If you two byte data has the format of a short int do this:[edited: casting]
    Code:
    short* sdata = static_cast<short*>(data);
    If it hasn't why converting?
    Last edited by siavoshkc; 08-08-2007 at 04:33 AM.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  3. #3
    C++ Programmer
    Join Date
    Aug 2005
    Posts
    39
    Well, this will be the response of a Doom 3/Enemy territory: Quake Wars server:
    http://www.int64.org/docs/gamestat-protocols/doom3.html

    The player's ping and rate are int16, and because I receive the data as a char *, I need to convert it.

    I guess, I can use the cast?

  4. #4
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Am I right? This string (data) has both ping and rate (and more data?) after each other.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  5. #5
    C++ Programmer
    Join Date
    Aug 2005
    Posts
    39
    Yep, that's true. C# has a nice BitConverter class, which allows you to convert some specific bytes at a specific offset to an other data type.

    You must not see this as a string, but as a byte sequence.

  6. #6
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    So you need the offset of rate and ping in the string. Suppose ping is byte 3 to 5 and rate is 5to 7. (zero based indexing)

    Code:
    short* ping_ptr  = static_cast<short*> (data + 3);
    short* rate_ptr =  static_cast<short*> (data + 5);
    Now our new pointers point to ping and rate in data string.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  7. #7
    C++ Programmer
    Join Date
    Aug 2005
    Posts
    39
    Ok, thanks I think I understand it, and when I de-reference them, I've got the short value isn't it?

  8. #8
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Yes, you will.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  9. #9
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    But I think there is better way. You should make a struct with the feelds :
    ping
    rate
    etc

    And then fill them with data.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  10. #10
    Registered User
    Join Date
    Jul 2006
    Posts
    162
    i could be off here, but can't you do something like...

    short s1 = (short) ((chrdata >> N) & 0xFF...F)

    an example scenerio:

    i have an int with rgb values in it, to get it out you do

    for example...

    iRGB = 0xFFA19300 //0xRRGGBB00
    // r = 255 -> 0xFF ... and so on

    so i'd capture the values by doing...

    int r = (iRGB >> 16) & oxFF
    int g = (iRGB >> 8) & oxFF
    int b = (iRGB) & oxFF


    same concept.... right?

  11. #11
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    No you can't do it with data because it is a pointer to a string. Bitwise operators work on variables not strings. In strings we don't need them because each byte is addressable directly.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  12. #12
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by MrLucky View Post
    The response of the server is stored in char * data; But now my question, how do I convert to bytes (actually 2 char's) to a short value?
    In addition to the cast, you might need to convert the bytes from network order to host order:

    Code:
    short sdata = ntohs(*static_cast<short *>(data));

  13. #13
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by siavoshkc View Post
    No you can't do it with data because it is a pointer to a string. Bitwise operators work on variables not strings. In strings we don't need them because each byte is addressable directly.
    It is pointer to character array (not each char array is a string)
    and of course each member of the array is a char - so we CAN apply bitwise operations to the members of the array.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  14. #14
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    It is pointer to character array (not each char array is a string)
    and of course each member of the array is a char - so we CAN apply bitwise operations to the members of the array.
    Of course you can. But it doesn't give you anything useful in this case. I said we can not do it on strings. But you can do it on each character.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  15. #15
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    ut it doesn't give you anything useful in this case.
    It depends on what is stored there... If some short value is stored as 2 bytes char array for sending throgh the network - of course the bitwise operation will give you something useful - the original value.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  2. Say what? - Weird error.
    By Blackroot in forum C++ Programming
    Replies: 6
    Last Post: 08-15-2006, 11:54 PM
  3. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM
  4. Color Variety
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 10-23-2002, 09:17 AM
  5. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 01:54 PM