Wrong answer!
Your problem is you have no idea how to call some very simple function with very good described parameter list!
so why do you have such a problem? :confused:
Printable View
This is an example of using Stuffdata.
Code:const char text[] = "abcdefghijklmnopqrstuvwxyz";
char codify[200];
StuffData(text, (sizeof(text)/sizeof(text[0])) - 1, codify);
You really, really cannot pick this up as you go along without having a very good grounding in the basics of the c++ language, which is obvious you do not have. You need to start with the very basics and progress one step at a time. You are trying to undertake the triple jump without knowing how to run.Quote:
Well, did you read some books about progamming with C++ for Windows?
Did you try some simple sample applications like "Hello Worlds"?
Some others a little more complicated?
Note, that you cannot from the level "zero" jump to a level for programming serial port communications...
OK thank you very much! Also if the problem is to send this packet: 0x01, 0x01, 0x01 , 0x00, 0x00, 0x00, 0x01, 0x00, 0x04, 0x01, 0x00. Not an Array of char.
The definiton for StuffData is
This says that ptr is a pointer to memory containing unsigned characters and that the characters pointed to cannot be changed. An area of memory containing unsigned characters is a character array (or an array of chars). So to use StuffData the first parameter has to be a pointer to an array of chars. So before you call the function StuffData, your program has to put into a char array the characters for StuffData. You cannot get away from not using a character array! This can be either a fixed size array (as in my previous example) or it can be a dynamically allocated memory (use new). However the character array is created, once it has been you need to populate it with the characters for StuffData.Code:void StuffData(const unsigned char *ptr, unsigned long length, unsigned char *dst)
So from your example, you want to use 11 characters. So you need a character array that contains these characters set up before you call StuffData.
Code:const unsigned char packet[] = {0x01, 0x01, 0x01 , 0x00, 0x00, 0x00, 0x01, 0x00, 0x04, 0x01, 0x00};
unsigned char codify[200];
StuffData(packet, sizeof(packet) / sizeof(packet[0]), codify);
Ok I thank you very much.
the follow code ran with success, but I'd like to see the encoding packet. I have thought to use printf, is it correct?
Code:int main()
{
const unsigned char text[] = {0x01, 0x01, 0x01 , 0x00, 0x00, 0x00, 0x01, 0x00, 0x04, 0x01, 0x00};
unsigned char codify[200];
StuffData(text, (sizeof(text)/sizeof(text[0])), codify);
//printf(...);
return 0;
}
I tried before to write!!! but it doesn't work (I have errors when it compiles or i read very strange characters) and for this reason I want to know if it is correct...
This code show strange characters without errors:
Quote:
int main()
{
const unsigned char text[] = {0x01, 0x01, 0x01 , 0x00, 0x00, 0x00, 0x01, 0x00, 0x04, 0x01, 0x00};
const int p= sizeof(text);
unsigned char codify[p];
StuffData(text, (sizeof(text)/sizeof(text[0])), codify);
printf("%s", codify);
return 0;
}
Pardon...this is the code:
Code:int main()
{
const unsigned char text[] = {0x01, 0x01, 0x01 , 0x00, 0x00, 0x00, 0x01, 0x00, 0x04, 0x01, 0x00};
unsigned char codify[200];
StuffData(text, (sizeof(text)/sizeof(text[0])), codify);
printf("%s", codify);
return 0;
}
there aren't any errors with this code.
The destination characters returned by StuffData in dst are NOT a c-style string and are NOT guaranteed to be NULL terminated. So using printf (or cout) to try to display the contents of dst is a non-starter. Have you actually looked at what StuffData does? It takes an array of char and a length and returns a codified char array where the first char is the number of bytes (including itself) followed by the chars. If the length of the bytes exceeds 255 then a new chuck is started again with the first byte as the length byte. So "qwerty" gets codified as 0x07 'q' 'w' 'e' 'r' 't' 'y'. 0x00 is treated specially and just gets translated as a new chuck of length 1. So after using StuffData, you pass dst to whatever function you use to actually send the data.
I'd say neither the initial buffer nor the destination one is "a c-style string". Both are just byte sequences with some predefined length.
And there is nothing wrong to use printf (or cout) to output their contents. Just use some proper format specification (like %X, "%08X", "%d" or similar) and do output in a loop for each element in the buffer!