Solved

Converting Unicode String to ASCII

Posted on 2002-04-21
35
650 Views
Last Modified: 2012-05-04
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
Comment
Question by:pjmajer
  • 17
  • 11
  • 3
  • +2
35 Comments
 
LVL 92

Accepted Solution

by:
objects earned 300 total points
ID: 6958897
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
 

Author Comment

by:pjmajer
ID: 6958909
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
 
LVL 92

Expert Comment

by:objects
ID: 6958930
> 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
 

Author Comment

by:pjmajer
ID: 6958933
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
 
LVL 92

Expert Comment

by:objects
ID: 6958934
In fact afaik ASCII is a subset of Unicode.
0
 

Author Comment

by:pjmajer
ID: 6958939
"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
 

Author Comment

by:pjmajer
ID: 6958943
"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
 
LVL 92

Expert Comment

by:objects
ID: 6958948
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
 

Author Comment

by:pjmajer
ID: 6958949
"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
 

Author Comment

by:pjmajer
ID: 6958952
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
 
LVL 4

Expert Comment

by:omry_y
ID: 6958955
<afaik - as far as I know.>

anyway, what problems do you have exactly?


0
 
LVL 92

Expert Comment

by:objects
ID: 6958957
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
 

Author Comment

by:pjmajer
ID: 6958967
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
 
LVL 92

Expert Comment

by:objects
ID: 6958969
> 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
 

Author Comment

by:pjmajer
ID: 6958974
Its a Unicode encoding - thats what I've said.  I believe its UTF-8.  Not sure exactly if thats it or not though.
0
 
LVL 92

Expert Comment

by:objects
ID: 6958975
> 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
 

Author Comment

by:pjmajer
ID: 6958980
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 92

Expert Comment

by:objects
ID: 6959011
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
 

Author Comment

by:pjmajer
ID: 6959018
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
 
LVL 92

Expert Comment

by:objects
ID: 6959049
Do you know what the non-ascii data is that the database contains?
0
 

Author Comment

by:pjmajer
ID: 6959061
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
 

Author Comment

by:pjmajer
ID: 6959063
PS - this is my last post for the night - I'm off to bed.
0
 
LVL 92

Expert Comment

by:objects
ID: 6959075
So you are writing the String to the database?
0
 

Author Comment

by:pjmajer
ID: 6959086
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
 
LVL 16

Expert Comment

by:heyhey_
ID: 6959096
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 6959322
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
 
LVL 92

Expert Comment

by:objects
ID: 6959359
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
 

Author Comment

by:pjmajer
ID: 6959915
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
 
LVL 16

Expert Comment

by:heyhey_
ID: 6959931
> Last Post  for the night.

long night ... :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 6959934
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
 

Author Comment

by:pjmajer
ID: 6960335
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
 
LVL 16

Expert Comment

by:heyhey_
ID: 6960348
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 6960383
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
 

Author Comment

by:pjmajer
ID: 6961709
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
 

Author Comment

by:pjmajer
ID: 7259383
Thanks for your help.  Problem solved - closing the question.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
pairstar challenge 2 42
base64 decode encode 12 100
advertisement module in core php 4 94
Groovy:unable to resolve class error 2 32
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now