Re: Converting char->int and int->char. HOW?

From: Kaj Risberg (krisberg_at_welho.com)
Date: 12/04/03


Date: Thu, 4 Dec 2003 22:49:16 +0200

This should work in java: (similar to c)

int a = 0;
char b = 1;

a = (int) b;
b = (char) a;

Same code in pascal:

VAR a : INTEGER,
         b: CHAR;

a := 0; b:=1;

a := ORD(b);
b := CHR(a);

"gajo" <gajo@eunet.yu> kirjoitti viestissä
news:bqnq14$3ot$1@news.eunet.yu...
> In Pascal you have these two functions:
>
> Ord(ch: char): integer; which returns the ASCII number of that char
> Chr(i: integer): char; which returns the character of that ASCII number
>
> How can I do the same in Java? I've found these two functions:
>
> static int getNumericValue(char ch)
> static char forDigit(int digit, int radix)
>
> getNumericValue does not return the correct number. For example, for 'c' I
> get 12, while I should get something like 65 I think.
> I haven't even tested the forDigit function, cause I have no idea what the
> radix is suppoed to be, and the documentation only says that 0 <= digit <
> radix
>
> So what is it I'm supposed to do? How can I have the same two functions in
> Java just like I have them in Pascal?
>
> Also, I've created a HashSet. I want to add a character, but it won't let
> me, and I can only add Strings. This really isn't a problem, but why is it
> so that I cannot add characters to a set?
>
> In Pascal I just type
> s: set of char;
> And it's done. In Java it seems impossible (to me).
>
> Gajo
> ocsabo@neobee.net
>
>



Relevant Pages

  • Re: K&R exercise 1-18
    ... _without_ the string terminator and has MAXIMUM+1 array elements. ... char lineis in this context equivalent to char *line ... int main ... You lose one character by overwriting it with '\n'. ...
    (comp.lang.c)
  • Re: Write to file
    ... You can read the file one character at a time. ... compared to the current char. ... Once the (lgh - 1)th char ... void initnext(int *next, const char *id, int lgh) ...
    (comp.lang.c)
  • Re: Common misconceptions about C (C95)
    ... int main ... void f ... printf("\nThe character is lower case.\n"); ... plain char to them, but they take unsigned char values. ...
    (comp.lang.c)
  • Re: Write to file
    ... int main(int argc, char *argv) ... I intended you you use c here not read another character! ... You reuse the buffer. ...
    (comp.lang.c)
  • Re: Small Correction to K&R Exercise 1-22 Solution on CLC-Wiki
    ... My fix is now reflected at the above address. ... I don't think that overwriting the n-th character with a ... single char location by letting a set sign bit indicate that a line ... int main ...
    (comp.lang.c)

Loading