Convert int to char in C

Now I am trying to convert int to char in C programming. After researching, I found that I should be able to do this as follows:

int value = 10;
char result = (char) value;

I would like this to return 'A' (and for 0-9 to return '0' - '9'), but this returns the new line character that I think. My whole function looks like this:

char int2char (int radix, int value) {
  if (value < 0 || value >= radix) {
    return '?';
  }

  char result = (char) value;

  return result;
}
+4
source share
6 answers

you don't need anything to convert int to char

char x;
int y;


/* do something */

x = y;

only one int value to char as a printable character (usually ASCII), as in your example:

const char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

int inttochar(int val, int base)
{
    return digits[val % base];
}

(char *), stansdard, sprintf, itoa, ltoa, utoa, ultoa.... :

char *reverse(char *str);
const char digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

char *convert(int number, char *buff, int base)
{
    char *result = (buff == NULL || base > strlen(digits) || base < 2) ? NULL : buff;
    char sign = 0;


    if (number < 0)
    {
         sign = '-';

    }
    if (result != NULL)
    {
        do
        {
            *buff++ = digits[abs(number % (base ))];
            number /= base;
        } while (number);
        if(sign) *buff++ = sign;
        if (!*result) *buff++ = '0';
        *buff = 0;
        reverse(result);
    }
    return result;
}
+6

-

const char* foo = "0123456789ABC...";

... - , .

foo[value] char. foo[0] '0', foo[10] 'A'.

(, , ASCII), .

+4

( ASCII) . '0' - '9' , 10 '0'. 10 10 'A':

char result;
if (value >= 10) {
    result = 'A' + value - 10;
} else {
    result = '0' + value;
}
+1

Int Char

, OP , 1 , radix .


int ( 1 char) sprintf(buf, "%d", value).

, , INT_MIN


C99 char*, . , .

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

// Maximum buffer size needed
#define ITOA_BASE_N (sizeof(unsigned)*CHAR_BIT + 2)

char *itoa_base(char *s, int x, int base) {
  s += ITOA_BASE_N - 1;
  *s = '\0';
  if (base >= 2 && base <= 36) {
    int x0 = x;
    do {
      *(--s) = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"[abs(x % base)];
      x /= base;
    } while (x);
    if (x0 < 0) {
      *(--s) = '-';
    }
  }
  return s;
}

#define TO_BASE(x,b) itoa_base((char [ITOA_BASE_N]){0} , (x), (b))

void test(int x) {
  printf("base10:% 11d base2:%35s  base36:%7s ", x, TO_BASE(x, 2), TO_BASE(x, 36));
  printf("%ld\n", strtol(TO_BASE(x, 36), NULL, 36));
}

int main(void) {
  test(0);
  test(-1);
  test(42);
  test(INT_MAX);
  test(-INT_MAX);
  test(INT_MIN);
}

base10:          0 base2:                                  0  base36:      0 0
base10:         -1 base2:                                 -1  base36:     -1 -1
base10:         42 base2:                             101010  base36:     16 42
base10: 2147483647 base2:    1111111111111111111111111111111  base36: ZIK0ZJ 2147483647
base10:-2147483647 base2:   -1111111111111111111111111111111  base36:-ZIK0ZJ -2147483647
base10:-2147483648 base2:  -10000000000000000000000000000000  base36:-ZIK0ZK -2147483648

Ref fprintf() ?

+1

ascii

, char, , . 10 -

0

, C ASCII ( UTF-8, ascii-). , "" "65" ( , ). "char" C - ASCII. int char, int ASCII - , C, , , char , int. , , . ,

if(value < 10) return '0'+value;
return 'A'+value-10;

will be what you want to return from your function. Keep your border checks with "radix", as you did, imho, which is good practice in C.

0
source

Source: https://habr.com/ru/post/1684915/


All Articles