Showing a UTF16-LE encoded string in textview for Android - android

I have a UTF-16LE encoded string that comes from a server. I would like to print that string in Textview of my activity. However, the string prints with spaces in between them. So, "Hello" prints as "H e l l o" and doesn't look all that nice in my screen.
Any help is appreciated.
Thanks

Assuming you have a stream (or array of bytes) containing a UTF-16LE encoded string.
String str0 = "Hello, I am a UTF-16LE encoded String";
byte[] utf16le = str.getBytes("UTF-16LE");
If you do not convert these back & stating the character set used you will be producing a string containing a lot of 0-bytes (UTF-16LE is, obviously, 16-bit) in your resulting String.
String wrong = new String(utf16le); // This will produce crap with \0:s in it.
String correct = new String(utf16le, "UTF-16LE"); // This will be the actual string.
Note: If you dump crap String:s like these into a TextView in ICS it will remove the garbage for you and not print "H e l l o".

Related

Static String variable containing base 64 encoded bitmap cut in the middle in android

I have a static string variable which I use to send data to the server from another class.
The message I want to send contains 5 string fields (one of them is a base64 encoded bitmap string) seperated by the character "|".
My problem is that the string just cut in the middle. The only parts I see are the first field and the base64 encoded bitmap (probably not even all of it).
I tried creating the string with MessageFormat, String.format and a regular string concatenation but none seemed to work.
Does anyone have any idea why?
Here is my current code:
toSend = "UPL|" + photo +"|" + Double.toString(location.latitude) + "|" +
Double.toString(location.longitude) + "|" + mapName;
The logcat prints this:
UPL|iVBORw0KGgoAAAANSUhEUgAAAPwAAAC9CAIAAAAYzYf5AAAAA3NCSVQICAjb4U/gAAAgAElEQVR4
nGy97a4kWXIcaGZ+IvPeqq7uGQ6JoSgQAveHnnCx+7ILCCssJGqGmuEM++tmhpvtDz+RVdTuBRqo
rrr5FeHH3dzM3JP/8PsbHUlIGEf1lG6N+FxLH2dX1W3d209JtgEIfPRZVYWgcWDxtrqfB2XgdEsy
YZ+sdZgAHukVQkWmuyUBejgSFkgSgKSH+whJ0kkCGoAMiNJayAdT0Ud6qZLYvtcCkDQcKB+sm6q7
b0Ao20u0Lek8z6qDDskz57woySQAjCbJKESIBdmGOL8gsOOlMgLAtkCI9ll1pM0SyYQAkpBJe0mQ
bDOZj4XrhyT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AIw
r/jxYKF9AruL0ld6hIOLKBRrkjdRg1oYDXzQzrsXVNI+lZpPOnDFKZGiHSfTXJ7dIKumknDkDW36
pTBXfEhCgFAQIQssjUSBTH0aMpFcUncPDzqAYRoDTMRwwttVhYhD56sSa/pLaVohouaYq8rBmYAw
9/0CYIeq7IwxHwcLJGBsDLM5ZYJSt+e6DK09HfpWIRJWlbQ/JlksANIhYlWBjKenSpAUp8UqkuSt
1hNc0oKOVbtGrXrEEJZxAhR6E6rsIYVBBFXrbE9OmhRSv/1889wq4hf3QRJ8+nmvPM+nyNtAMSpt
ayAHQRzFwQtnBiPmKAhx8lY604CW1MT0Fbzy1QRTJhkhdO+cDgACwoA03IfWGSNNcDgHCGmvqhGP
do/VXVq4+i2AzjlEsomFIqeMyu62VYWpfZfM1GmVnOnxMbV5ULJeeTEpK
note that the start of the base64 is after the "UPL|" in the start of the string

change hex of integer resource to String

I am new in android.
in my value resource i create an xml layout and put this line in to it:
<integer name="mode_happy"> 0x1F60A</integer>
in my activity I want convert 0x1F60A to String. for this i create a method:
private String getStringOfEmojiCode(int emogiCode) {
StringBuilder sb = new StringBuilder();
//convert hex to char
sb.append(Character.toChars(emogiCode));
return sb.toString();
}
when I pass mode_happyto my method:
mSelectedMode =
getStringOfEmojiCode(getResources().getInteger(R.integer.mode_happy));
I receive this :
mSelectedMode: ��
but i want to get like this:0x1F60A
where is my mistake?
A formatter may be used to build the string from hex characters in a particular format:
String mSelectedMode
= String.format("0x%05X", getResources().getInteger(R.integer.mode_happy));
This worked for me, the hex which you stored as an integer was being converted to a decimal for me, but using the formatter worked. This also meant I did not have to use any other method.
Explanation:
The format 0x%05X takes in a 5 digit hex character signified by %05X with 'X' indicating hex. A lowercase x may be used which in this case would give out the output in lower case: 0x1f60a. The 0x is added to the format so as to have it as a prefix for each string. Refer to this for further details on formatting.

How to Convert HTML to String android and print that string

I followed this/this to Print Receipts in part of POS(Point of Sale) from EPSON Printer
Here I am getting data Json from URL (inside the Json Object I am getting a html print template):
{
"response": {
"status": "<table>.... </table>"
}
}
so with intent I used the above json response to a string and converted it to html:
method = "addFeedLine";
mPrinter.addFeedLine(1);
textData.append("Test print Sample string\n");**//this is sample text**
textData.append(Html.fromHtml(status + "\n"));
**//this is JSON response which is nothing but HTML code, so I am converting it to string**
Over there I have used status as a string so that whatever the content is inside that string, it is printed.
If it's is not a html but just a plain text I will print it like this
method = "addFeedLine";
mPrinter.addFeedLine(1);
textData.append(status);
Here is an example of what status looks like
"status": "The store list Sample\nSTORE DIRECTOR – XYZ\n01/01/01 16:58 6153 05 0191 134\nST# 21 OP# 001 TE# 01 TR# 747\n------------------------------\n400 OHEIDA 3PK SPRINGF 9.99 R\n410 3 CUP BLK TEAPOT 9.99 R\n445 EMERIL GRIDDLE/PAN 17.99 R\n438 CANDYMAKER ASSORT 4.99 R\n474 TRIPOD 8.99 R\n433 BLK LOGO PRNTED ZO 7.99 R\n458 AQUA MICROTERRY SC 6.99 R\n493 30 L BLK FF DRESS 16.99 R\n407 LEVITATING DESKTOP 7.99 R\n441 ** Blue Overprint P 2.99 R\n476 REPOSE 4 PCPM CHOC 5.49 R\n461 WESTGATE BLACK 25 59.99 R\n------------------------------\nSUBTOTAL 160.38\nTAX 14.43\nTOTAL 174.81\nCASH 200.00\nCHANGE 25.19\n------------------------------\nPurchased item total number\nSign Up and Save!\nWith Preferred Saving Card\n"
Now, here I have a plain HTML page:
Search Images Maps Play YouTube News Gmail Drive More »
Web History | Settings | Sign in
Louisa May Alcott’s 184th birthday
[ ] Advanced
searchLanguage
[Google Search][I'm Feeling Lucky] tools
Advertising ProgrammesBusiness Solutions+GoogleAbout GoogleGoogle.com
© 2016 - Privacy - Terms
I need to print this from an url.
Can anyone suggest me how to print this plain text?
There is no HTML tags and no JSON data.
By Html.fromHtml method you can convert HTML to String -
String strToHtml = Html.fromHtml(htmlContentInStringFormat)
Log.e(TAG,"strToHtml :: "+strToHtml);
If you really want to print it like the html it really is, I recommend you to get the primary html code status (before parsing it and so on) and to push it into a WebView like this:
webview.loadDataWithBaseURL("", status, "text/html", "UTF-8", "");
Otherwise, if you just want to print it into the screen, you can use a simple TextView to do that by text_view.setText(textData.toString())
This is the original html value:
String htmldescription = school2.getJSONObject(0).getString("description");
This is the html formatted value:
Spanned spanned = Html.fromHtml(formattedText);
And this is the String conversion:
String formattedText = spanned.toString();
Got it from here: how to save encoded html in string
If this doesn't work out you should check out the developer docs
Hope it Helps, Good Luck!
You can use a simple Regex to convert HTML template to a plain text. It detects all types of HTML tags, but there may be loopholes.
For example:
// Regex pattern
private static final String STR_PATTERN = "\\<[^\\>]*\\>";
public static String htmlToPlainText(final String template) {
// replaceAll(String regex, String replacement)
return (template.replaceAll(STR_PATTERN, ""));
}
I hope it helps

Remove garbage character from String

In my Android Application when I am reading the particular data from NFC chip it's giving garbage values as show follows which is printed on Log
����������������
I used following line to remove garbage value
str.replaceAll("[^\\p{ASCII}]", "")
but it is not working.
Please provide me solution.
That is because � is not an ASCII character. It is a unicode character with (int) � returning 65533.
And your code str.replaceAll("[^\\p{ASCII}]", "") works perfectly fine.
scala> val str ="����������������"
str: String = ����������������
scala> str.replaceAll("[^\\p{ASCII}]", "")
res8: String = ""
You need to show more code and explain what exactly you are trying to do.
Better to retrieve the data in the form of UTF-8 format then it helps. try it out.
or convert the string to UTF-8 format
i.e, String _data=new String(str.getBytes(),"UTF-8");
it returns the data in UTF-8 format
One solution using this method .replaceAll("[^\\x00-\\x7F]", "")
String str = "jorgesys���������������� was here!";
str = str.replaceAll("[^\\x00-\\x7F]", ""));
so the result of str is:
jorgesys was here!

Converting String with toCharArray results in different Characters

This is baffling me. I am grabbing a String and converting it to a Char array but the resulting characters are not the same as the original String. What gives? I've tried it one character at a time as well as trying toCharArray(). Same results.
Output:
07-21 09:58:27.700: V/meh(22907): Loaded String = [C#42126d88
07-21 09:58:27.700: V/meh(22907): Convert to Char = [C#41693070
String temp = prefManager_.getString("PrevGameState", "");
Log.v("meh", "Loaded String = " + temp);
pieceStates_ = temp.toCharArray();
Log.v("meh", "Convert to Char = " + pieceStates_.toString());
The value it outputs is not a string indeed, it's a pointer in memory. Probably you are not overriding the toString() method or there is something wrong.
The fact that the two pointers are not the same doesn't mean that the two strings are not equal (which should be compared with .equals(..) and not in any different way).
To be more precise, if pieceStates_.toString() prints [C#41693070 then the toString is not overridden and Java doesn't know how to print it. Same thing applies to the other variable. Then an array type in Java is not printable by default, you should use Arrays.toString(..) to actually see its content.
Use :
System.out.println("Convert to Char = " + String.valueOf(pieceStates_) );
String.valueOf(Character_Array)
Above method converts it back to String object.

Categories

Resources