NumberFormat returns a negative ascii value

Formatting using NumberFormat class is returning an unidentified character, a negative ascii code number. The attached code snippet yields the following:

In the debugger, num looks like this: 5 000 $
bytes = 53 -96 48 48 48 32 36

What is the -96 doing in there? It's causing display problems for me. I've tried compiling under Java 1.5.0.4 and Java 1.6.0.16 and got same results.
Locale locale = new Locale("fr", "CA");
NumberFormat nf = NumberFormat.getCurrencyInstance(locale);
String num = nf.format(5000.0);
byte[] bytes = num.getBytes();

Open in new window

brian-barnettAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

CPColinSenior Java ArchitectCommented:
The byte type is signed in Java. That means it goes from -128 to 127 instead of 0 to 255. When printing them out, if you AND each byte with 0xFF, everything should show up fine.

So like instead of this:

System.out.print(bytes[i]);

do this:

System.out.print(bytes[i] & 0xFF);
0
brian-barnettAuthor Commented:
I don't actually need to put it into a byte array, I was just trying to figure out what the bad character was. After calling nf.format(5000.0), I end up taking that num value and return it to the client in an xml document. The error I am getting is:

XML Parsing Error: not well-formed
and it points to this-- beginningBalance="5¿000 $"

Any ideas why NumberFormat would do that? Am I not using NumberFormat correctly?
0
brian-barnettAuthor Commented:
The xml doc is UTF-8
0
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

brian-barnettAuthor Commented:
Looks like it is a non-breaking space issue with the NumberFormat implementation on a Windows box.

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4418200
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
CPColinSenior Java ArchitectCommented:
It looks like that weird character is supposed to be a non-breaking space. I guess the Canadian French locale uses that as the thousands separator. I don't know enough about XML to know whether that character is not allowed in an attribute. Does the problem persist if you use a different locale, like US English?
0
CPColinSenior Java ArchitectCommented:
Oops. You got it.
0
wannabetechieCommented:
The problem is with the usage of locale.I guess you want the language for the locale to be Canadian french
and the code for that is "fr_CA"

So change the locale definition to
Locale locale = new Locale("fr_CA", "CA");

And you should not get the junk character now.
0
brian-barnettAuthor Commented:
It does not happen with US english. It can be included in xml but needs special handling. It would be represented in xml like this -->  

wannabetechie:
I think your comment is erroneous. The constructor you specified for Locale accepts two params, a language and a country code. "fr_ca" is not a valid value for language. The correct syntax is as I showed in my original code snippet.

Locale locale = new Locale("fr", "CA");
0
CEHJCommented:
>>Looks like it is a non-breaking space issue with the NumberFormat implementation on a Windows box.

I get the same and don't use Windows. Is it a bug? Do have some locales have this weird NBSP in the format?
0
brian-barnettAuthor Commented:
I just read about here http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4418200. I experienced it with fr_CA locale (french canada). The link above describes the problem  with sv_SE (Sweden) as well, but I suspect it would happen with any currency that uses a space as the thousands separator. The idea is the NBSP keeps the number from being split across lines when being displayed.
0
CEHJCommented:
>>The idea is the NBSP keeps the number from being split across lines when being displayed.

Ah - i see
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
Java

From novice to tech pro — start learning today.