Unicode in MySQL and C



For my application, I have to save Unicode message in MySQL database. And my C application will pick that value.

In MySQL part,

# For each database:
ALTER DATABASE database_name CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
# For each table:
ALTER TABLE table_name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
# For each column:
ALTER TABLE table_name CHANGE column_name column_name VARCHAR(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

# (Don’t blindly copy-paste this! You have to use your own values.)

Now check.

SHOW VARIABLES WHERE Variable_name LIKE 'character\_set\_%' OR Variable_name LIKE 'collation%';

For MySQL part, You can get details from



Now, what I have to do in my C application

 // these two lines extra for unicode
 mysql_options(&mysql, MYSQL_SET_CHARSET_NAME, "utf8"); 
 mysql_options(&mysql, MYSQL_INIT_COMMAND, "SET NAMES utf8"); 


Now you can use normal procedure.

ret = mysql_query(con, your_query);

Binary to Decimal and Decimal to Binary Conversion


, , ,

#include <stdio.h>
#include <math.h>

int binary_to_decimal(int binary) /* Function to convert binary to decimal.*/
int decimal = 0, i = 0, reminder;
while (binary != 0)
reminder = binary % 10;
binary /= 10;
decimal += reminder*pow(2.0,i);
return decimal;

int decimal_to_binary(int decimal) /* Function to convert decimal to binary.*/
int reminder, i = 1, binary = 0;
while (decimal != 0)
reminder = decimal%2;
decimal /= 2;
binary += reminder*i;
i *= 10;
return binary;

Introduction to Android Native Development


, , ,

When I had started Android Native Development, I had to face some problems. So, I am writing this tutorial for the beginners who is just starting.

Environment Setup:
1. Eclipse 4.2
2. JDk7
3. Android SDK
4. CDT Plugin
5. ADT Plugin
6. NDK

Now Install JDK, Eclipse. Configure SDK location. Install CDT Plugin. Then Install ADT Plugin (If you don’t install CDT, you won’t install NDK plugin).
After you install NDK plugin, you will find a new sub-menu option of Android(Windows-> Preference->Android). Set NDK location (be sure the path can’t contain a space).

Note: In some tutorial you may find that, they mentioned about Cygwin or others to compile NDK. But latest, even I used android-ndk-r8, I didn’t need those.

Create Project
1. Create a new Android project.
2. Right click on project explorer. you find “Android tools” in the menu.
3. Select “Add Native Support…”. Write your library name. You will find a new folder in project explorer (jni). jni contains a cpp file and Android.mk.
4. Now write your method in cpp.
JNIEXPORT return_type JNICALL Java_yourPackage_YourJavaClass_MethodName(JNIEnv *env, jobject thiz)

JNIEnv *env, jobject thiz MUST be in parameters. If you need more, you can add after those. As it is cpp file, you need to use extern “C”.

Here I used.
extern “C” jint Java_com_example_ndktest_MainActivity_AddValue(JNIEnv *env,
jobject thiz,
jint nValue1,
jint nValue2)

In your MainActivity class, You should write.
System.loadLibrary(“MathSDK”); // libMathSDK.so is the lib name

public native int AddValue(int nValue1, int nValue2);

Now You can use AddValue Method as a member method of MainActivity class.
You don’t need to write anything till now in Android.mk.

Now build and run.

Convert char[] to hex string

char strInput[] = "yourchardata";
char chHex[3] = "";
int nLength = strlen(strInput);

char* chResut = new char[(nLength*2) + 1];
memset(chResut, 0, (nLength*2) + 1);

for (int i = 0; i < nLength; i++)
sprintf(chHex, "%02X", strInput[i] & 0x00FF);
memcpy(&(chResut[i*2]), chHex, 2);

printf("\n%s", chResut);
delete chResut;
chResut = NULL;

Create and Delete Thread in MFC



CWinThread* m_pThread;

// Creating Thread
if (NULL == m_pThread)
m_pThread = AfxBeginThread(ThreadProc,

m_pThread->m_bAutoDelete = false;

TerminateThread forces another thread to exit. You should avoid calling it at all costs as it will stop a thread dead in it’s tracks without any chance to cleanup. This includes any CRT memory allocated.

ExitThread is for the currently running thread to stop itself nice and cleanly. When you called it above, you likely forced the main (UI) thread to exit and likely left the running threads still lingering around. Hence, your program was still running as evidenced in Task Manager. GetExitCodeThread was also likely failing since the threads had not actually exited.

But the right way to stop a thread is to cleanly signal by any clean means necessary that it should exit. Then allow the threads to exit on their own before allowing the main thread to exit.

// Deleting thread
WaitForSingleObject(m_pThread->m_hThread, INFINITE);

// get the thread's exit code (I'm not sure why you need it)
DWORD dwExitCode;
BOOL bRet = GetExitCodeThread(m_pThread->m_hThread, &dwExitCode);

// cleanup the thread
m_pThread->m_hThread = NULL;

delete m_pThread;
m_pThread = NULL;

Get console output from exe in C++


, ,

I have an executable file which prints “Hello World”;  File name “PrintOutput.exe”.

I have to get the console output that is “Hello World” in C++ project. How will I do that?

int _tmain(int argc, _TCHAR* argv[])
char   psBuffer[128];
FILE   *pPipe;

pPipe = _popen( “PrintOutput.exe”, “rt” );
if (NULL == pPipe)
printf(“error input”);
return 0;

while(NULL != fgets(psBuffer, 128, pPipe))
printf(psBuffer); // print the console output of “PrintOutput.exe”


_pclose( pPipe );



Get console output from exe in php


, ,

I have an executable file which prints “Hello World”;  File name “PrintOutput.exe”.

I have to get the console output that is “Hello World” in my PHP page. How will I do that?


// put your code here

$output = shell_exec(“PrintOutput.exe”);

echo $output;     // it will print “Hello World”




Unsorted map


, ,

In C++, most of the programmers use map to store elements formed by a combination of a key value and a mapped value, following a specific order.

But for some cases, I needed an unsorted map. I am explaining.

I have some data,of string type such as date, account number, account name, balance, debit amount, credit amount etc. All are key values here. I have to store values associated with these.

Problem is, map is sorting my data. that is account name, account number, balance, date order. I am not getting data in my inserted order.

So, How did I solve this problem?

I used.

struct Cell
string keyValue;
string data;

vector<Cell> cells;

One of the most benefits is, vector is faster than map.

Check before joining a software firm


In my job experience, I got names of many software companies. Some seem good, but there are some internal issues. If anyone miss to know any of these, he/she may suffer after joining there.

I think it is a good idea to have a checklist for this. Here it goes!


1. Does that company follow software engineering process?
In my experience, I found some senior people who STRONGLY BELIEVE that no process can be applied in their projects. Also, some even don’t know the difference between Software Engineer and Programmer.

2. How about their interview process? That is they will take your interview and you will also get idea about them.
Some companies don’t know how to take interview for Senior software engineer, software engineers or programmers. All seem same to them. So, if you are appearing interview for sr s/w engg, but their interview seems that for programmers, you should know that they will assign you task of programmers.

3. How many hours do you “actually” need to work a day, on average? Can a programmer can work more than 8hrs a day? Every overtime just add bugs in projects.

4. How frequent is overtime and working in weekend? In some companies, 11/12 hrs a day is normal working hours and their projects are always buggy. And same about weekend, on an average 3/4 weekend are added to working day in a month.

5. How friendly is the relationship among the engineers and also with management?


1. How many projects are currently going on?

2. What is the average age/length of the projects? How many years employees are involved there? that is, project is long, but for some reason, employees can not stay there.

3. Do they work on products or projects for clients? Products are so long and all are preplanned. So. company has to release 1/2 times a year with the specific features. So, work load is distributed in whole year. but if projects for clients, sometime no task, sometime so much pressure.

4. Which technologies does the company work with? which domain etc.

SALARY and other benefits:

1. Office space or location. It shows how much the company is serious about their business, how much the company are investing for this business. It shows the job security, financial strength.

2. What is the salary range for a fresher?

3. How many times in a year do your company give salary increment? Do they review in time or some months delay?

4. What is the average percentage of increment?

5. Do you feel that your job is secured? How many years employees stay at that firm? Can this company show any long-term vision to employees?

6. What are the other benefits?

7. Do they use documents such as offer letter/appointment letter, experience letter, release letter? Get the offer letter, read the terms and conditions. You and the company will sign. You will take one copy and company will take another copy. some companies have some plan to cheat. They won’t use any documents of proof of job.

8. What is the payment method? Bank account or cheque or cash. That is, check whether their salary payment method is official or not. If they pay salary in cash, what are the proofs that you got salary, or you got some part of salary and some part will be paid, or how can you claim that your company didn’t pay your salary.

And at last Joel Test

The Joel Test

Do you use source control?
Can you make a build in one step?
Do you make daily builds?
Do you have a bug database?
Do you fix bugs before writing new code?
Do you have an up-to-date schedule?
Do you have a spec?
Do programmers have quiet working conditions?
Do you use the best tools money can buy?
Do you have testers?
Do new candidates write code during their interview?
Do you do hallway usability testing?

[1] http://www.joelonsoftware.com/articles/fog0000000043.html
Better score, better company..

So, you should choose your employer as like as employers choose their employees.

[ Idea courtesy: Ahmad Ferdous Bin Alam’s post  Checklist Before You Join a New Company ]

Association vs Aggregation vs Composition


, , ,

In programming experience, I found confusion about Association, Aggregation and composition. So, Here I am trying to clearing the concepts.

Association is a relationship where all object have their own lifecycle and there is no owner. Let’s take an example of Teacher and Student. Multiple students can associate with single teacher and single student can associate with multiple teachers but there is no ownership between the objects and both have their own lifecycle. Both can create and delete independently.


Aggregation is a specialize form of Association where all object have their own lifecycle but there is ownership and child object cannot belongs to another parent object. Let’s take an example of Department and teacher. A single teacher cannot belong to multiple departments, but if we delete the department teacher object will not destroy. We can think about “has-a” relationship.
Another example, Wheels are the part of Car. Car owns the wheels, but wheels can exist, if car is not available.


Composition is again specialize form of Aggregation and we can call this as a “death” relationship. It is a strong type of Aggregation. If owner object is destroyed the other one will no longer exist. Let’s take again an example of relationship between University and Department. University can contain multiple departments. There is no independent life of departments and any department cannot belong to two different universities. If we delete the university, departments will be automatically deleted.
Let’s take another example relationship between Questions and options. Single questions can have multiple options and option can not belong to multiple questions. If we delete questions options will be automatically deleted.


So in summary, we can say that aggregation is a special kind of an association and composition is a special kind of an aggregation. (Association->Aggregation->Composition)


Figure : Association, Aggregation, Composition


Get every new post delivered to your Inbox.