Click here to Skip to main content
11,798,280 members (78,003 online)
Click here to Skip to main content

Data Conversions

, 28 Jun 2002 361.1K 140
Rate this:
Please Sign up or sign in to vote.
These are a few samples of data conversions which you can use for a quick reference


Here are a few data conversions with small examples :-

Decimal Conversions

Decimal To Hex

// Use _itoa( ) function and set radix to 16.

char hexstring[10]; 
int number = 30;
itoa( number, hexstring, 16);

// In hexstring is 1e.

Hex To Decimal

// You can use strtol function and you can specify base. 

char * hexstring= "ABCDEF";
char * p;
int number = strtol(hexstring, &p,16);

//  A function that does this

bool HexToDecimal (char* HexNumber, int& Number)
    char* pStopString; 
    Number = strtol (HexNumber, &pStopString, 16);
    return (bool)(Number != LONG_MAX);

Decimal to time

char *DecToTime(float fTime, char *szTime)
    int nHrs, nMin, nSec; 
    fTime *= 3600;
    nHrs = (int)fTime / 3600;
    nMin = (int)(fTime - nHrs * 3600) / 60;
    nSec = (int)(fTime - nHrs * 3600 - nMin * 60);
    wsprintf(szTime, "%02d.%02d.%02d Hrs.Min.Sec.", nHrs, nMin, nSec);
    return szTime;

String Conversions

String to Hex

sscanf(string, %04X, &your_word16);
// where string = your string and 
// 04 = length of your string and X = hex

Hex to CString

CString Str;
unsigned char Write_Buff[1];
Write_Buff[0] = 0x01;

COleVariant to CString

CString strTemp;
COleVariant Var;
Var = "FirstName";
strTemp = Var.bstrVal;

CString to char pointer

CString MyString = "ABCDEF";
char * szMyString = (char *) (LPCTSTR) MyString;
char *pBuffer = new char[1024];
CString strBuf = "Test";
pBuffer = strBuf.GetBuffer(sizeof(pBuffer));

char pointer to CString

char * mystring = "12345";
CString string = mystring;

Double to CString including the fractional part

CString strValue,strInt, strDecimal;
int decimal,sign;
double dValue = 4.125;
strValue = _fcvt(dValue,6,&decimal,&sign); 
    // Now decimal contains 1 because there is 
    // only one digit before the .

strInt = strValue.Left(decimal); // strInt contains 4
strDecimal = strValue.Mid(decimal); // strDecimal contains 125

CString strFinalVal;
    // strFinalVal contains 4.125

Double To CString

CString strValue;
int decimal,sign;

double dValue = 123456789101112;
strValue = _ecvt(dValue,15,&decimal,&sign);

CString To Double

strValue = "121110987654321";
dValue = atof(strValue);

CString to LPCSTR

CString str1 = _T("My String");
int nLen = str1.GetLength();
LPCSTR lpszBuf = str1.GetBuffer(nLen);
// here do something with lpszBuf...........

CString to LPSTR

CString str = _T("My String");
int nLen = str.GetLength();
LPTSTR lpszBuf = str.GetBuffer(nLen);
// here do something with lpszBuf...........

CString to WCHAR*

CString str = "A string here" ;
LPWSTR lpszW = new WCHAR[255];

LPTSTR lpStr = str.GetBuffer( str.GetLength() );
int nLen = MultiByteToWideChar(CP_ACP, 0,lpStr, -1, NULL, NULL);
MultiByteToWideChar(CP_ACP, 0, lpStr, -1, lpszW, nLen);
AFunctionUsesWCHAR( lpszW );
delete[] lpszW;


int nLen = MultiByteToWideChar(CP_ACP, 0, lptStr, -1, NULL, NULL);
MultiByteToWideChar(CP_ACP, 0, lptStr, -1, lpwStr, nLen);

string to BSTR

string ss = "Girish";
BSTR _bstr_home = A2BSTR(ss.c_str());

CString to BSTR

CString str = "whatever" ;
BSTR resultsString = str.AllocSysString(); 

_bstr_t to CString

#include <ANSIAPI.H>
#include <comdef.h>
_bstr_t bsText("Hai Bayram");
CString strName;
W2A(bsText, strName.GetBuffer(256), 256);

char szFileName[256];

Character arrays

Char array to integer

char MyArray[20];
int nValue;

nValue = atoi(MyArray);

Char array to float

char MyArray[20];
float fValue;

fValue = atof(MyArray);

Char Pointer to double

char *str = " -343.23 "; 
double dVal; 
dVal = atof( str );

Char Pointer to integer

char *str = " -343.23 "; 
int iVal;
iVal = atoi( str );

Char Pointer to long

char *str = "99999"; 
long lVal; 
lVal = atol( str );

Char* to BSTR

char * p = "whatever";
_bstr_t bstr = p;

Float to WORD and Vice Versa

float fVar;
WORD wVar;
fVar = 247.346;
wVar = (WORD)fVar; //Converting from float to WORD. 
    //The value in wVar would be 247

wVar = 247;
fVar = (float)fVar; //Converting from WORD to float. 
    //The value in fVar would be 247.00


All luck and have a great time. I've checked most of the code above, but if I've been wrong somewhere, I'd like to know about it so that I can modify it. Thank you.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

India India
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionConversion of CString to uchar Pin
Ramya Suvarna17-Jul-07 17:45
memberRamya Suvarna17-Jul-07 17:45 
Generalthanx Pin
sony fernando23-May-07 19:04
membersony fernando23-May-07 19:04 
GeneralThanx Pin
smzhaq22-May-07 4:44
membersmzhaq22-May-07 4:44 
GeneralCString to const WCHAR* Pin
smzhaq22-May-07 3:37
membersmzhaq22-May-07 3:37 
QuestionHow to convert a CString to _bstr_t Pin
ganesa moorthy10-Apr-07 1:32
memberganesa moorthy10-Apr-07 1:32 
AnswerRe: How to convert a CString to _bstr_t Pin
J-D20-Apr-07 4:35
memberJ-D20-Apr-07 4:35 
QuestionHow to convert CString to wstring? Pin
Sunil Jampa2-Jan-07 23:05
memberSunil Jampa2-Jan-07 23:05 
Questionwhere can i find the sample conversion of WideCharToMultiByte? Pin
SKhokalay22-Sep-06 0:50
memberSKhokalay22-Sep-06 0:50 
AnswerRe: where can i find the sample conversion of WideCharToMultiByte? Pin
metmac12-Aug-07 1:17
membermetmac12-Aug-07 1:17 
Generalconverting byte array to hex string [modified] Pin
ranu_hai26-Jul-06 20:43
memberranu_hai26-Jul-06 20:43 
QuestionCString usage / conversion Pin
jpl7818-May-06 0:35
memberjpl7818-May-06 0:35 
AnswerRe: CString usage / conversion Pin
metmac12-Aug-07 1:32
membermetmac12-Aug-07 1:32 
GeneralDouble to CString suggestion Pin
l00p1n62-Nov-05 12:50
memberl00p1n62-Nov-05 12:50 
GeneralRe: Double to CString suggestion Pin
lagsita25-Apr-06 23:19
memberlagsita25-Apr-06 23:19 
GeneralBug in Double to CString Pin
viki66625-Aug-05 0:55
memberviki66625-Aug-05 0:55 
AnswerRe: Bug in Double to CString [modified] Pin
Nkwenti9-Aug-07 0:55
memberNkwenti9-Aug-07 0:55 
GeneralCString to char pointer Pin
Can Isin18-Feb-05 6:28
sussCan Isin18-Feb-05 6:28 
Questionfloat to char[ ] ?? Pin
anindya1234531-Jan-05 0:04
memberanindya1234531-Jan-05 0:04 
Questionchar * to LPTSTR?? Pin
jerrelh15-Dec-04 23:49
memberjerrelh15-Dec-04 23:49 
AnswerRe: char * to LPTSTR?? Pin
Mircea Puiu16-Dec-04 0:14
memberMircea Puiu16-Dec-04 0:14 
GeneralRe: char * to LPTSTR?? Pin
l00p1n62-Nov-05 12:32
memberl00p1n62-Nov-05 12:32 
Generalchar pointer to CString Pin
OsoPolar7-Dec-04 8:10
memberOsoPolar7-Dec-04 8:10 
GeneralRe: char pointer to CString Pin
OsoPolar7-Dec-04 8:16
memberOsoPolar7-Dec-04 8:16 
GeneralRe: char pointer to CString Pin
vmihalj8-Sep-05 0:13
membervmihalj8-Sep-05 0:13 
Generalwchar to float Pin
sammyc6-Dec-04 20:55
membersammyc6-Dec-04 20:55 
GeneralRe: wchar to float Pin
Mehta Jigar21-May-06 22:05
memberMehta Jigar21-May-06 22:05 
Questionwhat about this Pin
Tom Wright23-Sep-04 6:23
memberTom Wright23-Sep-04 6:23 
AnswerRe: what about this Pin
VGirish23-Sep-04 23:47
memberVGirish23-Sep-04 23:47 
AnswerA Simpler Way Pin
VGirish24-Sep-04 0:14
memberVGirish24-Sep-04 0:14 
GeneralRe: A Simpler Way Pin
jasonslater200016-Feb-05 10:54
memberjasonslater200016-Feb-05 10:54 
QuestionDecode a UTF-8 file to a CString? Pin
d00_ape30-Aug-04 1:51
memberd00_ape30-Aug-04 1:51 
AnswerRe: Decode a UTF-8 file to a CString? Pin
peterchen3-Jan-05 9:04
memberpeterchen3-Jan-05 9:04 
GeneralConvert BSTR to char* (unicode to ascii Pin
Anonymous7-Jan-04 6:12
sussAnonymous7-Jan-04 6:12 
GeneralRe: Convert BSTR to char* (unicode to ascii Pin
Anonymous3-Feb-04 6:24
sussAnonymous3-Feb-04 6:24 
GeneralRe: Convert BSTR to char* (unicode to ascii Pin
Anonymous11-Nov-04 21:15
sussAnonymous11-Nov-04 21:15 
GeneralPrevent memory leaks, optimize for small strings Pin
EfimPechat24-Nov-04 4:27
sussEfimPechat24-Nov-04 4:27 
GeneralRe: Convert BSTR to char* (unicode to ascii Pin
kp_9520-Jan-05 10:55
memberkp_9520-Jan-05 10:55 
GeneralRe: Convert BSTR to char* (unicode to ascii Pin
Anonymous8-Feb-05 7:51
sussAnonymous8-Feb-05 7:51 
GeneralRe: Convert BSTR to char* (unicode to ascii Pin
Anonymous8-Feb-05 7:53
sussAnonymous8-Feb-05 7:53 
GeneralRe: Convert BSTR to char* (unicode to ascii Pin
Anonymous11-Apr-05 6:22
sussAnonymous11-Apr-05 6:22 
GeneralRe: Convert BSTR to char* (unicode to ascii Pin
Rainer Schulz27-Jan-05 21:07
sussRainer Schulz27-Jan-05 21:07 
GeneralRe: Convert BSTR to char* (unicode to ascii Pin
Anonymous27-Jan-05 21:11
sussAnonymous27-Jan-05 21:11 
GeneralRe: Convert BSTR to char* (unicode to ascii Pin
vmihalj8-Sep-05 0:22
membervmihalj8-Sep-05 0:22 
GeneralRe: Convert BSTR to char* (unicode to ascii Pin
at_ramya13-Mar-07 20:27
memberat_ramya13-Mar-07 20:27 
GeneralLPWSTR to BSTR Pin
JigarThakkar15-Dec-03 18:51
memberJigarThakkar15-Dec-03 18:51 
GeneralCString to LPTSTR (CString to WCHAR*) Pin
Dutch Joan6-Nov-03 21:00
memberDutch Joan6-Nov-03 21:00 
GeneralRe: CString to LPTSTR (CString to WCHAR*) Pin
Mr.Prakash27-Nov-03 19:39
memberMr.Prakash27-Nov-03 19:39 
GeneralRe: CString to LPTSTR (CString to WCHAR*) Pin
mahesh kumar s27-Mar-07 0:43
membermahesh kumar s27-Mar-07 0:43 
GeneralRe: CString to LPTSTR (CString to WCHAR*) Pin
Mr.Prakash27-Mar-07 3:35
memberMr.Prakash27-Mar-07 3:35 
GeneralLPSTR TO CString or LPSTR to BSTR Pin
Anonymous18-Sep-03 7:34
sussAnonymous18-Sep-03 7:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151002.1 | Last Updated 29 Jun 2002
Article Copyright 2002 by VGirish
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid