Jump to content
  • Advertisement
Sign in to follow this  
NightMarez

a simple stupid question :)

This topic is 5104 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay i got this simple (and stupid) question i got a char * data contains several bytes.. lets say 8 (size changes though), would be 01 02 03 04 05 06 07 08 okay, i need to run a switch on it, like switch(data) { case 0x01: blabla; return; }; now, i need to get the first byte only, (0x01) to check it.. like data[0], but that wont work (or atleast aint working here). anyone got an idea of super quickly getting the first byte only into the swich check ? Thanks

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
data[index] should work, what error are you getting?

Share this post


Link to post
Share on other sites
Subscripting should definately work, provided your pointer is valid...

#include <iostream>
using namespace std;

int main()
{
char buff [] = { 0x0, 4, 3, 1, 6, };
char* ptr = buff;
switch(ptr[0])
{
case 0x0:
cout << "Zero" << endl;
break;
default:
cout << "Nonzero" << endl;
break;
}
return 0;
}


Share this post


Link to post
Share on other sites
Chris Hare is right. When you did
switch (data)
you were actually switching based on the value of "data", which is simply a pointer. What you want to switch on is what data points to, which is
data[0]
or equivalently
*data

Share this post


Link to post
Share on other sites
still aint working, here is what i got so far


void checkout(char* buffer)
{
char * ptr = buffer;

switch(ptr[0])
{
// general stuff
case 0x67:
do something
break;
default:
do something else
break;
};
}



now, if my buffer looks like this

67 00 00 01 20 22 02 00
it will use default

if my buffer looks like this
67
it will use the case 0x67

Share this post


Link to post
Share on other sites
0x67 is very different to 67. 67 is, well, 67 in base 10 [decimal], but 0x67 is in base 16 [hexadecimal], or 6 * 16 + 7 * 1 = 103 in decimal. So, just use plain 67.

Share this post


Link to post
Share on other sites
im guessing i need a workaround, since i need to compare hex values.


case 0x67
case 0x68

and so on.

so my guess is, i need to convert the char * buffer to a int * buffer or something.

Share this post


Link to post
Share on other sites
0x67 will fit into a char var, it's just that you need to ensure you write 0x67 to the array and not 67. That is, if you are using hexadecimal, use it all the way through your relevant code.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!