Thứ Năm, 10 tháng 11, 2011

Convert int to std::string and long long to std::string

This is a long time i have never had a post. Today,  i will introduce you how to convert int and long long number to std::string.
When i join in a project about fuzzing browsers, i have to use a very large data type because i must count the number of requests to browsers and this course is continuous and it occures in a very and very long time.
So i think about long long data format. But when you send information to a browser, you must send a string request and if so, you need to convert long long to string before you send it.
When i got this problem, i found it on the internet in many famous websites, and the more websites i flurted, the more information i got. After that, i synthesized all information that i had and my work is well done.
In this topic i will introduce mainly about long long to std::string because "int" is a very popular data type.
"long long" is a data type that is used in C++, it is 64bits in length.  it's range in signed is from (-2^63) to (2^63 - 1) and in unsigned is from 0 to 2^64 - 1, so you can see it is a very and very range. This is the reason that i choose it for my project.
And now, i will introduce to convert this data type to std::string

+ Convert int to std::string



std::string IntToString(int number)
{
std::stringstream out;
out << number;
return out.str();
}

+ Convert long long to std::string


std::string LongLongToString(unsigned long long number)
{
std::stringstream out;
out << number;
return out.str();
}


To use two function, you need to declare some librabry, such as: <string> and <sstream>
I hope this information will help you in your work.
Thanks for visiting my blog.
Do your best, the rest will come!

Thứ Năm, 21 tháng 7, 2011

Relax corner

Come back to me - David Cook

Lyric:
You say you gotta go and find yourself
You say that you're becoming someone else
Don't recognize the face in the mirror
Looking back at you

You say you're leavin
As you look away
I know theres really nothin left to say
Just know i'm here
Whenever you need me
I'll wait for you

So i'll let you go
I'll set you free
And when you see what you need to see
When you find you come back to me

Take your time i wont go anywhere
Picture you with the wind in your hair
I'll keep your things right where you left them
I'll be here for you

Oh and i'll let you go
I'll set you free
And when you see what you need to see
When you find you come back to me

And i hope you find everything that you need
I'll be right here waiting to see
You find you come back to me

I can't get close if your not there
I can't get inside if theres no soul to bear
I can't fix you i can't save you
Its something you have to do

So i'll let you go
I'll set you free
And when you see what you need to see
When you find you come back to me
Come back to me
So i'll let you go
I'll set you free
And when you see what you need to see
When you find you come back to me

And i hope you find everything that you need
I'll be right here waiting to see
You find you come back to me

When you find you come back to me
When you find you come back to me
When you find you come back to me

How can i browse a folder C++, MFC

I have just finished a project and my project has got an method which allow you create a button for browse an folder. I have created a class to do this duty.

// This is getFolderPath.h
#include "shlobj.h"
#include <string>
class getFolderPath
{
public:
    getFolderPath(void);
    ~getFolderPath(void);
   static  bool GetFolderPath(std::string& folderpath, HWND);
};

// This is getFolderPath.cpp
// You have to add "stdafx.h" library if you make your project in Visual Stdio
#include "getFolderPath.h"

getFolderPath::getFolderPath(void)
{
    /*hOwner = hWnd;*/
}

getFolderPath::~getFolderPath(void)
{
}

bool getFolderPath::GetFolderPath(std::string& folderpath,HWND hOwner)
{
    bool retVal = false;

    // The BROWSEINFO struct tells the shell
    // how it should display the dialog.
    BROWSEINFO bi;
    memset(&bi, 0, sizeof(bi));

    bi.ulFlags   = BIF_USENEWUI;
    bi.hwndOwner = hOwner;
    bi.lpszTitle = NULL;

    // must call this if using BIF_USENEWUI
    ::OleInitialize(NULL);

    // Show the dialog and get the itemIDList for the selected folder.
    LPITEMIDLIST pIDL = ::SHBrowseForFolder(&bi);

    if(pIDL != NULL)
    {
        // Create a buffer to store the path, then get the path.
        char buffer[_MAX_PATH] = {L'\0'};
        if(::SHGetPathFromIDListA(pIDL, buffer) != 0)
        {
            // Set the string value.
            folderpath = buffer;
            retVal = true;
        }       

        // free the item id list
        CoTaskMemFree(pIDL);
    }

    ::OleUninitialize();

    return retVal;
}

How you can use my class.
+ Create two file .h  and  .cpp with their content as  i wrote above.
+ Coppy two file into your project, add "getFolderPath.h" library in you programme
+ In main programme or when you process an event such as click to a button, you report an std::string variable to call GetFolderPath function. Example: std::string path = "";
   - If your application is console application,  hOwner = NULL 
   - If  it is a window application or a MFC application, you have to use it's HWND for GetFolderPath function. You can get it's HWND by this function: this->GetSafeHWND();
If success, (GetFolderPath return true)  path will contain your path to folder which you choose
I use static function static  bool GetFolderPath(std::string& folderpath, HWND); so you can call it although you don't create new object

This is example which i do with console application:

#include "getPathFolder.h"
#include <string>
int main()
{
        std::string sPath = "";
        if(getPathFoder::GetFolderPath(sPath,NULL))
        {
               printf("You have just chosen this folder: %s",sPath.c_str());       
        }
        else
        {
                 // Process when can get folder's path
        }
        return 1;
}
Best wishes for you, thanks for visiting my blog!
Do your best, the rest will come!

Convert std::string to int and vice versa

 You can convert std::string to int by this syntax:

std::string sNumber = "123";
int number;

number = atoi(sNumber.c_str());

To convert int to std::string, you can convert int to char*, after that you can convert char* to std::string
To convert int to char*, you can be able to use this syntax


char str[10];
itoa(number,str,10);  // 10 --> convert to 10 base

Thứ Ba, 19 tháng 7, 2011

Get all file's name and subdirectory from directory 's path

If you want to get all file's name from folder's path in C++, you can use many way. I know one way which can help you to do this work.
There are some steps you have to do:
+ The first, you must add chain: "\\*.*" after your path.
+ The second, calling FindFirstFileA function to find the first file in your folder
+ Then you calling FindNextFileA function to find all file to the end
And this is my source code that you can use:

   // You have to use windows.h and tchar.h library

    char * pathFolder = "D:";
    char* FileNext;
    WIN32_FIND_DATAA  findFile;
    HANDLE hfind;
    char folderFind[255];
    int isFinish;

   sprintf(folderFind,"%s\\*.*",pathFolder);
    hfind = FindFirstFileA(folderFind,&findFile);
    isFinish = FindNextFileA(hfind,&findFile);     


    while(isFinish)
    {
        FileNext = findFile.cFileName;                      // Do with your file's name
        isFinish = FindNextFileA(hfind,&findFile);  
    }

Best wishes for you. Thanks for visting my blog!
Do your best, the rest will come!

Convert CString to std::wstring

To convert CString to std::wstring in MFC or C++, you can use this syntax:
 
   CString myCString = "I love Viet Nam";  // If you use Unicode format, you have to insert 'L' character before this quote
   std::wstring result;
    result = (LPCTSTR)outPutFolder;

We look it is same as the way that we use to convert CString to std::string when we don't use Unicode format.
Best wishes for you. Thanks for visiting my blog!
Do your best, the rest will come!

Install Kubernetes using kubeadm with two nodes master & worker

Well come back for a long time my friends! Today I want to take a note about some basic knowledge about Kubernetes. I hope that it will...