Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4539234
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T14:55:07+00:00 2026-05-21T14:55:07+00:00

I have an int[2] representation of a long int in a 32 bit machine

  • 0

I have an int[2] representation of a long int in a 32 bit machine and want to convert it to long on 64bit machine. is there a safe architecture independent way of doing this conversion?

The source machine is 32bit and an int is 32bits. Destination machine is 64bit and the long long type is definitely 64bits.

can I do the following?

long i;
int j[2];
#ifdef LITTLEENDIAN
j[1] = *(int*)(&i);
j[0] = *(((int*)(&i))+1)
#else
j[0] = *(int*)(&i);
j[1] = *(((int*)(&i))+1)
#endif

If the above is incorrect, then what is the best and safest way for this? I am sure this would have been asked previously, but I didn’t find a clean answer.

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T14:55:08+00:00Added an answer on May 21, 2026 at 2:55 pm

    I have an int[2] representation of a long int in a 32 bit machine and want to convert it to long on 64bit machine. is there a safe architecture independent way of doing this conversion?

    Not really. Because apart from endianness, the sizes of the two datatypes may vary as well. On some popular platforms, int and long have the same size (both 32 bits)

    Ultimately, it depends on how you created your int[2] representation. Whatever you did to create that int array has to be reversed in order to get a valid long out of it.

    One approach which will work in practice (but is, technically speaking, undefined behavior), is to place both in a union:

    union {
      int i2[2];
      long l;
    } u;
    

    Now you can simply write to u.i2 and read from u.l. The C++ standard technically doesn’t allow this (it is undefined behavior), but it is such a common trick that major compilers explicitly support it anyway.

    However, a better approach might be to use a char[] instead of int[], because char‘s are explicitly allowed to alias other types.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a long int which represents a Unxi timestamp and want to use
I have tried this way to get the binary representation in C# int i
Possible Duplicate: Floating point error in representation? I have problem with this code int
I have a long number. Now what I want is following (given in pseudo
I have a graph representation such that each nodes contains (int id, String categoryName)
I have to convert a binary number like for example unsigned int bin_number =
Let's say I have int a() { /* Tons of code ....*/ return someInt;
I have an int property in my viewmodel that returns a custom class property.
I have an int and float array of length 220 million (fixed). Now, I
i have two int array of images , i have implemented it in grid

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.