Is there a quick or smart way to turn an int
type into a char*
type in the C programming language?
For example, if I have an integer defined such as int X = 9000
and a character array buffer char *Y
, how can Y
be assigned 0x90,0x00
?
I can only think of subtraction, that is to separate the int X
into two bytes and then subtract a certain number in order to obtain the corresponding char
in that ASCII representation.
Here are the relevant parts to the code I am using which might help solve this:
int main(void)
{
unsigned char *datafield, *command1, command[256], response[256], commandlen1, resplen;
unsigned short int commandlen, i, j, k, sw, x;
sw1 = 0x90;
sw2 = 0x00;
response[0] = sw1;
response[1] = sw2;
resplen = sizeof(sw);
printf("reponse[0] %02Xh ",response[0]);
printf("reponse[1] %02Xh ",response[1]);
printf("reponse %02Xh", response);
Send(response, resplen);
}
and the declaration of the Send
function being called is:
short int Send(char *message, unsigned short int nbytes)
{
printf("message %02Xh ", message);
printf("message[0] %02Xh ", message[0]);
printf("message[1] %02Xh", message[1]);
printf("message[2] %02Xh", message[2]);
printf("nbytes %i ", nbytes);
if ((fd = open("reader.txt", O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0) {
printf("open error\n");
if (write(fd, message, nbytes) != nbytes) {
printf("write error\n");
}
if (close(fd) < 0) {
printf("close error\n");
}
return(NO_ERR);
}
}
After running the code this is the output I received:
reponse[0] 90h reponse[1] 00h reponse 244FD38hmessage 244FD38h message[0] FFFFFF90h message[1] 00hmessage[2] 00h
-
0Can you copy and paste some of your relevant code? I think it might be clearer on what is going on that way maybe 🙂 — Brian Wozeniak