0

How can I convert a long variable to a char[] variable without using library functions?

17
  • 2
    C doesn't have a string type; do you want a C string or a C++ std::string? Commented Oct 3, 2014 at 17:44
  • 1
    @AlterMann, sprintf is not a system call. Commented Oct 3, 2014 at 17:46
  • 5
    All you need is division and modulo operations, and a knowledge of the character set. The only system call you need is the realloc function. Commented Oct 3, 2014 at 17:46
  • 1
    sprintf() is not a system call. I'd like to ask which OS's system calls you are targetting. Also, do you actually know what a system call is? Commented Oct 3, 2014 at 17:46
  • 1
    Your question is ambiguous. Are you trying to a) convert a long integer value from some calculations into a text representation suitable for displaying to the user, or b) get at the raw bytes making up that long value by using a char[] to get at individual bytes, or c) something else entirely... Commented Oct 3, 2014 at 18:08

2 Answers 2

2

Working example (1) - thread-safe, requires min. buffsize = 40.

static const char *
xllitoa(long long int x, char *buff)
{
        char *p = buff + 40;
        int sign = 0;
        *(p--) = 0;
        if (x < 0) sign = 1;
        else x = -x;
        do { *(p--) = -(x % 10) + '0'; x /= 10; } while(x);
        if (sign) *(p--) = '-';
        return (const char *)(p+1);
}

Working example (2) - not thread-safe

static const char *
xllitoa(long long int x)
{
        static char buff[40];
        char *p = buff + 40;
        int sign = 0;
        *(p--) = 0;
        if (x < 0) sign = 1;
        else x = -x;
        do { *(p--) = -(x % 10) + '0'; x /= 10; } while(x);
        if (sign) *(p--) = '-';
        return (const char *)(p+1);           
}

Many thanks to reviewers. Now it accepts LLONG_MAX and LLONG_MIN as well.

Sign up to request clarification or add additional context in comments.

10 Comments

+1 because IMHO it is the solution, but you do not use system calls ... was the question badly asked ?
use '0' instead of 48. C99 guarantees that '1' is next to '0'. '0' might differ from 48 (EBCDIC?). If long long is 128bit on some (future?) system then you need bufsize=40 (including '\0' at the end). Perheps some formula with sizeof(long long) should be used.
Note that in C99 you can document the 'buffer must be 32 characters' requirement explicitly (if obscurely) with: const char *xllitoa(long long int x, char buff[static 32]) { … }.
xllitoa() fails in 2 ways. 1) It does not terminate the buffer (But then OP said char array and not "string" Hmmmm. 2) It does not work for LLONG_MIN.
@soerium C specifies LLONG_MIN <= LONG_MIN. Since they could be the same value (they are on my gcc machine), failing LLONG_MIN as this code does, also can fail OP's LONG_MIN. Going to long long to avoid a problem with LONG_MIN does not always work.
|
2

After accept answer that works for all values LONG_MIN to LONG_MAX.

This uses a helper function to recursive work with negative values of n. By using negative values, there is no problem with LONG_MIN.

static char *ltostr_helper(long n, char *dest) {
  if (n <= -10)
    dest = ltostr_helper(n / 10, dest);
  *dest++ = (char) ('0' - n % 10);
  return dest;  // return pointer to end
}

void ltostr(long n, char *dest) {
  if (n < 0) {
    *dest++ = '-';
  } else {
    n = -n;
  }
  *ltostr_helper(n, dest) = '\0';
}

int main(void) {
  char buf[sizeof(long) * CHAR_BIT /3 + 3];//  size buffer to our needs
  ltostr(0, buf); printf("%s\n", buf);
  ltostr(123, buf); printf("%s\n", buf);
  ltostr(-123, buf); printf("%s\n", buf);
  ltostr(LONG_MAX, buf); printf("%s\n", buf);
  ltostr(LONG_MIN, buf); printf("%s\n", buf);
  return 0;
}

Output
0
123  
-123  
9223372036854775807  
-9223372036854775808  

1 Comment

+1 for sizeof(long). The negative values trick works for any allowed long representation: two's complement, one's complement, sign/magnitude representations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.