我没有得到正确的输出。
data.m
---------
@implementation data
- (id) initWithID:(int) uMessageId withData:(id)pData withSize:(size_t) uDataSize
{
//if(self=[super init])
//{
// Initialize the member variables
m_uSessionId = 0x00;
m_chSequenceChar= 0;
// Initialize values from derived class
m_nMessageId = uMessageId;
m_pData = (int*)pData;
m_uDataSize = (int)uDataSize;
//}
return self;
}
- (BOOL) TxCreateImage:(id) pData withLen:(id)uLen
{
sprintf((char *)pData,"%x%d%d%d",ASCII_STX,m_uSessionId,m_chSequenceChar,m_nMessageId);
//uLen = ENCODED_MSG_DATA_OFFSET;
NSLog(@"%s",pData);
return YES;
}
- (void)dealloc
{
[super dealloc];
}
@end
data_derived.m //derived class of data
--------------------
@implementation requestSession
- (id)init
{
char* pData[4096];
size_t asize = sizeof(st);
self=[super initWithID:ID withData:(id)pData withSize:asize];
if (self) {
data* dat = [[data alloc]init];
[dat TxCreateImage:self withLen:self];
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
@end
test2.m
---------
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
requestSession* session = [[requestSession alloc]init];
[pool drain];
return 0;
}
remote.h
-----------
#import <Foundation/Foundation.h>
struct RMH_REQUEST_SESSION_MSG //Message Data
{
int uDeviceID;
int uProtocolVersion;
int uReserved[5];
};
typedef enum
{
RM_REQUEST_SESSION = 0x11 //Message ID
} REMOTE_MESSAGE_ID;
@interface remote : NSObject {
}
@end
data.h
---------
#import <Foundation/Foundation.h>
#import "remote.h"
@interface data : NSObject {
@public
int m_nMessageId; //Message ID
int m_uSessionId; //Session ID
int m_chSequenceChar; //Sequence ID
int* m_pData; //Integer buffer to carry data
int m_uDataSize; //Datasize
}
- (id)initWithID:(int) uMessageId withData:(id)pData withSize:(size_t) uDataSize;
- (void)dealloc;
- (BOOL) TxCreateImage:(id) pData withLen:(id)uLen;
//+ (id)CreateMessage:(REMOTE_MESSAGE_ID)nMessageNumber;
//+ (id)CreateMessage:(const int*)szMessageName;
@end
data_derived.h
---------------------
#import <Foundation/Foundation.h>
#import "data.h"
#define DECLARE_RS232_NEWMSG(ClassID)\
enum \
{ \
ID = ClassID \
}; \
@interface requestSession : data {
@public
DECLARE_RS232_NEWMSG(RM_REQUEST_SESSION);
struct RMH_REQUEST_SESSION_MSG st;
int size;
}
-(id)init;
-(void)dealloc;
@end
预期输出
2 0 0 17
。但它打印我
2 0 0 0
我需要传递
中指定的值initWithData方法
在data.m中
TxCreateMessage
在data.m。
答案 0 :(得分:2)
我的回答无关紧要,因为我错过了你的问题。问题是你正在向int *
转换char *
,指针转换是许多问题的根源(在这种情况下,它可能是一个结束问题。)
如果您想使用sprintf,请在char*
上使用,而不是int*
:
- (BOOL) TxCreateImage:(id) pData withLen:(id)uLen
{
char printStr[30];
sprintf(printStr,"%x%d%d%d",ASCII_STX,m_uSessionId,m_chSequenceChar,m_nMessageId);
//uLen = ENCODED_MSG_DATA_OFFSET;
NSLog(@"%s",printStr);
return YES;
}