Converting Decimal to ASCII Character

26,709

Solution 1

Convert a character to a number in C:

int j = 'A';

Convert a number to a character in C:

char ch = 65;

Convert a character to a number in python:

j = ord('A')

Convert a number to a character in Python:

ch = chr(65)

Solution 2

I'm not sure what language you're asking about. In Java, this works:

int a = 'a';

Solution 3

Most languages have a 'char' function, so it would be Char(j)

Share:
26,709
Kevin McFadden
Author by

Kevin McFadden

Updated on July 09, 2022

Comments

  • Kevin McFadden
    Kevin McFadden almost 2 years

    I am trying to convert an decimal number to it's character equivalent. For example:

    int j = 65 // The character equivalent would be 'A'.

    Sorry, forgot to specify the language. I thought I did. I am using the Cocoa/Object-C. It is really frustrating. I have tried the following but it is still not converting correctly.

    char_num1        = [working_text characterAtIndex:i];   // value = 65               
    char_num2         = [working_text characterAtIndex:i+1];    // value =  75
    char_num3         = char_num1  + char_num2;         // value = 140                                      
    char_str1   = [NSString stringWithFormat:@"%c",char_num3];  // mapped value = 229
    char_str2   = [char_str2 stringByAppendingString:char_str1];    
    

    When char_num1 and char_num2 are added, I get the new ascii decimal value. However, when I try to convert the new decimal value to a character, I do not get the character that is mapped to char_num3.

    • Mitch Wheat
      Mitch Wheat over 14 years
      what language, platform?
    • pavium
      pavium over 14 years
      Welcome aboard Kevin. You will notice that simple questions like this usually get an answer, maybe several, in a matter of minutes. It's a good idea to hang around and see the answers so you can reply to simple queries like "what language?" and more focussed answers can be given. Don't forget to 'accept' an answer by clicking on the big tick/checkmark (but don't do it before all the answers are in).