08-16-2010 01:08 AM
Solved! Go to Solution.
08-17-2010 04:17 PM
While revising the code you posted (and the original one linked by you) I seem to have found a little mistake that may possibly explain why your code is not working correctly. In my opinion the for clause should be for (i = 0; i < strlen (src) / 2; i += 2) for the code to work correctly; in the original code infact characters 0-1 are read in the first run, 1-2 in the second and so on, while you should read 0-1 in the first run, 2-3 in the second and so on.
08-18-2010 07:10 AM
Hi Roberto,
Thanks for your suggestion, but still i am facing problem,
i think the real problem is with 00,because when converting into ASCII, third byte 00 is noted as null character .so i was not able to read the entire message,finally in dst i am getting only 2 ascii character for 03 and 46 and rest will be omitted by seeing null character (00) . what is the solution to read the all characters. i am struggling to get solution. pls help me
int i, ch,senddata_len =0; char src[30], dst[20]; char dst_data[100]={0}; unsigned char final_msg[100]={0}; strcpy (src, "0346000101012A4BC0"); senddata_len = strlen(src); for (i = 0; i < senddata_len ; i++) { Scan (src, "%s[i*w2]>%i[r16]", i * 2, &ch); sprintf (&dst[i], "%c", ch); //Fmt(dst_data,"%c",ch); //strcat(final_msg,dst_data); } ComWrt (port, dst, senddata_len);
08-20-2010 02:01 AM
Hi,
maybe this help 🙂
int i, ch,senddata_len =0;
char src[30];
unsigned char dst[20];
//it is nesscesary to work with binary data as unsigned, otherwise some signed/unsigned conversion happen
char dst_data[100]={0};
unsigned char final_msg[100]={0};
strcpy (src, "0346000101012A4BC0");
senddata_len = strlen(src);
senddata_len-=senddata_len%2;//use only lengtht in multiple of two(if input src has bad length "034"
for (i = 0; i < senddata_len ; i+=2)//process every two characters
{
//Scan (src, "%s[i*w2]>%i[r16]", i * 2, &ch);
//Scan(...)posibly good command, but i am not familiar with "Scan
// and i think next code can be litle faster after some optimization"
//{Warning:this code not check invalid characters like "03T600"}
ch=0;
if((src[i]>='0')&&(src[i]<='9'))ch=src[i]-'0';
if((src[i]>='a')&&(src[i]<='f'))ch=src[i]-'a'+10;
if((src[i]>='A')&&(src[i]<='F'))ch=src[i]-'A'+10;
ch=ch<<4;//shift data by 4bits(the same as ch=ch*16;)
if((src[i+1]>='0')&&(src[i+1]<='9'))ch|=src[i+1]-'0';
if((src[i+1]>='a')&&(src[i+1]<='f'))ch|=src[i+1]-'a'+10;
if((src[i+1]>='A')&&(src[i+1]<='F'))ch|=src[i+1]-'A'+10;
//sprintf (&dst[i], "%c", ch);
//do not use sprintf to contencate binary data
dst[i/2]=ch;
}
//ComWrt (port, dst, senddata_len);
//Binary data has only half size of its Ascci representation
ComWrt (port, dst, senddata_len/2);
08-20-2010 03:54 AM
Hi OVR_CZ,
it really works well..
u have solved my issues.
Thank you very much