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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:11:52+00:00 2026-05-31T06:11:52+00:00

I am having trouble converting a int64_t to a char array and back. I

  • 0

I am having trouble converting a int64_t to a char array and back. I don’t know what is wrong with the code below, it makes complete logical sense to me. The code works for a as shown, but not the second number b which clearly falls into the range of int64_t.

#include <stdio.h>
#include <stdint.h>

void int64ToChar(char mesg[], int64_t num) {
  for(int i = 0; i < 8; i++) mesg[i] = num >> (8-1-i)*8;
}

int64_t charTo64bitNum(char a[]) {
  int64_t n = 0;
  n = ((a[0] << 56) & 0xFF00000000000000U)
    | ((a[1] << 48) & 0x00FF000000000000U)
    | ((a[2] << 40) & 0x0000FF0000000000U)
    | ((a[3] << 32) & 0x000000FF00000000U)
    | ((a[4] << 24) & 0x00000000FF000000U)
    | ((a[5] << 16) & 0x0000000000FF0000U)
    | ((a[6] <<  8) & 0x000000000000FF00U)
    | ( a[7]        & 0x00000000000000FFU);
  return n;
}

int main(int argc, char *argv[]) {
  int64_t a = 123456789;
  char *aStr = new char[8];
  int64ToChar(aStr, a);
  int64_t aNum = charTo64bitNum(aStr);
  printf("aNum = %lld\n",aNum);

  int64_t b = 51544720029426255;
  char *bStr = new char[8];
  int64ToChar(bStr, b);
  int64_t bNum = charTo64bitNum(bStr);
  printf("bNum = %lld\n",bNum);
  return 0;
}

output is

aNum = 123456789
bNum = 71777215744221775

The code also gives two warnings that I don’t know how to get rid of.

warning: integer constant is too large for ‘unsigned long’ type
warning: left shift count >= width of type
  • 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-31T06:11:53+00:00Added an answer on May 31, 2026 at 6:11 am

    This is rather simple, the problem is that you are shifting bits in the char array but size of a[i] is 4 byes (upcast to int), so your shift just goes over range. Try replacing this in your code:

    int64_t charTo64bitNum(char a[]) {
      int64_t n = 0;
      n = (((int64_t)a[0] << 56) & 0xFF00000000000000U)
        | (((int64_t)a[1] << 48) & 0x00FF000000000000U)
        | (((int64_t)a[2] << 40) & 0x0000FF0000000000U)
        | (((int64_t)a[3] << 32) & 0x000000FF00000000U)
        | ((a[4] << 24) & 0x00000000FF000000U)
        | ((a[5] << 16) & 0x0000000000FF0000U)
        | ((a[6] <<  8) & 0x000000000000FF00U)
        | (a[7]        & 0x00000000000000FFU);
      return n;
    }
    

    In this way you’ll cast the char to a 64bit number before doing the shift and you won’t go over range. You’ll obtain correct results:

    entity:Dev jack$ ./a.out 
    aNum = 123456789
    bNum = 51544720029426255
    

    Just a side note, I think this would work fine too, assuming you don’t need to peek inside the char array:

    #include <string.h>
    
    void int64ToChar(char a[], int64_t n) {
      memcpy(a, &n, 8);
    }
    
    int64_t charTo64bitNum(char a[]) {
      int64_t n = 0;
      memcpy(&n, a, 8);
      return n;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having trouble converting some code from VB6 to VB.NET (I don't have
I'm having trouble converting this piece of code (originally in VB) to C#. In
I am having trouble converting this code into extension method syntax: var query =
I've been having trouble converting the following fairly straight forward c# code into vb.net
I'm having trouble converting an array into correctly nested HTML. Assuming I have an
I am having trouble converting my int values to doubles. I know I have
Having trouble converting the following code from dealing with IDs to Classes. Basicly what
I made my first class and I'm having trouble converting the objects back into
I'm having trouble converting the following SQL-statement to LINQ-to-entities: SELECT l.* FROM locations l
I'm having trouble converting a C# Linq statement to VB.NET. I need the following

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.