?
Solved

Converting a DWORD into a CHAR array

Posted on 2004-05-02
7
Medium Priority
?
1,986 Views
Last Modified: 2007-12-19
Ok, I'm going to try to be as clear as possible:

I've got a DWORD, with a value in, which I'm going to assume is 0x441C0010 for this example (it can be anything though). I need to convert it so that it becomes a series of 4 bytes in a char array,so that the first 2 bytes, are the first character's ascii code, the 2nd two are the second character's ascii code etc.

E.g.:

0x441C0010 would become:
myChar[0] = 0x44
myChar[1] = 0x1C
myChar[2] = 0x00
myChar[3] = 0x10

another example:

0x12345678 would become:
myChar[0] = 0x12
myChar[1] = 0x34
myChar[2] = 0x56
myChar[3] = 0x78

Note, those are single characters, and I'd like it done in fairly basic C++ (if possible!).

Many many thanks if you can help!

eAi
0
Comment
Question by:eAi2k
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
7 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 375 total points
ID: 10971362
DWORD dw;
char myChar [4];

DWORDToChar(&dw, myChar);

void DWORDToChar(DWORD* pDWORD, char* pChar)
{
    char* p = (char*) pDWORD;

    for ( int i = 0; i < 4; i++ )
        *pChar++ = *p++;
}
0
 
LVL 1

Author Comment

by:eAi2k
ID: 10972364
I managed to fix this problem myself very simply, by just doing:

char myVariable[3]
myVariable[3] = myDword;
myVariable[2] = (myDword >> 8);
myVariable[1] = (myDword >> 16);
myVariable[0] = (myDword >> 24);

eAi
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 10972668
>> char myVariable[3]

You need at least an array of 4 char.

So, change it to

   char myVariable[4]

Regards, Alex


0
 
LVL 39

Assisted Solution

by:itsmeandnobodyelse
itsmeandnobodyelse earned 375 total points
ID: 10972731
You may also do this:

     DWORD  dw      = 0x12345678;
     unsigned char* pc = (unsigned char*)&dw;
     unsigned char  c[4];
     c[3] = pc[0];
     c[2] = pc[1];
     c[1] = pc[2];
     c[0] = pc[3];

Your request is equivalent to turn the byte order of the storage from little-endian to big-endian.

So you may use sockets helper function htonl that made the same

     DWORD  dw      = 0x12345678;
     dw = htonl(dw);
     unsigned char* pc = (unsigned char*)&dw;

Regards, Alex


   

0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question