Home > Software engineering >  Unsigned char [] array transformation const char *
Unsigned char [] array transformation const char *

Time:09-23

Project requirements called dynamic link library input parameter is the char [] type, is the actual input data byte [] type (which is actually eight bytes), namely 0 ~ 255, thus using the unsigned char, assignment using sprintf method, requires the use of const char *, so do the casts, but found the result of the transformation is not what I want, is converted to a character string, and does not appear to be unsigned, and only one Chinese character, is 4 bytes or 2 bytes? Also less, I really not in c + +, seek advice, thank you
The code is,

Unsigned char \ [8].
Unsigned char newIP [4]={192168,0,111};
Int newCmdPort=3000;
Int newImgPort=4001;
\ [0]=newIP [0];
Temp [1]=newIP [1];
\ [2]=newIP [2];
\ [3]=newIP [3].
\ [4]=(char) (newCmdPort/256);
\ [5]=(char) (newCmdPort % 256);
\ [6]=(char) (newImgPort/256);
\ [7]=(char) (newImgPort % 256);

Const char * cIp=(const char *) temp (unsigned char *);
Sprintf (para. IpRemoteChange, cIp);

CodePudding user response:

Problem has changed, use the following code to achieve the parameter input dynamic link library, but a new problem, warned ipRemoteChange (char [] type) needs to be converted to char *, should how to convert? How the unsigned char conversion of a char *?

Unsigned char \ [8].
Unsigned char newIP [4]={192168,0,111};
Int newCmdPort=3000;
Int newImgPort=4001;
\ [0]=newIP [0];
Temp [1]=newIP [1];
\ [2]=newIP [2];
\ [3]=newIP [3].
\ [4]=(char) (newCmdPort/256);
\ [5]=(char) (newCmdPort % 256);
\ [6]=(char) (newImgPort/256);
\ [7]=(char) (newImgPort % 256);

//const char * cIp=(const char *) (unsigned char *) temp;
//strcpy (para. IpRemoteChange, cIp);
//sprintf (para. IpRemoteChange, cIp);
for(int i=0; I<8; I++)
{
Para. IpRemoteChange [I]=\ [I];
}

CodePudding user response:

No translation, direct assignment will automatically convert,

CodePudding user response:

Memcpy with more convenient, it is not recommended in this circulation assignment:
 
Memcpy (ipRemoteChange, temp, 8);

CodePudding user response:

Para. IpRemoteChange (pa) (char *)

CodePudding user response:

Thank you for your reply, now the question is warned ipRemoteChange types defined as unsigned char [] [] when the input is correct, I need to send the UDP para. IpRemoteChange [], before using the sendto () method, can only send a const char *, so always want to para. IpRemoteChange [] into a const char *, probably because he is a byte (greater than 127), the result of the transformation is wrong, now I want to use UDP directly send byte [], should with what method?

CodePudding user response:

Assignment do not use sprintf, sprintf is dealing with string, met 0 to truncate, char and unsigned char only need to pay attention to this problem

CodePudding user response:

refer to 6th floor xianglitian response:
assignment do not use sprintf, sprintf is dealing with string, met 0 to truncate, char and unsigned char only need to pay attention to this problem


Thank you very much, could you speak more specific, what method should use the assignment

CodePudding user response:

Sendto (sock, (char *) para. IpRemoteChange,...
Try this

CodePudding user response:

 
#include
Void XXXX ()
{
192168,0,111 BYTE newIP [4]={};
WORD newCmdPort=3000;
WORD newImgPort=4001;
//
Const BYTE \ [8]={newIP [0], newIP [1], newIP [2], newIP [3],
LOBYTE (newCmdPort),
HIBYTE (newCmdPort),
LOBYTE (newImgPort),
HIBYTE (newImgPort)};
}

CodePudding user response:

refer to 7th floor syue126 response:
Quote: refer to the sixth floor xianglitian response:

Assignment do not use sprintf, sprintf is dealing with string, met 0 to truncate, char and unsigned char only need to pay attention to this problem


Thank you very much, could you speak more specific, what method should use assignment?
memcpy

CodePudding user response:

Const BYTE \ [8] must use the initialization statement, cannot be defined before the assignment (const BYTE!)
  • Related