Author | Message | Time |
---|---|---|
bethra | I've tried several things that I thought that might work, but I don't think they do. Here is my DebufDWORD() function [code] DWORD debuffer::DebufDWORD() { DWORD debDWORD; char remDWORD[sizeof(DWORD)]; memcpy(remDWORD, debuf + pos, 4); debDWORD = *remDWORD return debDWORD; } [/code] 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. | December 14, 2004, 12:09 AM |
shadypalm88 | [quote author=Sorc.Polgara link=topic=9895.msg92265#msg92265 date=1102982965] I've tried several things that I thought that might work, but I don't think they do. Here is my DebufDWORD() function [code] DWORD debuffer::DebufDWORD() { DWORD debDWORD; char remDWORD[sizeof(DWORD)]; memcpy(remDWORD, debuf + pos, 4); debDWORD = *remDWORD return debDWORD; } [/code] 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. [/quote]Looks like you're thinking in VB. [code] DWORD debuffer::DebufDWORD() { DWORD dwTemp = *(DWORD*) (debuf + pos); pos += sizeof(DWORD); return dwTemp; }[/code] | December 14, 2004, 12:16 AM |
Mephisto | 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. | December 14, 2004, 1:04 AM |
Adron | [quote author=Mephisto link=topic=9895.msg92277#msg92277 date=1102986296] 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. [/quote] 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: [code] DWORD debDWORD; memcpy(&debDWORD, debuf + pos, 4); pos += 4; return debDWORD; [/code] And then we can all ignore that irrelevant post of yours (which would've been better not posted at all?). | December 14, 2004, 7:56 AM |
Mephisto | 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... | December 14, 2004, 3:05 PM |
Myndfyr | [quote author=Mephisto link=topic=9895.msg92373#msg92373 date=1103036700] 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... [/quote] 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. | December 14, 2004, 5:32 PM |
Mephisto | Thought he was talking to me since he was describing my post (static cast, etc.). | December 14, 2004, 7:58 PM |
Eibro | [quote author=MyndFyre link=topic=9895.msg92393#msg92393 date=1103045562] [quote author=Mephisto link=topic=9895.msg92373#msg92373 date=1103036700] 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... [/quote] 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. [/quote]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. | December 14, 2004, 9:11 PM |
Mephisto | Bleh, forgot to do do the pointer deferencing with my typecast. :\ | December 14, 2004, 11:34 PM |
OnlyMeat | [quote author=Sorc.Polgara link=topic=9895.msg92265#msg92265 date=1102982965] I've tried several things that I thought that might work, but I don't think they do. Here is my DebufDWORD() function [code] DWORD debuffer::DebufDWORD() { DWORD debDWORD; char remDWORD[sizeof(DWORD)]; memcpy(remDWORD, debuf + pos, 4); debDWORD = *remDWORD return debDWORD; } [/code] 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. [/quote] 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:- [code] DWORD dwResult; memcpy( &dwResult, debuf + pos, sizeof(DWORD) ); [/code] 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:- [code] debDWORD = *remDWORD [/code] so a corrected version of your code could be represented as follows:- [code] DWORD debuffer::DebufDWORD() { DWORD dwResult; memcpy( &dwResult, debuf + pos, sizeof(DWORD) ); return dwResult; } [/code] These are common mistakes when one tries to learn c++, continue the good work! | December 14, 2004, 11:44 PM |
Zakath | I agree with Eibro...there's no need to involve memcpy. Just dereference the pointer and copy by value... | December 15, 2004, 12:26 AM |
tA-Kane | if you don't like using pointers and all that, you can use a simple union. [code]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; }[/code] just an alternative to typecasting, if you're ewwy++ with pointers | December 15, 2004, 7:58 PM |
Adron | [quote author=Mephisto link=topic=9895.msg92404#msg92404 date=1103054328] Thought he was talking to me since he was describing my post (static cast, etc.). [/quote] 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? | December 15, 2004, 10:30 PM |