CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2005
    Posts
    63

    reinterpret cast

    Hi,
    Since I need to send a long variable through a network. I need to convert it to a char * first. I tried to use reinterpret_cast without success.
    Code:
    long numBytes = 0;
    char *test;
    char sendData = 0x00;
    
    numBytes = ftell( stream );
    test = reinterpret_cast<char *>(&numBytes);
    sendData = test[0];
    I am using visual studio and this is what the debugger shows me:
    numBytes 0x000058d7 long
    &numBytes 0x0107fcb4 long *
    test[0] 0xd7 '×' char
    test[1] 0x58 'X' char
    test 0x0107fcb4 "×X" char *

    It seems test has the correct pointer but then I want to retrieve 58 and d7 from the original long variable but instead test[0] and test[1] gives me Xs. Can someone explain what I am doing wrong. Thanks a lot
    Amish

  2. #2
    Join Date
    Oct 2005
    Location
    Seattle, WA U.S.A.
    Posts
    353

    Re: reinterpret cast

    Hi '284 !

    You ask what you're doing wrong .... beats me. Your code works for me. I copied it to VS2003 Managed C++.

    I did a cut and paste with the following exceptions:

    1) I changed the initial value of numBytes to 81203 (decimal)
    2) I commented out the "numBytes = ftell( stream )" statement

    I executed the code and at the end of the function, 'SendData' contained a decimal 51 (0x33). The hex value of NumBytes (81203.) is 0x13D33, so it looks to me like it worked.

    Just this moment changed the "Test[0]" to "Test[1]" and 'SendData' now contains a decimal 61 (0x3D) which would be the next byte in the 'array'.

    It kinda looks like it's workin'

    bill

  3. #3
    Join Date
    Jan 2005
    Posts
    63

    Re: reinterpret cast

    Ok i am offially brain dead Thanks for noticing that though. You're right, I was looking at it wrong. I guess I got wrapped up into which is a pointer and which is actual data in the debugger window. Thanks a lot. You saved me hours of googling.
    Amish

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured