• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 733
  • Last Modified:

Converting Unicode String to ASCII

If I have a string in Unicode stored in a String Variable, how do I convert the stored string to ASCII?

I'm passing a Unicode String into a java class via VB, and then I'm trying to pass the string to some other java class method.  However, the string I pass to the other java method must be ASCII but currently I believe its Unicode when I pass it from VB to my first java class file.  I need to convert it first but do not know a very simple and effective way of doing that.

Thanks in advance.
0
pjmajer
Asked:
pjmajer
  • 17
  • 11
  • 3
  • +2
1 Solution
 
objectsCommented:
All Java strings are Unicode.
Are you actually having a problem? WHy do you say:
"the string I pass to the other java method must be ASCII"
0
 
pjmajerAuthor Commented:
I was getting data from a DB in the past that had ASCII values.  Now the DB contains Unicode values.  I believed all strings in Java were Unicode.  However, my code is not working now and I believe its because the string I get from the DB is not ASCII its now Unicode.

I say that the string I pass to the other java method must be ASCII because its code provided by a 3rd party and they said their code does not support Unicode.
0
 
objectsCommented:
> said their code does not support Unicode.

Does it take a String or a byte array as a parameter?
Can't see how it could accept a String if it doesn't support Unicode.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
pjmajerAuthor Commented:
It takes a string.  I really do not know that many details about them and why/how they could not support Unicode.  All I know is that running the my same class file against a non-Unicode DB my code works.  Running it against a Unicode DB it doesn't.
0
 
objectsCommented:
In fact afaik ASCII is a subset of Unicode.
0
 
pjmajerAuthor Commented:
"afaik ASCII"

What is this?

What you said may be true, but all I know is that the code does not do what it did on a non-Unicode DB now that its on a Unicode DB.  Thats the only thing thats different.
0
 
pjmajerAuthor Commented:
"afaik ASCII"

What is this?

What you said may be true, but all I know is that the code does not do what it did on a non-Unicode DB now that its on a Unicode DB.  Thats the only thing thats different.
0
 
objectsCommented:
If the database is returning values outside the ASCII range (>=128) then I'd say it's up to you to decide how these are mapped into the ASCII range.
0
 
pjmajerAuthor Commented:
"afaik ASCII"

What is this?

What you said may be true, but all I know is that the code does not do what it did on a non-Unicode DB now that its on a Unicode DB.  Thats the only thing thats different.
0
 
pjmajerAuthor Commented:
First I'd like to apologize by posting the same comment 3 times.  I hit refresh on accident multiple times.

I have a question regarding what you meant its iup to me to decide how they are mapped into the ASCII range.  I just want to ensure what I'm passing to the 3rd party method is supported by them (once again they told me they only support ASCII and not Unicode)
0
 
omry_yCommented:
<afaik - as far as I know.>

anyway, what problems do you have exactly?


0
 
objectsCommented:
Sorry, akaik == as far as I know.

I think your problem is not converting Unicode to ASCII, but rather the encoding used by your database(s).
You need to know this to construct your String correctly.
0
 
pjmajerAuthor Commented:
I don't know if you have experience with Actuate reports.  I've created an Actuate Report to display signatures.  The signature is basically a string stored in the DB - I pass the string to the 3rd party vendor's software and they convert the string to a .jpg image.

In the past, the string was ASCII and single byte (stored in the DB) and when I ran the report, the signature was displayed.

Now, running off of a Unicode DB, the string is double byte (right?  Unicode is double byte chars) and the signature is not displayed.

I was told the 3rd party vendor does not support Unicode and thats why I was asking how to convert this string from Unicode to ASCII before passing it to them.
0
 
objectsCommented:
> its iup to me to decide how they are mapped into the
> ASCII range

If you're database returns a value outside the ASCII range, say 147. How are you going to map this to 0-127?
You need to have know what mapping to use. That's why I said above that you need to know what encoding is used on the database, so you can know how character 147 should be displayed.
0
 
pjmajerAuthor Commented:
Its a Unicode encoding - thats what I've said.  I believe its UTF-8.  Not sure exactly if thats it or not though.
0
 
objectsCommented:
> In the past, the string was ASCII and single byte
> (stored in the DB) and when I ran the report, the
> signature was displayed.

All Java strings are Unicode and double bytes.
When it is saved (or retrieved) from a database some form or encoding is done to transform what is in the database to a Java Unicode String.
0
 
pjmajerAuthor Commented:
so you're saying the string I received from the DB 2 months ago with my ASCII DB, is the same one I'm receiving now from my Unicode DB?

If thats the case, why is my code not working now with the Unicode DB?  I believe there is something missing from this.
0
 
objectsCommented:
No I'm not saying that. The data coming from the ASCII database only had single byte per character so when converted to a Unicode string all characters would fall inside the ASCII range.
The second database however contains data outside the ASCII range so some form of mapping is needed to map values>=128 into the ASCII range. The question is what these characters are and how should they be represented.

0
 
pjmajerAuthor Commented:
How do I find out and what do I do?  Thats my question for you.  I'm sort of confused and lost on what the solutoin to this problem is.
0
 
objectsCommented:
Do you know what the non-ascii data is that the database contains?
0
 
pjmajerAuthor Commented:
The data in the string is random data.  Its a string that actually represents a Signature.  But its stored in a string var as a bunch of ascii characters.  However, the string is now going to be stored in a Unicode DB.  Thats the only difference between when it was working and when it was not.
0
 
pjmajerAuthor Commented:
PS - this is my last post for the night - I'm off to bed.
0
 
objectsCommented:
So you are writing the String to the database?
0
 
pjmajerAuthor Commented:
Last Post  for the night.

No, I'm not writing it to the DB.  I'm reading it from the DB.  Then I'm passing it to a 3rd party software the converts the string into a .jpg image.  However, they don't support unicode.  In the ASCII DB it worked.  With the Unicode DB it does not.
0
 
heyhey_Commented:
1. get the field as binary stream
2. dump the bytes so that we can see what exactly they contain.
3. try creating new String with

String result = new String(buf, "UTF8");
0
 
CEHJCommented:
I think heyhey's moving in the right direction here. Can you also tell us something about this '3rd party software'? Presumably you're using its API to pass in the string. What is the method signature of the method to which you pass the string?
0
 
objectsCommented:
If you're not writing the data to mthe db, then find out who is and how you should be interpreting the non-ASCII data that the database contains.
0
 
pjmajerAuthor Commented:
Last Post  for the night.

No, I'm not writing it to the DB.  I'm reading it from the DB.  Then I'm passing it to a 3rd party software the converts the string into a .jpg image.  However, they don't support unicode.  In the ASCII DB it worked.  With the Unicode DB it does not.
0
 
heyhey_Commented:
> Last Post  for the night.

long night ... :)
0
 
CEHJCommented:
Can you also tell us something about this '3rd
party software'? Presumably you're using its API to pass in the string. What is the signature
of the method to which you pass the string?
0
 
pjmajerAuthor Commented:
Sorry about the Last Post message again.  Hit refresh on accident.

Can you expand on your question about what is the signature of the method to which I pass the string?  What do you want to know?
0
 
heyhey_Commented:
Comment

From: heyhey_
Date: 04/22/2002 12:08AM PST

1. get the field as binary stream
2. dump the bytes so that we can see what exactly they contain.
3. try creating new String with

String result = new String(buf, "UTF8");
0
 
CEHJCommented:
In your own words:
'I'm reading it from the DB.  Then I'm passing it to a 3rd party software
the converts the string into a .jpg image'
You're presumably using some API to pass it to the 3rd party software. Can you give use the technical details for this - i.e. what language is this software written in, what is the function call you're using, what is the method signature, etc?
0
 
pjmajerAuthor Commented:
I believe the code worked fine as it is.  I got it working and I think it was a mistake on my point where I did not use the correct Actuate Report files (used incorrect param #/val in the java method call - it was erroring out).

I got it working.  It displays the images now.  

So who do I give the points to?
0
 
pjmajerAuthor Commented:
Thanks for your help.  Problem solved - closing the question.
0
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.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

  • 17
  • 11
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now