| Multi-byte | Unicode |
| char | TCHAR |
| strcat_s() | _tcscaft_s() |
| strcpy_s() | _tcscpy_s() |
| strncpy_s() | _tcsncpy_s() |
| strien() | _tcsien() |
| sprinif_s() | _stprintf_s() |
Showing posts with label Tips for Programming. Show all posts
Showing posts with label Tips for Programming. Show all posts
Wednesday, June 10, 2020
Multi-byte vs. Unicode
Friday, May 29, 2020
How to get paths defined in the system environment
char path[_MAX_PATH];
const char* varName = "[system environment variable name]";size_t len;
getenv_s(&len, path, 80, varName);
Print out a (decimal) number as a hexadecimal number
int decNum = 123456;
char hexNum[16];
// decimal number to hexadecimal number
sprintf_s(hexNum, 16, "%X", decNum);
printf("%d -> %s \n", decNum, hexNum);
// hexadecimal number to decimal number
sprintf_s(hexNum, 16, "FF");
decNum = (int)strtol(hexNum, nullptr, 16);
printf(" %s -> %d \n", hexNum, decNum);
char hexNum[16];
// decimal number to hexadecimal number
sprintf_s(hexNum, 16, "%X", decNum);
printf("%d -> %s \n", decNum, hexNum);
// hexadecimal number to decimal number
sprintf_s(hexNum, 16, "FF");
decNum = (int)strtol(hexNum, nullptr, 16);
printf(" %s -> %d \n", hexNum, decNum);
Wednesday, September 19, 2012
Short memo for angle calculation using atan2
Angle and arc-tangent function (in C/C++)
θ = atan2(x,y)
θ' = -(360 - θ)
#1: Bearing and an arc-tangent function
b = atan2(E, N)
Angle between 'N' axis and a vector.
CW is positive.

#2: Rotation angle about an axis and an arc-tangent function
ω = -atan2(x, y)
Angle between 'Y' axis and a vector.
CCW is positive.
Monday, September 3, 2012
Visual Studio 2010 C++ tips: for loop initializer
In Visual Studio 2010, for a loop's initialization variable is not available out of for loop scope, unlike Visual C++ 6.0. Whenever we compile old codes (generated in VC++ 6.0) using newest version, it annoys us. Then, take easy, and change project option to release your blood pressure.
Go to "Project property pages - C/C++ - Language - Force Conformance in For Loop Scope", and select "No (/Zc:forScope-)".
For more information, "http://msdn.microsoft.com/en-us/library/84wcsx8x.aspx".
Go to "Project property pages - C/C++ - Language - Force Conformance in For Loop Scope", and select "No (/Zc:forScope-)".
For more information, "http://msdn.microsoft.com/en-us/library/84wcsx8x.aspx".
Visual Studio 2010 C++ tips: Deprecated functions
strcpy, sscanf, fscanf.... etc. There functions are not available anymore in recent Visual Studio .Net C++. Instead of these function, it is recommend to use new functions such as strcpy_s, sscanf_s, fscanf_s ... etc. If you are not pleased to change your old style functions with new ones, you can avoid this problem easily by using this define statement, "#define _CRT_SECURE_NO_DEPRECATE".
For more information about deprecated functions, refer to "http://msdn.microsoft.com/en-us/library/ms235384(v=vs.80).aspx"
For more information about deprecated functions, refer to "http://msdn.microsoft.com/en-us/library/ms235384(v=vs.80).aspx"
Monday, July 30, 2012
Visual Studio 2010 C++ tips: Instead of istream::eatwhite
istream::eatwhite is not supported anymore by recent visual studio C++.
So far, I've used an alternative function implemented by myself. I realized recently that there is a simpler and easier way to avoiding the effort.
In Visual Studio 2010 and 2008, the use of "ws" is the way to skips white space in the stream.
For example, in order to skip white spaces end of each line of a text file.
fstream imufile;
imufile.open(fname, ios::in);
imufile>>record0>>record1>>ws;
For more information,
http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=KO-KR&k=k(%22ISTREAM%2fSTD%3a%3aWS%22);k(%22STD%3a%3aWS%22);k(WS);k(DevLang-%22C%2B%2B%22);k(TargetOS-WINDOWS)&rd=true
Thursday, July 28, 2011
Visual Studio 2010 C++ tips: sscanf_s and fopen_s
-. sscanf vs. sscanf_s
구 버전과 비교하여 파일 및 스트링 관련 함수들에 "_s"가 붙은 함수들이 제공되고 있다. 그러한 함수들 중에는 buffer overflow를 방지하기위한 목적 buffer 사이즈를 명시하도록 규정하고 있다.예를들어strcpy와strcpy_s를 비교해보면, 새로제공되는 함수는 target과source사이에 buffer 사이즈를 입력해야 한다.
"_s"가 붙은 신규함수와 붙지않은 구함수 사이에 함수원형에 있어서 차이점이 없는 경우는, #define을 사용하여 손쉽게 구함수를 신함수로 전환할 수 있으나, 주의해야할 함수가 있다. sscanf_s는 원형이 구 함수인 sscanf와 동일한것 처럼 보이고, 단순히 함수이름만 변경하여도 컴파일 에러가 발생하지 않지만, 실행시 에러가 발생하는 경우가 있다. %c 또는 %s의 포맷으로 데이터를 입력받는 변수의 경우, sizeof()를 사용하여 buffer의 사이즈를 명시하지 않는 경우, 에러가 발생하게 됨으로 주의해야 한다.
-. fopen vs fopen_s 과 fsopen
fopen_s 또한 fopen을 대신하도록 권고되고 있는데, 두 함수사이에 차이점이 있다.
outfile = fopen(filename, "w");
fopen(outfile, filename, "w");
위와 같이 파일을 open하는 경우, fopen과 달리 fopen_s를 사용하는 경우, open된 파일을 sharing이 허용되지 않는다.
따라서, sharing이 허용되도록 하기 위해서는
outfile = fsopen(filename, "w", _SH_DENYNO);
와 같이 fsopen함수와 _SH_DENYNO 플래그를 사용하여 파일을 open해야 한다.
구 버전과 비교하여 파일 및 스트링 관련 함수들에 "_s"가 붙은 함수들이 제공되고 있다. 그러한 함수들 중에는 buffer overflow를 방지하기위한 목적 buffer 사이즈를 명시하도록 규정하고 있다.예를들어strcpy와strcpy_s를 비교해보면, 새로제공되는 함수는 target과source사이에 buffer 사이즈를 입력해야 한다.
"_s"가 붙은 신규함수와 붙지않은 구함수 사이에 함수원형에 있어서 차이점이 없는 경우는, #define을 사용하여 손쉽게 구함수를 신함수로 전환할 수 있으나, 주의해야할 함수가 있다. sscanf_s는 원형이 구 함수인 sscanf와 동일한것 처럼 보이고, 단순히 함수이름만 변경하여도 컴파일 에러가 발생하지 않지만, 실행시 에러가 발생하는 경우가 있다. %c 또는 %s의 포맷으로 데이터를 입력받는 변수의 경우, sizeof()를 사용하여 buffer의 사이즈를 명시하지 않는 경우, 에러가 발생하게 됨으로 주의해야 한다.
-. fopen vs fopen_s 과 fsopen
fopen_s 또한 fopen을 대신하도록 권고되고 있는데, 두 함수사이에 차이점이 있다.
outfile = fopen(filename, "w");
fopen(outfile, filename, "w");
위와 같이 파일을 open하는 경우, fopen과 달리 fopen_s를 사용하는 경우, open된 파일을 sharing이 허용되지 않는다.
따라서, sharing이 허용되도록 하기 위해서는
outfile = fsopen(filename, "w", _SH_DENYNO);
와 같이 fsopen함수와 _SH_DENYNO 플래그를 사용하여 파일을 open해야 한다.
Monday, July 5, 2010
STL – vector 초간단 설명
vector는 container자료구조중의 하나로서 STL중에서 가장 자주 사용하는 템플릿중 하나이기도 하다. vector의 자료구조는 일반적인 배열과 유사하다. 배열과 상이한 점은 배열의 크기는 일단 정해진 이후에 고정이지만, vector는 동적으로 변할 수 있다는 점이다. 배열의 특징을 더 자세히 살펴보면 데이터가 배열내에 입력된 이후에 특정한 위치에 새로운 데이터의 삽입이 용이하지 않으며, 새로운 데이터의 삽입으로 인해 배열의 크기를 넘어서는 경우 처리가 까다롭다. 특정위치의 데이터를 삭제한 경우에도 빈 공간으로 유지하지 않고, 연속적인 데이터 구조를 유지하기 위해 삭제된 공간을 그 이후의 데이터를 이동하여 채울경우 이 또한 부차적인 처리를 해야하는 불편함이 있다. 반면, 배열은 가장 단순한 형태의 자료구조중 하나이기 때문에 구현이 쉽고, 각 데이터에 대한 random access가 용이하다. vector를 배열의 단점을 어느정도 극복한 형태 자료구조로 여길 수 있다. 즉, 데이터는 순차적으로 저장하지만, 데이터의 개수가 가변적이어도 이에 대응할 수 있다. 그러나, STL의 list와 달리 데이터의 삽입 및 삭제가 용이하지는 않다. 다음의 표는 배열, vector, list에 대한 특징을 간단히 정리한 것이다.
<vector 사용법>
#include <vector>//header file
using namespace std;//name space선언
vector<myType> myvector;//vector instance선언
myvector.push_back(newdata1);//vector에 데이터 추가 (뒤에서 추가)
myvector.push_back(newdata2);
int numElements = myvector.size();//저장된 데이터의 갯수
for(int i=0; i<numElements; i++) //일반 배열과 동일한 방식의 데이터 접근 (iterator사용도 가능하다.)
{
myType val = (myType)targetvalue;
for(vector<myType>::iterator i=myvector.begin(); i != myvector.end(); )
{
myvector.clear();//vector 데이터 모두 삭제
| Array | STL의 vector | STL의 list | |
| 가변적 크기 | X | O | O |
| 임의 위치의 데이터 삭제/삽입 | X | X | O |
| Random access | O | O | X |
<vector 사용법>
#include <vector>//header file
using namespace std;//name space선언
vector<myType> myvector;//vector instance선언
myvector.push_back(newdata1);//vector에 데이터 추가 (뒤에서 추가)
myvector.push_back(newdata2);
int numElements = myvector.size();//저장된 데이터의 갯수
for(int i=0; i<numElements; i++) //일반 배열과 동일한 방식의 데이터 접근 (iterator사용도 가능하다.)
{
myvector.at(i) = myvector.a[i]*2;//at은 특정 위치에 있는 데이터의 reference를 반환한다.
cout<<myvector[i];}
myType val = (myType)targetvalue;
for(vector<myType>::iterator i=myvector.begin(); i != myvector.end(); )
{
if(*i == val)}
{
i = myvector.erase(i);//erase는 iterator i가르키는 데이터를 삭제한 후, 그 다음 데이터를 가르키는 iterator를 반환한다. 따라서, iterator를 증가시키는 프로세스를 필요로 하지 않는다. (주의 요망).
}
else
{
i++;
}
myvector.clear();//vector 데이터 모두 삭제
Saturday, March 27, 2010
Conversion between CString & std::string
[How to convert CString to std::string]
In Visual Studio 6.0
One used to:
string mystring;
CString myCString("this is a CString");
mystring=(LPCTSTR)myCString;
However, it is not available anymore in Visual Studio 2005.
One should use "CT2CA" do like this:
string mystring;
CString myCString("this is a CString");
CT2CA myCT2CA(myCString);
mystring=myCString;
In Visual Studio 6.0
One used to:
string mystring;
CString myCString("this is a CString");
mystring=(LPCTSTR)myCString;
However, it is not available anymore in Visual Studio 2005.
One should use "CT2CA" do like this:
string mystring;
CString myCString("this is a CString");
CT2CA myCT2CA(myCString);
mystring=myCString;
Subscribe to:
Posts (Atom)
-
Reduced normatrix for photogrammetric bundle adjustment based on the collinearity equations Note: There are $m_1$ photos and $m_2$ obje...
-
Linear Intersection for a Stereo Pair There are two images. Object point vs. image point, in the first image: $$ \begin{pmatrix} X \\...
-
int decNum = 123456; char hexNum[16]; // decimal number to hexadecimal number sprintf_s(hexNum, 16, "%X", decNum); printf("%d...

