简体   繁体   中英

How can I store hexadecimals inside an array? C++ MFC

I have to use an array of hexadecimals because I'm doing a program to communicate with a video server controller and he just understands messages in hexadecimal. I can connect the video controller with my server, but when I try to send messages using the send() function, passing an array of unsigned char that contains my information in hexadecimal, it doesn't work.

This is how I am using the array. I don't know if it is correct.

 void sendMessage()
        {
               int retorno;
           CString TextRetorno; 
               unsigned char HEX_bufferMessage[12]; // declaration

            // store info
                HEX_bufferMessage[0] = 0xF0;
                HEX_bufferMessage[1] = 0x15;
                HEX_bufferMessage[2] = 0x31;
                HEX_bufferMessage[3] = 0x02;
                HEX_bufferMessage[4] = 0x03; 
                HEX_bufferMessage[5] = 0x00;
                HEX_bufferMessage[6] = 0x00; 
                HEX_bufferMessage[7] = 0xD1; 
                HEX_bufferMessage[8] = 0xD1; 
                HEX_bufferMessage[9] = 0x00;
                HEX_bufferMessage[10] = 0x00;
                HEX_bufferMessage[11] = 0xF7;

        retorno = send(sckSloMo, (const char*) HEX_bufferMessage, sizeof(HEX_bufferMessage), 0); 

                TextRetorno.Format("%d", retorno);
                AfxMessageBox(TextRetorno); // value = 12

                if (retorno == SOCKET_ERROR)
                {
                    AfxMessageBox("Error Send!! =[ ");
                    return;
                }



            return;

            }

Pop quiz. What's the difference between:

int n = 0x0F;

and:

int n = 15;

If you said, "nothing," you're correct.

When assigning integral values, specifying 0x , 00 for octal, or nothing for decimal makes no difference in what is actually stored. This is a convenience for you, the programmer only. These are integral variables we're talking about -- they store numeric data only. They don't store or care about radix. In fact, you might be surprised to learn that when you assigned a numeric value to an integral variable, what is actually stored isn't decimal or hexadecimal or even octal -- it's binary.

Since you're storing these values as unsigned char , and char ( unsigned or otherwise) is really just an integral type, then what you're doing is fine:

HEX_bufferMessage[0] = 0xF0;
HEX_bufferMessage[1] = 0x15;
HEX_bufferMessage[2] = 0x31;

but your question makes no sense:

Anyone knows if using an array of unsigned char is the right way to store hexadecimals??

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM