0
int x = 1231212;
memcpy(pDVal, &x, 4);
int iDSize = sizeof(double);
int i = 0;
for (; i<iDSize; i++)
{
    char c;
    memcpy(&c, &(pDVal[i]), 1);
    printf("%d|\n", c);
    printf("%x|\n", c);

}

I used above code segment to print the hex value of each byte of a Integer. But that is not working properly. What is issue here ?

3
  • How is pDVal declared? Commented Jun 8, 2011 at 9:20
  • The code is not clear. What are you trying to achieve? Maybe you need sprintf? Commented Jun 8, 2011 at 9:21
  • 4
    this is tagged C++, do it the C++ way: std::cout << std::hex << x << std::endl; that will print the hex value of x. Commented Jun 8, 2011 at 9:22

5 Answers 5

1

Try something like this:

void Int32ToUInt8Arr( int32 val, uint8 *pBytes )
{
  pBytes[0] = (uint8)val;
  pBytes[1] = (uint8)(val >> 8);
  pBytes[2] = (uint8)(val >> 16);
  pBytes[3] = (uint8)(val >> 24);
}

or perhaps:

UInt32 arg = 18;
array<Byte>^byteArray = BitConverter::GetBytes( arg);
// {0x12, 0x00, 0x00, 0x00 }

byteArray->Reverse(byteArray);
// { 0x00, 0x00, 0x00, 0x12 }

for the second example see: http://msdn2.microsoft.com/en-us/library/de8fssa4(VS.80).aspx

Hope this helps.

Sign up to request clarification or add additional context in comments.

3 Comments

The question didnt mention C++/CLI anywhere.
yes, it's tagged as C++, not "C++/CLI". These are two different languages.
Uhm, this is wrong. No idea why it was accepted but is doesn't do what the question asks (not even remotely) as you don't actually print anything and don't use standardised C++ (as others have noted). Note that '0' != 0. No matter what Byte is, if you print this straight forward it will produce the wrong outcome, as 31 wont be printed as 1F but as 31.
0

Just use the sprintf function. You will get a char*, so you have your array. See the example on the webpage

2 Comments

itoa is a non-standard function. Consider using sprintf instead.
I'm kinda tempted of downvoting this since he tagged it as c++ and you are providing a c answer. But maybe the OP does not know what he wants since his code is also c...
0

Your code looks awful. That's it.

memcpy(pDVal, &x, 4);

What is pDVal? Why do you use 4? Is it sizeof(int)?

int iDSize = sizeof(double);

Why sizeof(double)? May be you need sizeof(int).

memcpy(&c, &(pDVal[i]), 1); makes copy first byte of i-th array pDVal element.

printf("%d|\n", c); is not working because "%d" is waiting integer.

2 Comments

I agree with the rest of the comments, but the last is not a problem. Passing a char as a vararg will result in integral promotion taking place, and the char will be converted to int (possibly not with the value he expects---on a lot of machines, char is signed).
"%d" is waiting integer ?? So, he's passing a char.
0

Print like this:

printf("%d|\n", c & 0xff);
printf("%x|\n", c & 0xff);

Comments

0

If you are serious about the , this is how I would suggest to do it.

#include <sstream>

template <typename Int>
std::string intToStr(Int const i) {
  std::stringstream stream;
  stream << std::hex << i;
  return stream.str();
}

Which you may invoke as intToStr(1231212). If you insist on getting a char array (I strongly suggest you use std::string), you can copy the c_str() result over:

std::string const str = intToStr(1231212);
char* const chrs = new char[str.length()+1];
strcpy(chrs,str.c_str()); // needs <string.h>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.