Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need Function for Conversion to send buffer

Status
Not open for further replies.

DougCa

Technical User
Nov 1, 2002
13
0
0
US
DougCa (TechnicalUser) Jan 8, 2003
How do I do this?

Here is a function. I need to modify this to put the data into 4 char bytes, an upper set an a lower set. For example, I want the value of data if it is integer 31 to go into buf2 as four vaules on the end of prefix before the \3. See below.

Say the prefix is "123" and data is between -31 and +31.

setvalue (char *prefix, int data)

{
int Result;

strcpy (buf2, prefix);
sprintf(buf2 + strlen(buf2), "%2x\3", data);
Result = sendData (buf2, strlen(buf2));
return Result;
}

I want buf2 to contain 123FFE1\3 or 123001F\3
The -31 becomes FF E1 and + 31 becomes 00 1F. Basically I need an interger to ASCII Character Hex routine. It takes the integer an converts it to Character Hex values. For example, 31 becomes 001F and -31 becomes FFE1.The values 0-9 get converted to 30 to 39 Hex and A-F get converted to 41-46 Hex.

I think I want something like:

dataupper
if "-" then FF
else
00
datalower
itoa (31)
would convert it to 1F
sprintf (buf2 + strlen(buf2), "%2x%3x", dataupper, datalower);
Result = sendData (buf2, strlen(buf2));

I would like it if someone can give me a complete function that would result in sendData sending a buf2 containing the prefix, the 4 bytes of the converted integer and the \3 (ETX) on the end. Any questions let me know. Otherwise thanks.


Thanks for the Help.

Doug Cabell
Alternate Eye, LLC


 
Why don't you try something like this:
value (char *prefix, int data)

{
int Result;
char temp[10]; char * p;

sprintf(temp, "%04X", data);
if(data < 0) p = &temp[4];
else p = temp;

strcpy (buf2, prefix);
sprintf(buf2 + strlen(buf2), &quot;%s\3&quot;, p);
Result = sendData (buf2, strlen(buf2));
return Result;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top