CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2017
    Posts
    41

    Reinterpret cast not showing correctly

    I have the following:

    ctx->state0 is a 200 byte array

    I'm using reinterpret cast to convert the array to 32 bit values and to 64 bit values instead of 8 bits

    uint64_t* h0 = reinterpret_cast<uint64_t*>(ctx->state0);
    uint32_t* h00 = reinterpret_cast<uint32_t*>(ctx->state0);

    cout << h0[1] << endl;
    cout << h00[3]<< endl;

    for h0[0] the values in h00[0] and h00[1] corespond
    for h0[1] the values in h00[2] and h00[3] don't corespond

    h0[1] = 1011010100110110101011001111011100011111101011010001011010000
    h00[2] = 11100111100110000101110000100111
    h00[3] = 11100010100001000101100000110100

  2. #2
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Reinterpret cast not showing correctly

    In what system did you test this?
    Could you post the complete code example?
    Victor Nijegorodov

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Reinterpret cast not showing correctly

    As Victor requests, would you post your code example.

    However, consider

    Code:
    #include <cstdint>
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	uint64_t i64 = 0b1011010100110110101011001111011100011111101011010001011010000;
    
    	uint32_t *p32 = reinterpret_cast<uint32_t*>(&i64);
    
    	uint32_t p0 = p32[0];
    	uint32_t p1 = p32[1];
    
    	cout << hex << "i64 " << i64 << "  i32[0] " << p0 << "  i32[1] " << p1 << endl;
    }
    which displays

    Code:
    i64 16a6d59ee3f5a2d0  i32[0] e3f5a2d0  i32[1] 16a6d59e
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Reinterpret cast not showing correctly

    Quote Originally Posted by binary2 View Post
    Reinterpret cast not showing correctly
    Why do you think it is a "reinterpret cast" problem?
    It seems to more likely be a big/little endian problem.
    See also https://betterexplained.com/articles...an-byte-order/
    Victor Nijegorodov

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