Char string to unsigned short

This is a discussion on Char string to unsigned short within the C++ Programming forums, part of the General Programming Boards category; I checked faq and eather didnt see it for wasnt there. I have a character array like this char array ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    110

    Char string to unsigned short

    I checked faq and eather didnt see it for wasnt there.

    I have a character array like this


    char array = {0x20,0x30);

    I want to combine them in a unsigned short is it will be 8240 (0x20,0x30);

    I made a struct and did a memory copy but the number was wrong.

    How do i do this?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    There is a macro for this, if your compiler dosen't have it then it is defined like this
    Code:
    #define MAKEWORD(a, b) \ 
        ((WORD) (((BYTE) (a)) | ((WORD) ((BYTE) (b))) << 8))

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    Cant seem to get it to work, it compiles but not sure how to use it!

    unsigned short Value = MAKEWORD(array[0],array[1]);

    ?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    unsigned short Value = MAKEWORD(array[0],array[1]);
    That looks fine, what happens?

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Try this
    Code:
    unsigned short Value = MAKEWORD(array[1],array[0]);

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    BYTE Undeclared idetifyer?

    Im using Vs.net 2004 I didnt know there was a BYTE variable in it. What do I inculde for it?

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    I think BYTE is declared in windows.h or one of the files inlcuded by windows.h .
    You can also change BYTE to unsigned char. Here is a more portable version
    Code:
    #define MAKEWORD(a, b) ((unsigned short) (((unsigned char) (a)) | ((unsigned short) ((unsigned char) (b))) << 8))
    Last edited by Quantum1024; 05-22-2005 at 11:22 PM.

  8. #8
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    Nice yeah I changed it, kind of wish i could get the BYTE to work. Right now this is perfect!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A Question About Unit Testing
    By Tonto in forum C++ Programming
    Replies: 2
    Last Post: 12-14-2006, 08:22 PM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 06:59 AM
  4. ANY BODY WILLING TO HELP ME WITH Microsoft Visual C++
    By BiG pImPiN fOoL in forum C++ Programming
    Replies: 12
    Last Post: 11-04-2001, 06:03 PM

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