Convert a char to a String - string

I want to convert a char to a String in the following way:
char aaa = '\uE001';
I want to obtain a string with the value of "\uE001" so I can use substring(2) to obtain only "E001". Is that possible? Please help

Well, the character itself is a single character, U+E001. It has the hex value 0xE001. If you want that value as an integer, just use:
int unicodeValue = aaa;
You can then convert that integer value to hex in various ways, if you really need to, for example:
String hex = Integer.toString(unicodeValue, 16);
(That's assuming that overload is available on java-me.)
... or Integer.toHexString if that's available but Integer.toString(int, int) isn't.
Why do you want this value though? If you could clarify that, we may be able to give you more useful advice.

Integer.toHexString((int)aaa) ;
..and no substring() required.

This is the simple like that
char aaa = '\uE001';
String s=String.valueOf(aaa);

You can take any integer value and create a hex string from it like this:
String s = Integer.toHexString(num);
so Jon Skeet is on the right track. You can:
char aaa = '\uE001';
int num = aaa;
String hex = Integer.toHexString(num); //now contains "e001"

Related

how to separate a string char by char and add a symbol after in c#

Any idea how i can separate a string with character and numbers, for example
12345ABC678 to make it look like this
1|2|3|4|5|A|B|C|6|7|8??
Or if this is not possile, how can i take this string a put every character or nr of it in a different textBox like this?
You can use String.Join and String.ToCharArray:
string input = "12345ABC678";
string result = String.Join("|", input.ToCharArray());
Instead of ToCharArray(creates a new array) you could also cast the string to IEnumerable<char> to force it to use the right overload of String.Join:
string result = String.Join("|", (IEnumerable<char>)input);
use
String aString = "AaBbCcDd";
var chars = aString.ToCharArray();
Then you can loop over the array (chars)

How to get the X-th position of integer from a string?

I have a string that says "12345".
I want to take the 3rd element from the string to convert it to integer (which in this case, the 3rd element would be '3').
How would I do so ?
I heard that you can use Integer.parseInt(s) but this will return the whole integer of the String.
I just want one element from it, and at the x-th position.
I think the easiest way would be to simply use String.SubString. Something like:
string number = "12345";
string element = number.SubString(2, 1);
Where 2 is the position of the third character (remember it's 0 indexed) and 1 is the number of characters to return. You can then turn that into an integer if you want.
you can do some thing like that...
int number = Integer.parseInt(givenString.charAt(xPosition));
Hope it helps.
The [charAt() method] (http://www.w3schools.com/jsref/jsref_charat.asp) should get you what you need

Is it possible to compare two characters in Processing?

I am a novice programmer and I am trying to compare two characters from different strings, such that I can give an arbitrary index from each string and check to see if they match. From the processing website it seems that you can compare two strings, which I have done, but when I try to do so with characters it seems that the arguments (char,char) are not applicable. Can someone tell me where I am going wrong? Thanks.
You can use String's charAt() method/function to get character from each string at the desired index, then simply compare:
String s1 = ":)";
String s2 = ";)";
void setup(){
println(CompareCharAt(s1,s2,0));
println(CompareCharAt(s1,s2,1));
}
boolean CompareCharAt(String str1,String str2,int index){
return s1.charAt(index) == s2.charAt(index);
}
Note that when you're comparing strings == doesn't help, you need to use String's equal()
String s1 = ":)";
String s2 = ";)";
println(s1.equals(s2));
println(s1.equals(":)"));
Also, if data comes from external sources, it's usually a good idea to compare both strings at using the same case:
println("MyString".equals("myString"));
println("MyString".toLowerCase().equals("myString".toLowerCase()));
maybe you can pass the argument after converting(typecasting) the char to string.
(string(char),string(char))
Yep. Just use == as it gets interpreted as a char datatype.
This is assuming you've split the char from the String...
char a = 'a';
char b = 'a';
if(a == b) {
// etc
}
As mentioned above, use .equals() for String comparison.
String a = "a";
String b = "a";
if(a.equals(b)) {
// etc
}
Also, the proper way to cast a char as a String is str() not string()

Arduino issue: String to float adds two zeros instead of the correct integer

Code snippet:
Serial.println(sensorString); //so you can see the captured string
char carray[sensorString.length() + 1]; //determine size of the array
Serial.println(sizeof(carray));
sensorString.toCharArray(carray, sizeof(carray)); //put sensorString into an array
float sensorStringFloat = atoi(carray); //convert the array into an Integer
Serial.println(sensorStringFloat);
Serial.println(sensorStringFloat) prints out 5.00 instead of the correct float value of 5.33. Why is that and how do I fix this issue? I would eventually like to pass sensorStringFloat over to:
aJson.addNumberToObject(sensor, "ph", sensorStringFloat);
atoi converts a numeral in ASCII to an integer. The comment on that line also says it converts to an integer. So you got an integer result, 5. To convert to floating-point, consider using atof. (Note that “f” stands for floating-point, not “float”. atof returns a double.)
you should pass another parameter which defines the format, in this case it is the number of digits after the floating point.
Serial.println(sensorString,2);
String temp = String (_float, 0);
say float x;
convert to String using
String _temp = String(x, 0);
The second parameter 0... says i want no trailing zeros.
Caution: However this is only suitable for whole numbers.
This solution would not work for say... 1.24
You'll get just 1.

Does groovy "cast" a string to an integer based on its hashCode/ASCII Code?

I started coding with groovy today and I notices that if I take the following code:
int aaa = "6"
log.info(aaa)
The output I get is:
54 <-- (ASCII Code for '6')
If I assign aaa with any number which is beyond the range of 0..9 I get a class cast exception.
Looks like if the string is actually a single character - groovy converts its ASCII code/hashCode.
I tried this code:
int aaa = "A"
log.info(aaa)
And the output I got was:
65 <-- (ASCII code for 'A')
What is the official reason for this?
Is it because groovy automatically changes "A" into 'A'?
As Jochen says here in the JIRA; Strings of length 1 are converted to chars if needed (and by putting it into an int variable, it is assuming that that is what you want to do)
If you want to accept bigger numbers, you can do:
int a = '12345' as int
And that will convert the whole number to an int.

Resources