DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 5 of 5

Hybrid View

  1. #1
    Steve Guest

    Convert ascii to character and vice-versa


    How do I convert an ascii integer to a character and vice-versa in Java?


    Thanks,


    Steve Shier

  2. #2
    Paul Clapham Guest

    Re: Convert ascii to character and vice-versa

    There's no such thing as an ASCII integer. ASCII is a character set. Maybe
    you could explain what you're trying to do in some other words?

    PC2

    Steve <sshier@3millsoft.com> wrote in message
    news:3a651e67$1@news.devx.com...
    >
    > How do I convert an ascii integer to a character and vice-versa in Java?
    >
    >
    > Thanks,
    >
    >
    > Steve Shier




  3. #3
    Steve Shier Guest

    Re: Convert ascii to character and vice-versa


    Thank you Paul. I'm trying to do 2 things :

    1) Take a decimal integer , say, 65 and convert it to its ascii equivalent,
    "A". In VB this is done by using the function Chr(65) which returns "A"
    . I have no solution for doing this in Java other than chreating a map to
    map all ascii integers to their corresponding characters. This problem is
    so basic that I feel I'm missing something.

    2) Take a character, say , "A" and find out the ascii byte integer which
    corresponds to it. In VB this is done by using
    Asc("A") which returns the integer 65 . I have temporarily solved this second
    problem by using:

    int iAscii;
    byte[] charbytes;

    charbytes = sValue.substring(i, 1).getBytes();
    iAscii=charbytes[0];

    sValue contains a string in which I need to convert each byte to its Ascii
    code (its in a loop and the index i iterates through the string). My method
    appears to be overly complex-in VB this can be done in one line and I wonder
    if it might be possible in Java to do this more simply.

    Thank you for your help in advance.

    Steve Shier




    "Paul Clapham" <pclapham@core-mark.com> wrote:
    >There's no such thing as an ASCII integer. ASCII is a character set. Maybe
    >you could explain what you're trying to do in some other words?
    >
    >PC2
    >
    >Steve <sshier@3millsoft.com> wrote in message
    >news:3a651e67$1@news.devx.com...
    >>
    >> How do I convert an ascii integer to a character and vice-versa in Java?
    >>
    >>
    >> Thanks,
    >>
    >>
    >> Steve Shier

    >
    >



  4. #4
    Paul Clapham Guest

    Re: Convert ascii to character and vice-versa

    You are correct, it is so basic that you are missing something. You can
    cast between char and int:

    int i = 65;
    char c = (char)i;

    char c = 'A';
    int i = (int)c;

    PC2

    Steve Shier <sshier@3millsoft.com> wrote in message
    news:3a65fd06$1@news.devx.com...
    >
    > Thank you Paul. I'm trying to do 2 things :
    >
    > 1) Take a decimal integer , say, 65 and convert it to its ascii

    equivalent,
    > "A". In VB this is done by using the function Chr(65) which returns "A"
    > I have no solution for doing this in Java other than chreating a map to
    > map all ascii integers to their corresponding characters. This problem is
    > so basic that I feel I'm missing something.
    >
    > 2) Take a character, say , "A" and find out the ascii byte integer which
    > corresponds to it. In VB this is done by using
    > Asc("A") which returns the integer 65 .




  5. #5
    Steve Shier Guest

    Re: Convert ascii to character and vice-versa


    Thanks a bunch Paul. If I was any smarter I'd feel like an idiot ) . I
    can't tell you how much time I wasted on this, but I chalk it up to being
    a future expert.

    Thanks again.


    Steve

    "Paul Clapham" <pclapham@core-mark.com> wrote:
    >You are correct, it is so basic that you are missing something. You can
    >cast between char and int:
    >
    >int i = 65;
    >char c = (char)i;
    >
    >char c = 'A';
    >int i = (int)c;
    >
    >PC2
    >
    >Steve Shier <sshier@3millsoft.com> wrote in message
    >news:3a65fd06$1@news.devx.com...
    >>
    >> Thank you Paul. I'm trying to do 2 things :
    >>
    >> 1) Take a decimal integer , say, 65 and convert it to its ascii

    >equivalent,
    >> "A". In VB this is done by using the function Chr(65) which returns "A"
    >> I have no solution for doing this in Java other than chreating a map

    to
    >> map all ascii integers to their corresponding characters. This problem

    is
    >> so basic that I feel I'm missing something.
    >>
    >> 2) Take a character, say , "A" and find out the ascii byte integer which
    >> corresponds to it. In VB this is done by using
    >> Asc("A") which returns the integer 65 .

    >
    >



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links