Hexa to Ascii convertion

How can i write a function that takes an hexa aas String and returns me the ascii representation of the number(as char).

Ex:   convert("4B") -> 'K'
LVL 1
kodcanavariAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sciuriwareCommented:
A character in JAVA is 16 bits, however common ascii is converted by adding 8 0-bits in front.

You might convert that HEX string by parsing to a short, and adding the zero bits in front
as a char is unsigned in JAVA.

;JOOP!
0
sciuriwareCommented:
public char convert(String hex)
{
     int h = Integer.parseInt(hex, 16);
     char c = (h & 0377);

    return(c);
}

;JOOP!
0
sciuriwareCommented:
char c = (h & 0377);  // Will give a compiler warning, better is:

public char convert(String hex)
{
     int h = Integer.parseInt(hex, 16);
     return((char)(h & 0377));
}

;JOOP!
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

sciuriwareCommented:
And beware of NumberFormatExceptions when junk string is given ......................

;JOOP!
0
dnunes_brCommented:
This answers your question, I believe so:
char val = (char)Integer.valueOf("4B", 16).intValue();

Open in new window

0
kodcanavariAuthor Commented:
first thant for the response, it corresponds exactly. As you

if i write this char to a file how many bites will it be?
is there any way to write the value of the hexa on a file?
4B -> 01001011 (on the file system)
0
kodcanavariAuthor Commented:
i wanted to say thanks on the 1st line
0
dnunes_brCommented:
If you want to write exactly the ones and zeroes on a text file or the console you can do as so:


int hexVal = Integer.valueOf("4B", 16).intValue();
char val = (char)hexVal;
String st = Integer.toBinaryString(hexVal);
//Writing to the console...
System.out.println(val);
System.out.println(st);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kodcanavariAuthor Commented:
Thats exactly what i want, thanks a lot.
0
sciuriwareCommented:
The question was already answered!!!!!!!!!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Languages-Other

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.