Click here to Skip to main content
11,704,768 members (50,525 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: MFC VC++
CString itemno;
 
UpdateData(TRUE);
 
itemno.Format("%x",m_itemno);// here m_itemno is integer in range 0 to 100

int  len = itemno.GetLength();
 
if(len == 1)
 
itemno = "0x0" + itemno;
 
if(len == 2)
 
itemno = "0x" + itemno;
 

 
unsigned char data[1] = itemno; // Here I  get problem;

~~~code for CreateFile etc~~~
 
bool retval = WriteFile(hport,data,1,&byteswritten,NULL);
 
//I have to send only 1 byte of data
Now the problem I am facing is that I am unable to convert from CString to
unsigned char data[1]. 
If for example if m_itemno is 12,then its hexadecimal equivalent is 0x0c

if I write unsigned
char data[1] = {0x0c}
,everything works fine but it is like hardcoding

but I want it done programatically so that I can enter any number between o to 100;

How to do it ?
Posted 21-Jun-12 17:53pm
Edited 21-Jun-12 18:11pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You want to send a binary data byte containing a (decimal) number in the range of 0 to 100 (0x64). Your member variable m_itemno already contains this value. So you might use this:

unsigned char data = static_cast<unsigned char>(m_itemno & 0xFF);
bool retval = WriteFile(hport,&data,1,&byteswritten,NULL);

Because Intel processors use Little-Endian byte order (least significant byte is stored at lowest address) you may even use:

bool retval = WriteFile(hport,&m_itemno,1,&byteswritten,NULL);
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

My problem got solved by using following code snippets
CString Class::IntToHex(int* yourint)
 
{
unsigned char ch[1];
Cstring output;
memcpy(ch,yourint,sizeof(int));
retVal = WriteFile(hPort,ch,1,&byteswritten,NULL);
}
 
CString Class::FloatToHex(float* yourfloat)
{
unsigned char ch2[4];
Cstring output;
 
memcpy(ch2,yourfloat,sizeof(float));
retVal = WriteFile(hPort,ch2,4,&byteswritten,NULL);
}
  Permalink  
v2
Comments
Jochen Arndt at 23-Jun-12 4:55am
   
Copying the binary data using memcpy is not necessary. Just pass them as they are to WriteFile:
WriteFile(hPort, yourint, 1, &byteswritten, NULL);
WriteFile(hPort, yourfloat, sizeof(float), &byteswritten, NULL);
Your code contains a buffer overrun in IntToHex when copying 4 bytes (sizeof(int)) to the buffer ch which has a size of 1. The only reason why your code does not crash is the existance of the unused local variable CString output that is overwritten by memcpy.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 679
1 OriginalGriff 455
2 Andy Lanng 245
3 CPallini 242
4 Maciej Los 225
0 OriginalGriff 8,864
1 Sergey Alexandrovich Kryukov 8,245
2 CPallini 5,189
3 Maciej Los 4,726
4 Mika Wendelius 3,606


Advertise | Privacy | Mobile
Web03 | 2.8.150819.1 | Last Updated 22 Jun 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100