[Q] How to convert ASCII character to decimal number in Android? - Java for Android App Development

Hi!
I'm working on an Android app. I have a TextView which contains an ASCII character. This character changes every 2 hours. I need to be able to read this character and convert it to decimal number, and then write it to an another TextView. So let's say the character is "[" and it's decimal value is 91. 2 hours later this character changes to "U" and it's decimal value is 85.
Can anyone help me what kind of code should I use in my app to be able to convert ASCII character to decimal number?
Thanks for helping.

adamhala007 said:
Hi!
I'm working on an Android app. I have a TextView which contains an ASCII character. This character changes every 2 hours. I need to be able to read this character and convert it to decimal number, and then write it to an another TextView. So let's say the character is "[" and it's decimal value is 91. 2 hours later this character changes to "U" and it's decimal value is 85.
Can anyone help me what kind of code should I use in my app to be able to convert ASCII character to decimal number?
Thanks for helping.
Click to expand...
Click to collapse
You can use an implicite cast:
Code:
char c = '[';
int decimal = c; //the decimal value
//for output:
Log.d("Decimal value", "" + decimal);

Related

[Q] Converting an editable text box to be used as a integer

I have a edit able text box that is next to a button lets say onclick of the button it will take the text from the editable text box and converted to an integer so it can be subracted by.. oh lets say 32 so my first method was this.
number = EText.getText();
textOutcome.setText(number - 32);
this gave me an error of
The operator - is undefined for the argument type(s) Editable, int
my second attempt was
textOutcome.setText((Integer.parseInt(EText.getText().toString()) - 32));
This gave me no errors and it ran fine untill i clicked the button which causes the command above to run. and it force closed my application.
Does any one have any ideas i can do, or any sample code?
Thanks,
Blue.
Bumping the topic.
Please can anyone help me with this..!
Have you tried stepping through the code with the debugger? Otherwise, the code you posted looks fine. There is probably something else at play here.
Basicall it force closes whea subtraction is made..
Sent from my Samsung Epic 4g.
Ahh the debugger found something early in the code thanks for the idea !
You have to do any mathematical arithmetic with the integer type, not with string, So when you extract the number from the EditText you have to convert the string into a integer, or whatever type of number it may be.
WHen you have the integer then you can apply the arithmetic.
when this is done you then have to convert the total into a string type again.
Hope this helps.
So basically im going to write a little of code off the top of my head which obviously can have errors. Since im not testing it.
EditText txbox = (EditText)findViewById(R.id.EditText01);
String txdata = txbox.getText().toString();
int txval = Integer.ParseInt(txdata);
//you should make sure that the value is an integer, if not i think //Integer.ParseInt() will just cut off the rest if its irrational.
//Now you can do arithmetic//
int b = 2;
int t;
t = txval - b;
//The total value is in variable t.
So lets convert this into a string and put it back into an object like edittext or textview
txbox.setText(String.valueOf(t));
//Thats all, hope that helps.

Collada Loader

I would like to point out that the example at google code of a Collada Loader is misleading. (code.google.com/p/androidcolladaloader)
The way to handle data in the characters(char[] ch, int start, int length) callback funtion should be done in the endElement(String uri, String localName, String name) callback function. This is because data between XML tags can be splitted by the sax xml parser and then call the characters function multiple times. The solution to this I found at coderanch:
coderanch.com/t/432967/XML/Missing-characters-SAX-Parser
Where Ulf Dittmer suggests:
Yes. The one you have apparently assumes that all text data is returned in a single chunk (I can't tell for sure since you didn't post the full code of the method). You need to change it so that it still works if the text is returned in several chunks. One way to do that would be to append the text to a StringBuffer (which would be field in your handler class). Once the end element of whatever tag surrounds this text is reached, you can handle the text itself.
Click to expand...
Click to collapse
An example of working with a stringBuffer in the characters function is shown here:
onjava.com/pub/a/onjava/2002/06/26/xml.html?page=2
Note that the start and length argument given with the characters call back function are not there for nothing!

[Q] Convert Editable to String?

I am making a dialog box that the user can enter text into as input. I am using this sample code http://www.tutorials-android.com/learn/How_to_prompt_user_input_with_an_AlertDialog.rhtml
In that example it uses "String value = input.getText();" but the Eclipse says it doesn't work and makes me change 'value' to type 'Editable'. How do I convert it back into a string?
Thanks in advance
hyperbyteX said:
I am making a dialog box that the user can enter text into as input. I am using this sample code http://www.tutorials-android.com/learn/How_to_prompt_user_input_with_an_AlertDialog.rhtml
In that example it uses "String value = input.getText();" but the Eclipse says it doesn't work and makes me change 'value' to type 'Editable'. How do I convert it back into a string?
Thanks in advance
Click to expand...
Click to collapse
Try using "String value = input.getText().toString();"
thanks I got it!

Quick variable type question.

I have an EditText object (et_travel) on my screen that's asking for miles traveled. I grab that data like this:
Code:
float travel = Float.parseFloat(et_travel.getText().toString());
if(travel > 40000){
I just discover that if someone puts 40000 in the EditText, everything works fine, but if they put 40,000 (adding a comma to the number), I force close on the float travel = ...statement.
How can I evaluate the number without having a problem from the user adding a comma?
you should parse the string before you run it through Float.parseFloat() so that there are no bad characters in the string. use some of the String methods to find and remove bad characters.
Also that parsefloat method probably throws some sort of exception. You could catch it and let your user know that there value is invalid and to try again
Sent from my Desire HD using XDA App
Set the properties on the edittext so it accepts numbers only.
android:inputtype="numberDecimal|numberSigned"
________________________________
http://ron-droid.blogspot.com

APK development help - encoding characters from a string

I am trying to swap out characters from one string to those in another (like the simple alphabetic offset code people used as kids), the code I am using is
public void convertMe(){
//lets convert the text
rInput = etrInput.getText().toString();
//lets extract the input characters to a chararray
char[] inputArray = rInput.toCharArray();
//lets get the numbers and make them a string
rNumbers = etrNumbers.getText().toString();
//lets extract them and place them in an chararray
char[] numbersArray = rNumbers.toCharArray();
for (int i=0; i<inputArray.length; i++){
rResult = rNumbers.replace(numbersArray, inputArray);
}
}
Clarification of the behaviour of my code above:
If the string rInput is smaller than the rNumbers array then the app crashes. If not then the only character that is encoded is the last character looped through the array.
I would like to be able to 'encode' all characters in the array, and also be able to encode any number of characters, not be limited to matching the length of the numbersArray.
How can I deal with the arrays of different lengths to substitute the values?
rInput must be 10character word
rNumbers can be any length string but only containing numbers.
The result should be that the numbers are "coded" in simple terms using the letters from the rInput charArray:
ACHROMATIC
0123456789
So encoding 5501 should give the answer MMAC, or 5512345 should give MMCHROM.
User enters a 10character word with no 2 characters the same (like the example achromatic)
User can then enter a series of numbers. (5501)
The substitution should then take the int value of each digit entered by the user, look up that value from the first word and substitute that character:
The fifth letter in the word achromatic is m (assuming you count from 0). The result of 5501 being substituted should be MMAC.
Thanks;
Andy
Anyone able to give me a steer on how i should adjust my code for this to work?

Categories

Resources