My String looks like :
std::wstring replacePattern = L";FOLD PTP %1 Vel=100 % PDAT%2";
I am expecting to put values on it :
CString replaceString;
replaceString.Format(replacePattern.c_str(), posData.m_posName, posData.m_posName)
Run Time :Error :
Expression: ("Incorrect format specifier", 0)
The value can be string or integer.
Please suggest.
If you need to use %1 and %2 then consider using the FormatMessage CString member instead. And, as mentioned, use %% if you want a % character.
But using FormatMessage you can do what you want.
So it would be something like:
std::wstring replacePattern = L";FOLD PTP %1% Vel=100 %% PDAT%2!d!";
CString replaceString;
replaceString.FormatMessage(replacePattern.c_str(), posData.m_posName, posData.m_posName)
If you want something like a integer you use:
%1!d!
Just change the number to indicate the variable you are using.
If you want something like a CString you use:
%1%
The help topic above provides more details. So, whilst you can amend your format string so that CString::Format will work, it does tie down the order of your values. But if due to localization you need ability to have values in different order, consider using CString::FormatMessage instead.
MFC/ATL's CStringT::Format doesn't support positional format specifiers, so %1, %2, and so on aren't legal type fields. You'll have to use %d (for integers) or %s for strings. If you need a percent sign in your format string, you need to escape it using %% (see printf Type Field Characters).
Your pattern should look like
std::wstring replacePattern = L";FOLD PTP %d Vel=100 %% PDAT%d";
or
std::wstring replacePattern = L";FOLD PTP %s Vel=100 %% PDAT%s";
depending on the data type of posData.m_posName.
Related
So I have a set of different strings made if a certain key is pressed using keyPressed(), but some of those strings i want to convert to characters and tried doing so like this:
char keyChar = keyChanged.charAt(0);
except now i get a nullpointerexpression. if it matters, keyChanged would be a 1 letter string like "r".
You can avoid a NullPointerException like this:
if (keyChanged != null){
char keyChar = keyChanged.charAt(0);
}
Solved it, It was pretty stupid on my part, I just set the original keyChanged variable as a character instead.
%let vc = 12025;
ideal output (with format comma ) is 12,025;
but %put %sysfunc(put(&vc,comma6.)) seems not working. Error as below.
ERROR: The PUT function referenced in the %SYSFUNC or %QSYSFUNC macro function is not found.
The PUT function is not available with %SYSFUNC, however you can use PUTN for numeric values, or PUTC for character.
Try :
%put %sysfunc(putn(&vc,comma6.));
An alternative to using the putn() function to format values returned by %sysfunc() is to use the little known 2nd parameter of %sysfunc() like so:
%let vc = 12025;
%put %sysfunc(sum(&vc),comma6.);
The second argument applies a format to the result returned by whatever function %sysfunc() is calling. In the above example, I'm just summing a number by itself which effectively just returns the number. If it was a character value, I could use the cats() function.
Worth noting as it will simplify code if you want to do something like:
%put %sysfunc(putn(%sysfunc(date()),date9.));
as it becomes:
%put %sysfunc(date(),date9.);
I have following output with sprintf,
getcmvol:get CM CLI voltage
getcmfirmwareversion: displays CM cli firmware version
getcmserialnum: display CM serial number
I wanted it to output like below,
getcmvol--------------------: get CM CLI voltage
getcmfirmwareversion----: displays CM cli firmware version
getcmserialnum------------: display CM serial number
Pleas Ignore ------- here since this editor is not considering the spaces I have used (hyphen or minus) -----.
printf formats(\t, %3d etc) are not working any help would be appreciated.
So as I understand you problem: You want to output a string field, left-justified with a certain width. To get your a string with this property you could do something like:
... = sprintf("%-20s: %d\n", "getcmvol", voltage);
... = sprintf("%-20s: %d\n", "getcmfirmwareversion", version);
...
The format strings of printf or sprintf are explained in detail e.g. here. In the above format-string %-20s means a 20 chars wide string field, which is left-justified.
I am currently learning c++/cli and I want to convert a character to its ASCII code decimal and vice versa( example 'A' = 65 ).
In JAVA, this can be achieved by a simple type casting:
char ascci = 'A';
char retrieveASCII =' ';
int decimalValue;
decimalValue = (int)ascci;
retrieveASCII = (char)decimalValue;
Apparently this method does not work in c++/cli, here is my code:
String^ words = "ABCDEFG";
String^ getChars;
String^ retrieveASCII;
int decimalValue;
getChars = words->Substring(0, 1);
decimalValue = Int32:: Parse(getChars);
retrieveASCII = decimalValue.ToString();
I am getting this error:
A first chance exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll
Additional information: Input string was not in a correct format.
Any Idea on how to solve this problem?
Characters in a TextBox::Text property are in a System::String type. Therefore, they are Unicode characters. By design, the Unicode character set includes all of the ASCII characters. So, if the string only has those characters, you can convert to an ASCII encoding without losing any of them. Otherwise, you'd have to have a strategy of omitting or substituting characters or throwing an exception.
The ASCII character set has one encoding in current use. It represents all of its characters in one byte each.
// using ::System::Text;
const auto asciiBytes = Encoding::ASCII->GetBytes(words->Substring(0,1));
const auto decimalValue = asciiBytes[0]; // the length is 1 as explained above
const auto retrieveASCII = Encoding::ASCII->GetString(asciiBytes);
Decimal is, of course, a representation of a number. I don't see where you are using decimal except in your explanation. If you did want to use it in code, it could be like this:
const auto explanation = "The encoding (in decimal) "
+ "for the first character in ASCII is "
+ decimalValue;
Note the use of auto. I have omitted the types of the variables because the compiler can figure them out. It allows the code to be more focused on concepts rather than boilerplate. Also, I used const because I don't believe the value of "variables" should be varied. Neither of these is required.
BTW- All of this applies to Java, too. If your Java code works, it is just out of coincidence. If it had been written properly, it would have been easy to translate to .NET. Java's String and Charset classes have very similar functionality as .NET String and Encoding classes. (Encoding to the proper term, though.) They both use the Unicode character set and UTF-16 encoding for strings.
More like Java than you think
String^ words = "ABCDEFG";
Char first = words [0];
String^ retrieveASCII;
int decimalValue = ( int)first;
retrieveASCII = decimalValue.ToString();
Sometimes in Strings I see something like this %1$s or this %2$d. Can somebody explain to me how to read such things?
Check this document http://download.oracle.com/javase/1,5.0/docs/api/java/util/Formatter.html#syntax the string is basically broken in
%[argument_index$][flags][width][.precision]conversion
From your example %1$s,
% means replace with a parameter
1$ is the position in the parameter array.
s signals that the parameter is a string.
This is taken from Java, but a lot of programming languages use the same syntax for string formatting.
the % stand for the relative argument position, and the "s" or "d" (or others) stands for the type.
This is used to format a string through the printf functions
format= 'The %2$s contains %1$04d monkeys';
printf(format, num, location);
see the printf docs of the langage you use to get all the details (there is a lot)