I am trying to take an integer argument (1-65536) and turn it into two hex characters. If argument -t is -t20 then I need 0x01 0x04 in a character array for sending through a socket.
Relavent snippets:

char tstr[3];
char timetxt[6];

struct pulsepayload {
	char chan;
	char state;
	char width[2];
} pp;
....
// get the arguments
case 't':         // if -t then do pulse command
	cmd0 = 0x07;
	strcpy(timetxt, &argv[1][2]);
        // put some magic here  to get
        // tstr[0] = 0x05;        // forcing 5 sec here
       // tstr[1] = 0x00;
       // when -t5

.....
// Populate the payload
	pp.chan = cnl;
	pp.state = st;
	pp.width[0] = tstr[0];   
	pp.width[1] = tstr[1];
	pdata = &pp;
	char *paylp = (char*)pdata;
.....
// combine header and payload and send
printf("pulse command: to State %d, time %d \n ",st, tstr[1]);
strcat(command, paylp);
bytesSent = send(m_socket, command, 51, 0);

Thanks in advance.

I am trying to take an integer argument (1-65536) and turn it into two hex characters.

First problem -- 65536 cannot fit in 2 hex characters. 0-255 can, but 65535 takes 4.

If argument -t is -t20 then I need 0x01 0x04 in a character array for sending through a socket.

Convert the 20 into decimal, then use math to convert to hex characters. How does 20 become 0x01 0x04?

Relavent snippets:

There is no relevance to this code snippet since there is no attempt to do the conversion yet. Computers don't work by magic and we -- like magicians -- don't give away secrets. We will help guide you to the secrets, but we don't give them away. Try it first yourself. Search these forums.

> Convert the 20 into decimal, then use math to convert to hex characters. How does 20 become 0x01 0x04?
20 decimal is 0x14 in hex.

Contrived, I know, but perhaps they're trying to display nibbles on a 7-segment display which only reads the least significant nibble of each byte.

We need more details!

First problem -- 65536 cannot fit in 2 hex characters. 0-255 can, but 65535 takes 4.

Sorry for my noobliness and mistypes. In my thinking 65535 needs to map to two characters 0xff and 0xff

Convert the 20 into decimal, then use math to convert to hex characters. How does 20 become 0x01 0x04?

Sorry again. I meant 0x00 0x14.

There is no relevance to this code snippet since there is no attempt to do the conversion yet. Computers don't work by magic and we -- like magicians -- don't give away secrets. We will help guide you to the secrets, but we don't give them away. Try it first yourself. Search these forums.

I looked pretty hard first. I am a habitual lurker (you should have guessed that by my inability to post intelligently) Here's what I started with for conversion:

case 't':         // if -t then do pulse command
    strcpy(timetxt, &argv[1][2]);
    time = atoi(timetxt);
    itoa(time, tstr, 16);

When argument -t10 => 0x10 = ('a') = 0x61 61 seconds vs. desired 10.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

There is no conversion.
20 decimal is stored in an integer as 0x00 0x14
You just send the least significant 16 bits of your int - that's all.

> Convert the 20 into decimal, then use math to convert to hex characters. How does 20 become 0x01 0x04?
20 decimal is 0x14 in hex.

I understood. It was a Q to the OP -- wondering if he understood.

Comments
okies :)
This article has been dead for over six months. Start a new discussion instead.