int->char? 
Author Message
 int->char?

Hi everybody,

I was wondering if there's a function in C that given an integer between 0
and 127, returns the char with the index in ASCII table that corresponds to
the integer given.  I want the OPPOSITE of:

---------- below is the opposite of what i want---------------

char my_char = 'a';
int ascii_idx;

ascii_idx = (int)my_char;

----------

----- below is what i want ------------

As an example, I would like:

f(65) = 'A'
f(97) = 'a'

etc...

Thanks very much for any help

Cart.



Mon, 26 Jul 2004 06:23:20 GMT  
 int->char?

Quote:

> I was wondering if there's a function in C that given an integer between 0
> and 127, returns the char with the index in ASCII table that corresponds to
> the integer given.  I want the OPPOSITE of:

> ---------- below is the opposite of what i want---------------

> char my_char = 'a';
> int ascii_idx;

> ascii_idx = (int)my_char;

The cast is gratuitous and unnecessary.

Quote:
> ----- below is what i want ------------

> As an example, I would like:

> f(65) = 'A'
> f(97) = 'a'

Here is a function that will do what you want, assuming you are
using an ASCII-based system:
        char f(int x) { return x; }


Mon, 26 Jul 2004 06:26:11 GMT  
 int->char?

Quote:

>  I was wondering if there's a function in C that given an integer between 0
>  and 127, returns the char with the index in ASCII table that corresponds to
>  the integer given.

You can do this with a lookup table if you really, really want ASCII. If
your implementation's execution character set is good enough, there is
nothing to do; a character is its value.

Quote:
> I want the OPPOSITE of:

>  ---------- below is the opposite of what i want---------------

>  char my_char = 'a';
>  int ascii_idx;

>  ascii_idx = (int)my_char;

The cast is not needed.
To answer your question, the opposite (in a certain sense) of a
conversion from char to int is a conversion from int to char. What
problems did you have with

int index = 65;
char character = index;

?

Gergo
--
Are we running light with overbyte?



Mon, 26 Jul 2004 06:29:41 GMT  
 int->char?

Quote:

>char ASCII_table[]={-1,-1,/*-1s for ASCII characters with no equivalent in

Gah.  Must've forgotten my coffee this morning.
The first value should, of course, be '\0' (though without ready access
to my copy of N869 I'm not going to claim that it couldn't be argued
that since the null character is just a special value and not really
a member of the character set, flagging it as `no equivalent in the
execution character set' could also be valid).
If char is unsigned, then -1 could be a suboptimal choice for the `no
equivalent' flag as well, especially if the (positive) value (unsigned
char)-1 represents a valid character in the execution character set.

dave

--

I think that recreational {*filter*} have become obsolete in most of the
world, as the school systems provide quite enough brain damage.
                                   --Billy Chambless in comp.lang.c



Mon, 26 Jul 2004 06:47:44 GMT  
 int->char?

Quote:

>Hi everybody,

>I was wondering if there's a function in C that given an integer between 0
>and 127, returns the char with the index in ASCII table that corresponds to
>the integer given.  I want the OPPOSITE of:

>---------- below is the opposite of what i want---------------

>char my_char = 'a';
>int ascii_idx;

>ascii_idx = (int)my_char;

>----------

Since you assume ASCII here...

Quote:
>----- below is what i want ------------

>As an example, I would like:

>f(65) = 'A'
>f(97) = 'a'

>etc...

...you can just use the identity function here as well.

If you need this to work on non-ASCII systems, you can create a lookup
table with the local equivalent of each ASCII character indexed by the
ASCII value of that character, so that, for example,
ASCII_table[65]=='A'
(which would be, for example, 193 (if I'm reading the table correctly)
on an EBCDIC system).  The nice thing about this is that you can portably
initialize this by using character constants:
--------
char ASCII_table[]={-1,-1,/*-1s for ASCII characters with no equivalent in
                             the basic execution character set*/,
                        'A','B','C',/*...*/,
                        'a','b','c',/*...*/};
--------
(you can read the characters off of an ASCII table when you code the
initialization of the lookup table).

dave

--

I think that recreational {*filter*} have become obsolete in most of the
world, as the school systems provide quite enough brain damage.
                                   --Billy Chambless in comp.lang.c



Mon, 26 Jul 2004 06:40:14 GMT  
 int->char?

Quote:

> As an example, I would like:

> f(65) = 'A'
> f(97) = 'a'

The following works for the implementation character set (ASCII in an
ASCII-environment, EBCDIC in an EBCDIC-environment, etc.) where x is in
the appropriate range.

char f(unsigned int x) { return x; }

--
Carius est nobis flagellari p doctrina quam nescire.
[leofre ys us beon beswungen for lare thaenne hit ne cunnan.]
 - MS Cotton Tiberius A, xv, fol. 60v (British Library)



Mon, 26 Jul 2004 08:18:23 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. int->char *

2. int->char conversion

3. int to char / char to int

4. char to int, int to char...

5. conversion int to char, char to int ?????

6. char *fnpars(const char *fn, int p, int v)

7. newbie: int->byte[]->int?

8. Help with pointers, and char -> int

9. unsigned char --> int conversion

10. char --->int

11. int <-> char[32]

12. Convert int -> char *

 

 
Powered by phpBB® Forum Software