Click here to Skip to main content
15,799,257 members
Please Sign up or sign in to vote.
5.00/5 (3 votes)
See more:
I started struggling with something I didn't think would be that complicated, so I would really appreciate some advice. I will tell you my situation.

I write data to a device/card. The writing procedure is easy and goes usually like this:

unsigned char sendBuffer[32];

sendBuffer[0] = headerInfo;
sendBuffer[1] = headerInfo;
sendBuffer[2] = data[0];
sendBuffer[3] = data[1];
sendBuffer[17] = data[15];

The data I write is usually 16 bytes, or multiple of 16. So, storing is easy, I just call: SendData(sendBuffer, length), and done, data is stored.

1) My question is say I want to store now integer 517 on the card - what data shall I send to the device? How to embed it in the sendBuffer array?
2) Similarly. Say I want to send string "Hello world xyz" to the device. How to embed it in the sendBuffer?

Some highlights. I am the receiver end. I will be reading the data back. And also depending where I stored it I may in advance know whether it is an int or string.
I also don't have endianness issues.

Below I will just provide working example which manages to write and read 16 byte HEX strings and hope this will be helpful to you to answer my questions better.

Say I want to write this string: "00000000000000000000AABBEEAABBEE"

We convert it to byte array:

for(int i=0,m=0; i < size; i+=2,m++)
        char ch1, ch2;
        ch1=(char)str[i]; // str is our hex string
        int dig1, dig2;
        if(isdigit(ch1)) dig1 = ch1 - '0';
        else if(ch1>='A' && ch1<='F') dig1 = ch1 - 'A' + 10;
        else if(ch1>='a' && ch1<='f') dig1 = ch1 - 'a' + 10;
        if(isdigit(ch2)) dig2 = ch2 - '0';
        else if(ch2>='A' && ch2<='F') dig2 = ch2 - 'A' + 10;
        else if(ch2>='a' && ch2<='f') dig2 = ch2 - 'a' + 10;
 array1[m] = (char)(dig1*16 + dig2); // output byte array

Now, we copy this byte array array1 to the sendBuffer as above, e.g.,

for(int i=0;i<16;i++)

and done we call send command as I mentioned before.

Reading is also easy, I call: Receive(receiveBuffer) and now receiveBuffer contains the byte array I wrote previously. Then I need to convert it back to hex like this:


And done, data now contains my initial hex string: "00000000000000000000AABBEEAABBEE".

1 solution

Embedding a integer or a (short) string is simple.
You may use 4 (or 2) bytes for storing each integer. Suppose you choose 4 bytes an integer, then
int k = whatever; // assuming 32-bit integers
sendbuffer[2] = (unsigned char)(k >> 24);
sendbuffer[3] = (unsigned char)(k >> 16);
sendbuffer[4] = (unsigned char)(k >> 8);
sendbuffer[5] = (unsigned char) k;

While for a simple string
const char * s = "hello";
int n;
// TODO: check if it fits
for (n=0; n=strlen(s); ++n) 
  sendbuffer[2+n] = s[n];
s[n] = '\0';

Of course you have to specify somehow what sendbuffer contains (you could use the headerinfo bytes for the purpose, I suppose).
Share this answer
_coder 20-Sep-13 5:36am    
CPallini thank you. I think I may not need to specify what sendBuffer contains, as I write the data and I read back(I know where I will store what). About string example I was thinking memcpy could also work, to memcpy string (e.g., "hello world") to sendBuffer, right? The only problem I can think of now is that sendBuffer must contain 16 bytes or multiple of 16. If string I want to write is say 11 bytes, I would probably have to pad it right?
CPallini 20-Sep-13 6:26am    
You don't need to pad it: just add the '\0' terminator, like I did (or prefix it with its length).
_coder 24-Sep-13 4:43am    
CPallini, what if I want to embed say two integers into the buffer, how do I do this? Similarly to your code? (e.g.,start embedding second number from index 6 of buffer) Are there any gotchas?? Thank you.
CPallini 24-Sep-13 4:50am    
Yes, you may do that. In the header you could specify how many valid ints are there.

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

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900