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

  • Home
  • SEARCH
  • 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 6953137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:26:24+00:00 2026-05-27T14:26:24+00:00

Can anyone tell me what is wrong with the following code? __inline__ char* ut_byte_to_long

  • 0

Can anyone tell me what is wrong with the following code?

__inline__
char* ut_byte_to_long (ulint nb) {

   char* a = malloc(sizeof(nb)); 
   int i = 0;
   for (i=0;i<sizeof(nb);i++) {
       a[i] = (nb>>(i*8)) & 0xFF;
   }
   return a; 
}

This string is then concatenated as part of a larger one using strcat. The string prints fine but for the integers which are represented as character symbols. I’m using %s and fprintf to check the result.

Thanks a lot.

EDIT

I took one of the comments below (I was adding the terminating \0 separately, before calling fprintf, but after strcat. Modifying my initial function…

__inline__
char* ut_byte_to_long (ulint nb) {

   char* a = malloc(sizeof(nb) + 1); 
   int i = 0;
   for (i=0;i<sizeof(nb);i++) {
       a[i] = (nb>>(i*8)) & 0xFF;
   }
   a[nb] = '\0' ; 
   return a; 
}

This sample code still isn’t printing out a number…

char* tmp;
tmp = ut_byte_to_long(start->id);

fprintf(stderr, "Value of node is %s \n ", tmp);
  • 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-27T14:26:25+00:00Added an answer on May 27, 2026 at 2:26 pm

    if you dont want to use sprintf(target_string,"%lu",source_int) or the non standard itoa(), here is a version of the function that transform a long to a string :

    __inline__
    char* ut_byte_to_long (ulint nb) {
        char* a = (char*) malloc(22*sizeof(char));  
        int i=21;
        int j;
        do
        {
            i--;
            a[i] = nb % 10 + '0';
            nb = nb/10; 
        }while (nb > 0);
        // the number is stored from a[i] to a[21]
    
        //shifting the string to a[0] : a[21-i]
        for(j = 0 ; j < 21 && i < 21 ; j++ , i++)
        {
            a[j] = a[i];
        }
        a[j] = '\0';
        return a;
    }
    

    I assumed that an unsigned long contain less than 21 digits. (biggest number is 18,446,744,073,709,551,615 which equals 2^64 − 1 : 20 digits)

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

Sidebar

Related Questions

Can anyone tell me what could be wrong with the following code! Im this
Can anyone tell me what is wrong with the following code? It adds the
Can anyone tell me where I am going wrong... I am using the following
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
Can anyone tell me what's wrong with this code? class Dataset < ActiveRecord::Base has_many
Can anyone tell me what exactly does this Java code do? SecureRandom random =
Can anyone help me debug this program? The following is server code : package
Can anyone tell me how I can display a status message like 12 seconds
Can anyone tell me what is the memory usage overhead associated with PHP opcode
Can anyone tell me if there are RDBMSs that allow me to create a

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.