-
January 13th, 2018, 12:37 PM
#1
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
-
January 14th, 2018, 04:51 AM
#2
Re: Reinterpret cast not showing correctly
In what system did you test this?
Could you post the complete code example?
Victor Nijegorodov
-
January 14th, 2018, 07:32 AM
#3
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)
-
January 14th, 2018, 08:08 AM
#4
Re: Reinterpret cast not showing correctly
Originally Posted by binary2
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|