Binary to Decimal and Decimal to Binary Conversion

Tags

, , ,

#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);
++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

Tags

, , ,

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.
static
{
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

Tags

,

CWinThread* m_pThread;

// Creating Thread
if (NULL == m_pThread)
m_pThread = AfxBeginThread(ThreadProc,
NULL,
THREAD_PRIORITY_NORMAL,
0,
CREATE_SUSPENDED,
NULL);

m_pThread->m_bAutoDelete = false;
m_pThread->ResumeThread();

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
CloseHandle(m_pThread->m_hThread);
m_pThread->m_hThread = NULL;

delete m_pThread;
m_pThread = NULL;

Get console output from exe in C++

Tags

, ,

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

Tags

, ,

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?

Solution:

<?php
// put your code here

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

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

?>

 

 

Unsorted map

Tags

, ,

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 join a software firm

Tags

In my 6yrs 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!

WORK CULTURE or PROCESS:

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?

PROJECT:

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.

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

Tags

, , ,

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.

association.jpg

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.

aggregation.jpg

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.

composition.jpg

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)

association_aggregation_composition.jpg

Figure : Association, Aggregation, Composition

How struct manage its variables?

Tags

, , ,

Suppose we have struct like this.

struct
{
double dVal;
int nVal;
char chVal;
} Data1;

So what will be the size of Data1?
We think like
sizeof(char) + sizeof(int) + sizeof(double) = 13. Is it?

But Surprisingly we are wrong.

Again look at another struct like this.


struct
{
int nVal;
char chVal;
char chVal2;
double dVal;
} Data2;

We think like
sizeof(char) + sizeof(char) + sizeof(int) + sizeof(double) = 14. Is it?

In both cases, sizeof(Data1) and sizeof(Data2) will be 16. how?

So, struct stores its variables in different way. Let’s discuss.

1. struct allocate memory for its first variable as same as the most longest variable used in that struct. Here double is most longest. so, it will allocated 8bytes for the first variable.

2. Then come for the second variable. It checks the size of the data type of the second variable. If already allocated memory for first variable, is enough then struct doesn’t allocate new memory. Otherwise, allocate big size like first variable.

Now discuss about our example.

struct Data1
1. For double it will allocate 8bytes,
2. For int, it will also allocated 8bytes.
3. For char, I won’t allocate any, because already 4bytes is extra from int type.

struct Data2
1. for int datatype it will allocate 8bytes, as double needs 8bytes.
2. for char it will need 1byte. As Data2 have already 4bytes extra, so no need to allocate for next two char.
3. Now Data2 have 2 bytes extra, but double needs 8bytes. So, 8 bytes will be allocated.

I think we all are clear now.

Follow

Get every new post delivered to your Inbox.