Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > C/C++
You last visited: Today at 22:18

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



[C++] char[] to string conversion

Discussion on [C++] char[] to string conversion within the C/C++ forum part of the Coders Den category.

Reply
 
Old   #1
 
AutoItDude's Avatar
 
elite*gold: 0
Join Date: Oct 2009
Posts: 93
Received Thanks: 315
[C++] char[] to string conversion

hi,

I have a problem with conversion char to string.

Code:
char GetUnitName()
{
	char cUnitName[16];
	memcpy(cUnitName,(void*)0x007E7A38,16);

	return cUnitName[strlen(cUnitName)];
}

string UnitName = GetUnitName();
printf("UnitName=%s\n",UnitName);
this doesn't work. I tried
Code:
char UnitName[] = GetUnitName();
string strUnitName(UnitName);
but this does not work to me as well so my question is:
how to convert char array into string properly?
AutoItDude is offline  
Old 07/22/2010, 13:44   #2
 
elite*gold: 115
Join Date: Oct 2007
Posts: 9,390
Received Thanks: 12,345
Code:
char *GetUnitName()
{
	char *pUnitName = new char[16];
	memcpy(pUnitName,(void*)0x007E7A38,16);

	return pUnitName;
}
Either
Code:
char *pUnitName = GetUnitName();
string UnitName = pUnitName;
delete pUnitName;
printf("UnitName=%s\n", UnitName.c_str());
or simply

Code:
char *pUnitName = GetUnitName();
printf("UnitName=%s\n", pUnitName);
delete pUnitName;
ms​ is offline  
Thanks
1 User
Old 07/23/2010, 14:59   #3
 
xNopex's Avatar
 
elite*gold: 0
Join Date: May 2009
Posts: 827
Received Thanks: 471
Long time ago I read an article which explained that it is not a good idea to receive a pointer from a function as the return value. According to the article you should pass the pointer as a parameter to the function instead. For example:

Code:
void GetString( std::string* pStr )
{
    char cUnitName[16];
    memcpy( pUnitName,(void*)0x007E7A38, 16 );
    std::string asString = cUnitName;
    *pStr = asString;
}

I didn't test this code but I think it should work. I do not think that the article was right that you mustn't receive pointers as return values. I think everyone should decide for himself or herself which way he or she prefers.
xNopex is offline  
Reply


Similar Threads Similar Threads
[?] Conversion 5017 to 4267
02/15/2010 - CO2 Private Server - 1 Replies
I have converted packets I know it is client sided issues, but when I go to log in I get some sort of error click the "Spoiler" below to see some pictures of it. http://i46.tinypic.com/2u40hoh.jpg If someone could help it would be well appreciated!
conversion C++ to Delphi
02/04/2010 - C/C++ - 4 Replies
Hi guys, Who can help with conversion C++ code to Delphi? private: CArray<byte> m_Packed; CArray<byte> m_Unpacked; class CUnpack { public:
[AutoIT]Variable Conversion
10/30/2009 - AutoIt - 2 Replies
Is there any way to convert a variable into a certain other type? For example: Variable $A should be a float set to 5.01382 however when I bring it into AutoIT, it converts it to an Unsigned Long giving me a different value of something like 1099238427 So basically I just want to know if there is a command, or any way to code a function that can convert Variables set to Unsigned Long into Float/Double/Unsigned Short
Yet Another CPS to Gold Conversion
01/21/2007 - CO2 Exploits, Hacks & Tools - 9 Replies
i think there are couple of conversion out there but this is very simple written in VB How to use: 1. Enter the avg price of the (inflated) DB. default is 20kk 2. If you want to find out what the item worth in CPS then leave Priceofitem(CPS) as Zero (0). Enter the price in GOLD in the other box. 3. If you want to find out what the item worth in Gold then leave Priceofitem(Gold) as Zero. Enter the price in CPS in the other box 4. Hit calculate. if you find any bug please let me know...
Tauren To Goblin Conversion
08/05/2006 - WoW Exploits, Hacks, Tools & Macros - 13 Replies
Tauren To Goblin Conversion v1 Hi all, this is my first time ever playing around with model's/texturing... for this game. So I decided to change out the bulky Taurens to a Race I felt should have been in the game for horde... The Goblins~... I redid over 160 textures for male and female taurens so they still have individuality, but please keep in mind, I am still fine tuning it. I also have changed the Druid Bear form, and Cat forms, as well as the Kodo mount to...



All times are GMT +1. The time now is 22:18.


Powered by vBulletin®
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2025 elitepvpers All Rights Reserved.