Socket question: How to send different datatypes in a byte array ?
-
Hi, I think my subject says most of it. I am a little stuck with this problem. What must I do if I want e.g. send a few normal chars and then an Integer ? I think I must somehow pack it all into a byte array. But how ? Hope someone can help me. Thanks
-
Hi, I think my subject says most of it. I am a little stuck with this problem. What must I do if I want e.g. send a few normal chars and then an Integer ? I think I must somehow pack it all into a byte array. But how ? Hope someone can help me. Thanks
something like this?
BYTE bytes[100];
int anInt = 10, anotherInt = 50;
char *str = "Hi there";BYTE *pPos = bytes;
memcpy(pPos, anInt, sizeof(int));
pPos += sizeof(int);strcpy(pPos, str, strlen(str));
pPos += strlen(str);memcpy(pPos, anotherInt, sizeof(int));
pPos += sizeof(int);image processing toolkits | batch image processing | blogging
-
Hi, I think my subject says most of it. I am a little stuck with this problem. What must I do if I want e.g. send a few normal chars and then an Integer ? I think I must somehow pack it all into a byte array. But how ? Hope someone can help me. Thanks
copying an integer value inside a byte array (an array of unsigned char, I suppose) is simple, for instance:
int i=5;
unsigned char buff[0x100];
memcpy((void*)buff, (void *)&i, sizeof(i));but you must be aware that INTEL processors have little-endian byte order, while TCP/IP network has big-endian one. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
copying an integer value inside a byte array (an array of unsigned char, I suppose) is simple, for instance:
int i=5;
unsigned char buff[0x100];
memcpy((void*)buff, (void *)&i, sizeof(i));but you must be aware that INTEL processors have little-endian byte order, while TCP/IP network has big-endian one. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
Hi, thanks that helped me. I already thought that it's something like memcpy. But there's my next question. How can I get then from this byte array the original values back ? Btw when I send data with TCP/IP, do I have to send it was unicode (TCHAR) or normal (unsigned char) ?
-
Hi, thanks that helped me. I already thought that it's something like memcpy. But there's my next question. How can I get then from this byte array the original values back ? Btw when I send data with TCP/IP, do I have to send it was unicode (TCHAR) or normal (unsigned char) ?
FreeCastle wrote:
I get then from this byte array the original values back
if you, at the opposite side of the TCP line, don't know what is the nature of the data contained inside the byte array then you are lost. So maybe you have to preceede each chunk of data with, say, a marker section (maybe few bytes) that state: (1) the type of the following items (2) the lenght of the following items Hey, you're establishing a protocol! :)
FreeCastle wrote:
Btw when I send data with TCP/IP, do I have to send it was unicode (TCHAR) or normal (unsigned char) ?
When you are sending bulk data maybe BYTES (hence unsigned char) are more appropriate. :) hope that helps.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
FreeCastle wrote:
I get then from this byte array the original values back
if you, at the opposite side of the TCP line, don't know what is the nature of the data contained inside the byte array then you are lost. So maybe you have to preceede each chunk of data with, say, a marker section (maybe few bytes) that state: (1) the type of the following items (2) the lenght of the following items Hey, you're establishing a protocol! :)
FreeCastle wrote:
Btw when I send data with TCP/IP, do I have to send it was unicode (TCHAR) or normal (unsigned char) ?
When you are sending bulk data maybe BYTES (hence unsigned char) are more appropriate. :) hope that helps.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
Hey thanks a lot again. And you're right, I am writing in fact a little protocol :-)