Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 73
Registered: ‎11-02-2009
My Device: Not Specified
Accepted Solution

blackberry £ symbol and \xA3

[ Edited ]

Hi, I'm having a small issue with pulling text from an AutoTextField and when the user enters a British pound sign, I'm getting the hex value of \xA3.  I was expecting a literal/html symbol, but now I need to convert any occurrences of \xA3 using a replaceAll function. Is there a simpler way to do this?

Retired
Posts: 2,559
Registered: ‎10-16-2009
My Device: BlackBerry Z10
My Carrier: Bell

Re: blackberry £ symbol and \xA3

Hi,

 

Could you please post some code for how you are performing this action? I have tried myself but am able to successfully retrieve the £ symbol using AutoTextEditField.getText(). Please also let us know which SDK and simulator/device model and software version you are using.

 

Thanks,

Garett
@garettBeuk
--
Goodbye everybody!
Highlighted
Developer
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: blackberry £ symbol and \xA3

I'm guessing that at some point you are using String.getBytes(), which uses the default ISO 8859-1 encoding. (0xA3 is the ISO 8859-1 encoding of the British pound sign.) I'm also guessing that you are converting to bytes in order to feed the data to some other process that is expecting something other than ISO 8859-1 encoding. You probably have various options:

  1. Use String.getBytes("UTF-8") to encode using UTF-8 (the default encoding for XML/XHTML).
  2. Specify ISO 8859-1 as the character encoding for the HTML.
  3. Encode the characters as HTML escape sequences.

For the latter, here's a piece of code I picked up somewhere on the net:

 

public static String encodeHTML(String s) {
StringBuffer out = new StringBuffer();
for(int i=0; i<s.length(); i++) {
char c = s.charAt(i);
if(c > 127 || c=='"' || c=='<' || c=='>') {
out.append("&#"+(int)c+";");
} else {
out.append(c);
}
}
return out.toString();
}



Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Developer
Posts: 73
Registered: ‎11-02-2009
My Device: Not Specified

Re: blackberry £ symbol and \xA3

Thanks for getting back to me on this guys. It's probably something pretty silly I'm going.

 

Basically, I get the text entered using getText().  When I upload the xml payload to the server, this particular character is represented as an unrecognised symbol when viewed on the web page, so I'm guessing I need to use the encode as HTML approach. I'll need to decode it also when viewed on the device again.  What I don't get is once I've got the string from the text field, any attempt to scan for \xA3 returns nothing. Basically, I use a global replace and pass "\\xA3" as the target to replace and nothing happens. I'm not using getBytes() and traversing each byte.

 

Stephen

Developer
Posts: 73
Registered: ‎11-02-2009
My Device: Not Specified

Re: blackberry £ symbol and \xA3

As addendum to my previous message, we are using JDE 4.2 for the blackberry app and the simulator is an 8310.

Retired
Posts: 2,559
Registered: ‎10-16-2009
My Device: BlackBerry Z10
My Carrier: Bell

Re: blackberry £ symbol and \xA3

Hi Stephen,

 

The reason nothing happens is because at this time the £ character is understood to be a £ by the device, it doesn't get changed/garbled until the application begins uploading and the text is re-encoded. Give Ted_Hopp's solution a try, this will code the character to an XML-safe escape character.

 

Regards,

Garett
@garettBeuk
--
Goodbye everybody!
Developer
Posts: 73
Registered: ‎11-02-2009
My Device: Not Specified

Re: blackberry £ symbol and \xA3

Cheers for the feedback on this, that fix works a treat Smiley Happy