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 7446481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T12:19:40+00:00 2026-05-29T12:19:40+00:00

I am trying to get the binary form of an unsigned long long and

  • 0

I am trying to get the binary form of an unsigned long long and store each bit of it in an array.

I have an input file like this:

0000000000000000    0000000000000000
FFFFFFFFFFFFFFFF    FFFFFFFFFFFFFFFF
3000000000000000    1000000000000001

where each entry is a 64-bit integer represented in hex. I am using an unsigned long long to hold this value then iterating over the bits and attempting to store them in an array, but some of the arrays have bits in the wrong position.

Here is what I have:

char key_in[17];
char plaintext_in[17];

//64-bit long variables to hold the 64-bit hex values in the input file
unsigned long long key, plaintext;

//I read an entry from the file with fscanf
fscanf(infile,"%s %s",&key_in, &plaintext_in)

//convert the numbers from hex to unsigned long long with strtoull
key = strtoull(key_in, NULL, 16);
plaintext = strtoull(plaintext_in, NULL, 16);

//initialize arrays with 64 positions that will hold the 
//binary representation of the key and plaintext
int key_arr[64];
int pt_arr[64];

//fill the arrays with the binary representations
//of the plaintext and the key
int64_to_bin_array(key, key_arr, 64);
int64_to_bin_array(plaintext, pt_arr, 64);    

//print both arrays
printArray(key_arr, 64);
printArray(pt_arr,  64);

here are the functions I created int64_to_bin_array and printArray:

/* Converts from an unsigned long long into an array of 
integers that form the binary representation of a */
void int64_to_bin_array(unsigned long long a, int *b, int length)
{
   int i;
   for(i = 0; i < length; i++)
   {
      *(b+i) = (a >> i) & 1; //store the ith bit in b[i]
   }
}

/* prints a one-dimensional array given
   a pointer to it, and its length */
void printArray(int *arr, int length)
{
   int i;
   for(i = 0; i < length; i++)
   {
      printf("%d ", *(arr + i));
   }
   printf("\n\n");
}   

When I print the array for the third input however, I receive an incorrect result:

input (in hex):

1.  3000000000000000    2.  1000000000000001   

output (in binary):

1    00000000 00000000 00000000 00000000 00000000 00000000 00000000 00001100 

2    10000000 00000000 00000000 00000000 00000000 00000000 00000000 00001000

Can anyone see where I have made a mistake?

EDIT

I get the correct output after both reading and printing in reverse, but my problem is I need the array to have its most significant byte first so I can manipulate it. Any ideas how that can be done? Would I have to reassign it to a new array and copy the elements in reverse?

  • 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-29T12:19:41+00:00Added an answer on May 29, 2026 at 12:19 pm

    Try reading it the other way around. Let’s take the last octet:

    00001100 = 0x0C
    00110000 = 0x30 <---
    

    That corresponds yo your first first octet, 0x30.

    For the second number:

    00001000 = 0x08
    00010000 = 0x10 <---
    

    That corresponds to your first first octet, 0x10.

    You’ll probably get what you expect if you print it like this:

    for(i = length - 1; i >= 0; i--)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Reduced to basic form, I'm trying to do something like this with a binary
Trying to get my css / C# functions to look like this: body {
I am trying to parse a simple binary file in Haskell with the Data.Binary.Get
Trying to get this example working from http://www.munna.shatkotha.com/blog/post/2008/10/26/Light-box-effect-with-WPF.aspx However, I can't seem to get
Trying to get comfortable with jQuery and I have encountered some sample code that
Trying to get this to work, with no luck: [DataMember] public Type ParameterType {
Trying to get a value out of 2d array inside of a hash and
Using the HTML5 File API I can get the Binary String representation of a
I have been trying to get the NSKeyedArchiver to write out my data in
I am trying to build an expression tree programmatically. I have in my input,

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.