• Welcome to Valhalla Legends Archive.
 

Converting string/chars to DWORD, WORD, BYTE datatypes

Started by Sorc.Polgara, December 13, 2004, 06:09 PM

Previous topic - Next topic

Sorc.Polgara

I've tried several things that I thought that might work, but I don't think they do.
Here is my DebufDWORD() function

DWORD debuffer::DebufDWORD()
{
DWORD debDWORD;
char remDWORD[sizeof(DWORD)];

memcpy(remDWORD, debuf + pos, 4);
        debDWORD = *remDWORD

return debDWORD;
}

now it only seems to be converting the first character of the 4 byte, 4 char string.  It seems logical enough, however if you can turn a DWORD into a 4 byte string, then you should be able to turn a 4 byte string or 4 char string into a DWORD.

shadypalm88

Quote from: Sorc.Polgara on December 13, 2004, 06:09 PM
I've tried several things that I thought that might work, but I don't think they do.
Here is my DebufDWORD() function

DWORD debuffer::DebufDWORD()
{
DWORD debDWORD;
char remDWORD[sizeof(DWORD)];

memcpy(remDWORD, debuf + pos, 4);
        debDWORD = *remDWORD

return debDWORD;
}

now it only seems to be converting the first character of the 4 byte, 4 char string.  It seems logical enough, however if you can turn a DWORD into a 4 byte string, then you should be able to turn a 4 byte string or 4 char string into a DWORD.
Looks like you're thinking in VB.

DWORD debuffer::DebufDWORD() {
DWORD dwTemp = *(DWORD*) (debuf + pos);
pos += sizeof(DWORD);
return dwTemp;
}

Mephisto

#2
If you just want to convert a string to a DWORD use: static_cast<DWORD>(atoi(theStringToBeConverted)); and same with a word with a static cast of type WORD.  Code is untested, but even if it doesn't work you should still be able to figure something out.  Also, you can't convert a string to type BYTE (BYTE is typedef unsigned char BYTE).  It's a single character in other words, so it doesn't make sense to want to take a multi-character string and convert it to BYTE, it's not possible.

Note: Using atoi() will just convert numeral characters to numerals.  If the function encounters a non-numeric character it will return 0 (IIRC).  If it doesn't, *(DWORD *)string should work.

Adron

Quote from: Mephisto on December 13, 2004, 07:04 PM
If you just want to convert a string to a DWORD use: static_cast<DWORD>(atoi(theStringToBeConverted)); and same with a word with a static cast of type WORD.  Code is untested, but even if it doesn't work you should still be able to figure something out.  Also, you can't convert a string to type BYTE (BYTE is typedef unsigned char BYTE).  It's a single character in other words, so it doesn't make sense to want to take a multi-character string and convert it to BYTE, it's not possible.

Note: Using atoi() will just convert numeral characters to numerals.  If the function encounters a non-numeric character it will return 0 (IIRC).  If this is a problem doing (DWORD)string should work.

Doing (DWORD)string will get you the pointer value, nothing useful. You have provided flawed advice so many times that I think you should start testing your ideas before posting them.

There's no absolute need for the static_casts. For a DWORD, compilers typically manage casting an int to an unsigned int by itself just fine. The cast to word will be useful to avoid a warning. You could also use strtoul to get a DWORD from the start. If he's after what you are suggesting, converting to a BYTE would make perfect sense too. Many 3 digit strings can be turned into a BYTE just fine. It handles values up to 255...

However, given what Sorc.Polgara has been up to before, I find it doubtful that converting digits is the wanted solution. Luckily shadypalm88 has already provided something that works for a packet buffer. I'll just add the memcpy version:


DWORD debDWORD;
memcpy(&debDWORD, debuf + pos, 4);
pos += 4;
return debDWORD;


And then we can all ignore that irrelevant post of yours (which would've been better not posted at all?).

Mephisto

#4
Then delete it *shrug*
And maybe you should point out my irrelevant suggestions?  I made one blatantly bad statement and you say that I provide irrelevant advice on a regualar basis?  I'm sorry I make mistakes and no one tells me...

MyndFyre

Quote from: Mephisto on December 14, 2004, 09:05 AM
Then delete it *shrug*
And maybe you should point out my irrelevant suggestions?  I made one blatantly bad statement and you say that I provide irrelevant advice on a regualar basis?  I'm sorry I make mistakes and no one tells me...

Looking at what was said, I believe Adron was speaking to Eibro, who had said:

Well nevermind, the post of him complaining that it was deleted was also deleted.  But I remember thinking that Adron was talking to Eibro, not you Mephisto.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Mephisto

Thought he was talking to me since he was describing my post (static cast, etc.).

Eibro

Quote from: MyndFyre on December 14, 2004, 11:32 AM
Quote from: Mephisto on December 14, 2004, 09:05 AM
Then delete it *shrug*
And maybe you should point out my irrelevant suggestions?  I made one blatantly bad statement and you say that I provide irrelevant advice on a regualar basis?  I'm sorry I make mistakes and no one tells me...

Looking at what was said, I believe Adron was speaking to Eibro, who had said:

Well nevermind, the post of him complaining that it was deleted was also deleted.  But I remember thinking that Adron was talking to Eibro, not you Mephisto.
He couldn't have been talking to me. The answer I provided was correct. Here, I'll post it again. DWORD data = *(DWORD*)charbuf;

There is nothing wrong with that. Nor was there with my n previous posts. If someone has a problem with me i'd appreciate if they told me.
Eibro of Yeti Lovers.

Mephisto

Bleh, forgot to do do the pointer deferencing with my typecast.  :\

OnlyMeat

Quote from: Sorc.Polgara on December 13, 2004, 06:09 PM
I've tried several things that I thought that might work, but I don't think they do.
Here is my DebufDWORD() function

DWORD debuffer::DebufDWORD()
{
DWORD debDWORD;
char remDWORD[sizeof(DWORD)];

memcpy(remDWORD, debuf + pos, 4);
        debDWORD = *remDWORD

return debDWORD;
}

now it only seems to be converting the first character of the 4 byte, 4 char string.  It seems logical enough, however if you can turn a DWORD into a 4 byte string, then you should be able to turn a 4 byte string or 4 char string into a DWORD.

There are a few mistakes you are making here, firstly you dont need to use an intermediate char array for extracting a dword from your buffer try this instead:-


DWORD dwResult;
memcpy( &dwResult, debuf + pos, sizeof(DWORD) );


The second problem is related to the first in the fact that there is no built in binary '=' operator in c++ for copying the contents of a char array to a DWORD therefore this expression will just copy the first element in the array to the dword:-


        debDWORD = *remDWORD


so a corrected version of your code could be represented as follows:-


DWORD debuffer::DebufDWORD()
{
DWORD dwResult;
memcpy( &dwResult, debuf + pos, sizeof(DWORD) );

return dwResult;
}


These are common mistakes when one tries to learn c++, continue the good work!

Zakath

I agree with Eibro...there's no need to involve memcpy. Just dereference the pointer and copy by value...
Quote from: iago on February 02, 2005, 03:07 PM
Yes, you can't have everybody...contributing to the main source repository.  That would be stupid and create chaos.

Opensource projects...would be dumb.

tA-Kane

if you don't like using pointers and all that, you can use a simple union.

typedef union {
  DWORD dwValue;
  char cValue[5];
} DWORDConverter;

int main(void){
  DWORDConverter Converter;
 
  Converter.dwValue = 'IX86';
  Converter.cValue[4] = 0;//need null byte to end the cstr
  printf("%s", Converter.cValue);
  return 0;
}


just an alternative to typecasting, if you're ewwy++ with pointers
Macintosh programmer and enthusiast.
Battle.net Bot Programming: http://www.bash.org/?240059
I can write programs. Can you right them?

http://www.clan-mac.com
http://www.eve-online.com

Adron

Quote from: Mephisto on December 14, 2004, 01:58 PM
Thought he was talking to me since he was describing my post (static cast, etc.).

Yes, I was talking to you. You seemed to have improved there for a while, giving valid advice. And then you come here and post that?