How to get the last character of a jobject in jni? - android-ndk

I have a jobject (ts) and I want to get the last character of it and then want to compare if it is equal to 0 or s (It is a mixture of numbers and characters). What can be the easiest way to do it?
jmethodID methodId_ts = env->GetMethodID(s_clazz,"toCharsString","()Ljava/lang/String;");
jobject ts = env->CallObjectMethod(signature,methodId_ts);

Just use ts.charAt(ts.length()-1):
jclass cls_String = env->GetObjectClass();
jmethodID mid_String_length = env->GetMethodID(cls_String, "length", "()I");
jmethodID mid_String_charAt = env->GetMethodID(cls_String, "charAt", "()C");
jint len = env->CallIntMethod(ts, mid_String_length);
jchar c = env->CallCharMethod(ts, mid_String_charAt, len-1);

Related

VC++ How to SUM two biginteger numbers

Say I have
static System::Numerics::BigInteger MinimoNumero; int16_t Uno = 1;
static System::Numerics::BigInteger MaximoNumero;
static const std::string MaximoNumeroString = "91389681247993671255432112333333333333333333333333333333333333333333333333333333333333333333333333333333";
MaximoNumero = System::Numerics::BigInteger::Parse(marshal_as<String^>(MaximoNumeroString));
MinimoNumero = System::Numerics::BigInteger::Parse("1");
How can I SUM 1 to MaximoNumero so I want a result as BigInteger as 91389681247993671255432112333333333333333333333333333333333333333333333333333333333333333333333333333334
If I use
System::Numerics::BigInteger NUM = MinimoNumero + MaximoNumero;
then I got error "more than one operator "+" matches these operands.."
You can't (generally) use the plain arithmetic operators on the BigInteger type. Instead, call the relevant member function(s) of the BigInteger type – in this case, the Add function:
System::Numerics::BigInteger NUM = System::Numerics::BigInteger::Add(MinimoNumero, MaximoNumero);

Conversion of character code for a letter to a string containing the letter itself in MSVC

MSVC 2015 CLI: I used the following code to convert the unicode for the letter A (i.e. decimal 65) into a string containing the letter itself:
int i = 65;
char c = (char)(i);
return c.ToString();
The string returned is not "A" but "65"
To resolve, I had to use the std string, i.e.
int i = 65;
char c = (char)(i);
std::string MyStdString = &c;
String^ d = gcnew String(MyStdString.c_str());
return d;
The string returned is "A"
There must be a way to solve this without the second approach. Although this solved my issue, I would be grateful for help on syntax for the first approach, and to understand why it won't work as it is.
You are mixing between char and Char(Note Capital C).
Following code should give you expected result:
int i = 65;
Char c = i;
Console::WriteLine(c.ToString());

How do I parse a string into a number with Dart?

I would like to parse strings like 1 or 32.23 into integers and doubles. How can I do this with Dart?
You can parse a string into an integer with int.parse(). For example:
var myInt = int.parse('12345');
assert(myInt is int);
print(myInt); // 12345
Note that int.parse() accepts 0x prefixed strings. Otherwise the input is treated as base-10.
You can parse a string into a double with double.parse(). For example:
var myDouble = double.parse('123.45');
assert(myDouble is double);
print(myDouble); // 123.45
parse() will throw FormatException if it cannot parse the input.
In Dart 2 int.tryParse is available.
It returns null for invalid inputs instead of throwing. You can use it like this:
int val = int.tryParse(text) ?? defaultValue;
Convert String to Int
var myInt = int.parse('12345');
assert(myInt is int);
print(myInt); // 12345
print(myInt.runtimeType);
Convert String to Double
var myDouble = double.parse('123.45');
assert(myInt is double);
print(myDouble); // 123.45
print(myDouble.runtimeType);
Example in DartPad
As per dart 2.6
The optional onError parameter of int.parse is deprecated. Therefore, you should use int.tryParse instead.
Note:
The same applies to double.parse. Therefore, use double.tryParse instead.
/**
* ...
*
* The [onError] parameter is deprecated and will be removed.
* Instead of `int.parse(string, onError: (string) => ...)`,
* you should use `int.tryParse(string) ?? (...)`.
*
* ...
*/
external static int parse(String source, {int radix, #deprecated int onError(String source)});
The difference is that int.tryParse returns null if the source string is invalid.
/**
* Parse [source] as a, possibly signed, integer literal and return its value.
*
* Like [parse] except that this function returns `null` where a
* similar call to [parse] would throw a [FormatException],
* and the [source] must still not be `null`.
*/
external static int tryParse(String source, {int radix});
So, in your case it should look like:
// Valid source value
int parsedValue1 = int.tryParse('12345');
print(parsedValue1); // 12345
// Error handling
int parsedValue2 = int.tryParse('');
if (parsedValue2 == null) {
print(parsedValue2); // null
//
// handle the error here ...
//
}
void main(){
var x = "4";
int number = int.parse(x);//STRING to INT
var y = "4.6";
double doubleNum = double.parse(y);//STRING to DOUBLE
var z = 55;
String myStr = z.toString();//INT to STRING
}
int.parse() and double.parse() can throw an error when it couldn't parse the String
Above solutions will not work for String like:
String str = '123 km';
So, the answer in a single line, that works in every situation for me will be:
int r = int.tryParse(str.replaceAll(RegExp(r'[^0-9]'), '')) ?? defaultValue;
or
int? r = int.tryParse(str.replaceAll(RegExp(r'[^0-9]'), ''));
But be warned that it will not work for the below kind of string
String problemString = 'I am a fraction 123.45';
String moreProblem = '20 and 30 is friend';
If you want to extract double which will work in every kind then use:
double d = double.tryParse(str.replaceAll(RegExp(r'[^0-9\.]'), '')) ?? defaultValue;
or
double? d = double.tryParse(str.replaceAll(RegExp(r'[^0-9\.]'), ''));
This will work for problemString but not for moreProblem.
you can parse string with int.parse('your string value');.
Example:- int num = int.parse('110011'); print(num); // prints 110011 ;
If you don't know whether your type is string or int you can do like this:
int parseInt(dynamic s){
if(s.runtimeType==String) return int.parse(s);
return s as int;
}
For double:
double parseDouble(dynamic s){
if(s.runtimeType==String) return double.parse(s);
return s as double;
}
Therefore you can do parseInt('1') or parseInt(1)
void main(){
String myString ='111';
int data = int.parse(myString);
print(data);
}
String age = stdin.readLineSync()!; // first take the input from user in string form
int.parse(age); // then parse it to integer that's it
You can do this for easy conversion like this
Example Code Here
void main() {
var myInt = int.parse('12345');
var number = myInt.toInt();
print(number); // 12345
print(number.runtimeType); // int
var myDouble = double.parse('123.45');
var double_int = myDouble.toDouble();
print(double_int); // 123.45
print(double_int.runtimeType);
}

How to Copying a folder in VC++?

How to copy a folder from one drive to other drive in VC++ ...?
I have come this far
String^ SourcePath = Directory::GetCurrentDirectory();
String^ DestinationPath = "c:\\Test";
CString s(SourcePath) ;
CString d(DestinationPath);
Directory::CreateDirectory(DestinationPath);
SHFILEOPSTRUCT* pFileStruct = new SHFILEOPSTRUCT;
ZeroMemory(pFileStruct, sizeof(SHFILEOPSTRUCT));
pFileStruct->hwnd = NULL;
pFileStruct->wFunc = FO_COPY;
pFileStruct->pFrom = (LPCWSTR)s;//"D:\test_documents\test1.doc";
pFileStruct->pTo = (LPCWSTR)d;
pFileStruct->fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR ;
bool i = pFileStruct->fAnyOperationsAborted ;
int status = SHFileOperation(pFileStruct);
if(status == 0)
{
return true;
}
return false;
the status is showing 2 instead of zero , can some one tell me why..?
Usually a String^ points to a managed string object. The SHFILOPSSTRUCT must befilled with pointers to unmanaged wchar_t. So you must pin the strings and convert. You tried to use the CString class as conversion helper.
Use the PtrToStringChars instead to get valid strings in pTo and pFrom:
http://msdn.microsoft.com/en-us/library/d1ae6tz5(VS.80).aspx
The read of the fAnyOperationsAborted member is not required for the operation.

stick integer to string and char*

How can I add an integer variable to a string and char* variable? for example:
int a = 5;
string St1 = "Book", St2;
char *Ch1 = "Note", Ch2;
St2 = St1 + a --> Book5
Ch2 = Ch1 + a --> Note5
Thanks
The C++ way of doing this is:
std::stringstream temp;
temp << St1 << a;
std::string St2 = temp.str();
You can also do the same thing with Ch1:
std::stringstream temp;
temp << Ch1 << a;
char* Ch2 = new char[temp.str().length() + 1];
strcpy(Ch2, temp.str().c_str());
for char* you need to create another variable that is long enough for both, for instance. You can 'fix' the length of the output string to remove the chance of overrunning the end of the string. If you do that, be careful to make this large enough to hold the whole number, otherwise you might find that book+50 and book+502 both come out as book+50 (truncation).
Here's how to manually calculate the amount of memory required. This is most efficient but error-prone.
int a = 5;
char* ch1 = "Book";
int intVarSize = 11; // assumes 32-bit integer, in decimal, with possible leading -
int newStringLen = strlen(ch1) + intVarSize + 1; // 1 for the null terminator
char* ch2 = malloc(newStringLen);
if (ch2 == 0) { exit 1; }
snprintf(ch2, intVarSize, "%s%i", ch1, a);
ch2 now contains the combined text.
Alternatively, and slightly less tricky and also prettier (but less efficient) you can also do a 'trial run' of printf to get the required length:
int a = 5;
char* ch1 = "Book";
// do a trial run of snprintf with max length set to zero - this returns the number of bytes printed, but does not include the one byte null terminator (so add 1)
int newStringLen = 1 + snprintf(0, 0, "%s%i", ch1, a);
char* ch2 = malloc(newStringLen);
if (ch2 == 0) { exit 1; }
// do the actual printf with real parameters.
snprintf(ch2, newStringLen, "%s%i", ch1, a);
if your platform includes asprintf, then this is a lot easier, since asprintf automatically allocates the correct amount of memory for your new string.
int a = 5;
char* ch1 = "Book";
char* ch2;
asprintf(ch2, "%s%i", ch1, a);
ch2 now contains the combined text.
c++ is much less fiddly, but I'll leave that to others to describe.
You need to create another string large enough to hold the original string followed by the number (i.e. append the character corresponding to each digit of the number to this new string).
Try this out:
char *tmp = new char [ stelen(original) ];
itoa(integer,intString,10);
output = strcat(tmp,intString);
//use output string
delete [] tmp;

Resources