Several examples of string conversion

xiaoxiao2021-03-06  14

Part ONE: - Decimal Conversions

Decimal to Hexa: -

------------------

Use _itoa () Function and Set Radix To

16.

CHAR HEXSTRING [

10];

Int number =

30;

ITOA (Number, HexString,

16);

In HexString IS 1E.

HEXA to Decimal: -

------------------

a)

You can use strtol function and you can specify base.

Char * hexstring =

"AbcDef";

Char * p;

Int number = strtol (HEXSTRING, & P,

16);

b)

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;

}

Part Two: - String Conversions

String to Hexa: -

-----------------

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;

Str.Format

"0x0% x", WRITE_BUFF [

0]);

Colevariant to cstring

----------------------

CString Strtemp;

Colevariant var;

VAR =

"Firstname";

Strtemp = var.bstrval;

AfxMessageBox (Strtemp);

CString to Char Pointer

-----------------------

a)

CString myString =

"AbcDef";

Char * szmystring =

CHAR *) (LPCTSTR) mYString;

b)

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 The IS Only One Digit Before The.

Strint = strValue.Left (decimal);

// Strint Contains 4

StrDecimal = strValue.mid (Decimal);

// strDecimal contains 125

Cstring strfinalval;

StrfinalVal.Format

"% s.% s", strint, strdecimal;

// StrfinalVal Contains 4.125

Double to cstring: -

--------------------

CString Strvalue;

INT Decimal, SIGN;

Double Dvalue =

123456789101112;

Strvalue = _ecvt (DVALUE,

15, & Decimal, & Sign);

// Converting Double To String

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

Str1.releasebuffer ();

CString to LPSTR: -

--------------------

CSTRING STR = _T (

"My string");

INT NLEN = str.getlength ();

LPTSTR LPSZBUF = Str.getBuffer (NLEN);

// Here do Something with lpszbuf ...........

Str.releaseBuffer ();

CString to wchar *

-----------------

CString str =

"A string here";

LPWSTR LPSZW =

New Wchar [

255];

LPTSTR LPSTR = Str.getBuffer (Str.getLength ());

INT Nlen = MultibyTowideChar (CP_ACP, 0, LPSTR, -

1, null, null;

MultibyToWideChar (CP_ACP,

0, LPSTR, -

1, LPSZW, NLEN);

AfunctionUnswchar (LPSZW);

DELETE [] LPSZW;

LPTSTR to LPWSTR

----------------

INT Nlen = MultibyToWideChar (CP_ACP,

0, LPTSTR, -

1, null, null;

MultibyToWideChar (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 =

"whatver";

BStr resultsString = str.allocsystring ();

_BSTR_T to CSTRING

-------------------

#include

#include

_BSTR_T BSTEXT

"Hai bayram");

CString strname;

W2a (BSText, Strname.getBuffer)

256)

256);

Strname.releaseBuffer ();

AfxMessageBox (STRNAME);

Char szfilename [

256];

GetModuleFileName (null, szfilename,

256);

AfxMessageBox (SZFileName);

Part THREE: - Character Arrays

Char array to integer

---------------------

Char myarray [

20];

Int nvalue;

Nvalue = atoi; mARRAY;

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 =

"whatver";

_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 247wvar =

Type 247;

Fvar =

Float) FVAR;

// converting from word to float. The value in fvar Would Be 247.00

All Luck And Have A Great Time.

转载请注明原文地址:https://www.9cbs.com/read-50561.html

New Post(0)