Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Storing bytes of an integer in a character array

Author
Message
Michael P
18
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 1st Jul 2008 18:09
I am trying to store the individual bytes of an integer in a character array and then take the bytes that are in the character array and put them back into a new integer.

The reason for this is that in networking (using winsock), data is sent in char form. Obviously, I could just convert the integer to character form so that each number is one character e.g. 5124 would appear as C0 = 5, C1 = 1, C2 = 2, C3 = 4; C representing character and each number representing the element. The problem with this is that large numbers fill more space than is necessary; storing the actual bytes of an integer would always store 4 bytes regardless of the size of the number.

Here is my first attempt which should demonstrate what I'm attempting to do. There are several problems with this; firstly, large numbers appear wrong and secondly on exit the following run time error occurs "Stack around the variable 'iIntReturn' was corrupted". I'm not sure what I'm doing wrong, or if I'm going about this the right way:
Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 1st Jul 2008 18:13 Edited at: 1st Jul 2008 18:13


jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 1st Jul 2008 18:19
That is a great solution - and here is a similiarly useful construct example I use for RGBA colors. Notice the UNION in the header.

In short - You can make "two structures" point to the same place in memory like Benjamin did, and access any and each part individually. Note that the compiler throws warnings but union structs have been in existance since (bell labs, AT&T) wrote the first c compiler.


jfc_rgba.h


jfc_rgba.cpp


Michael P
18
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 1st Jul 2008 18:21
that's awesome, thanks Benjamin.

Working code:
Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 1st Jul 2008 18:26
Watch out, as the array subscript references by chars, not integers, so effectively you should store at [0] followed by [4], then [8], etc.

Michael P
18
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 2nd Jul 2008 17:55
Urgh, I have a problem Things are fine when there is just one integer involved but with two or more the numbers returned are not the numbers entered into the buffer. It does allow enough space for each integer (4 bytes) and I've spent a good hour trying to figure out what is wrong (although it is probably something very obvious ). Here is the code:
Benjamin
21
Years of Service
User Offline
Joined: 24th Nov 2002
Location: France
Posted: 2nd Jul 2008 18:03
What's the loop for? If you're only storing/retrieving a single 4-byte integer you can use the code I posted.

Michael P
18
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 2nd Jul 2008 18:23
Ohh I see how it works now, I assumed that "*(int*)&sBuffer[n] = Add;" only stored one byte at a time when in fact it does all 4.

Thanks again Ben!

Correct code (hopefully):

Login to post a reply

Server time is: 2024-09-30 01:37:42
Your offset time is: 2024-09-30 01:37:42