473,387 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Convert String to Char*

Hey all. I have a small problem. I have a string called hexed. inside hexed for example is the string "003eb". I need to be able to convert this string into a char* because i have a predefined function that converts hexadecimal to decimal but its parameter that needs to be passed to it is a char *. I have been looking into a few things and Im just a little confused on how to implement it.
Apr 14 '09 #1
5 3839
Banfa
9,065 Expert Mod 8TB
You can't convert string to char *. You can convert string to const char * using the method string::c_str.

To do what you want you would need to copy the string to an array of char of an appropriate length using strcpy and string::c_str, call your function then assign the result back to your string.

It may be easier or at least produce cleaner code to re-write (or write an overload for) you function that operates directly on a string.
Apr 14 '09 #2
Studlyami
464 Expert 256MB
I have used this to assign a CString to a char *.

char* = (char *) (LPCTSTR)CString;

I don't know if that will help you or not.
Apr 17 '09 #3
Banfa
9,065 Expert Mod 8TB
Since the op is planning on writing data to that pointer that would be a really bad idea. You have cast away the constness of a pointer, always a disaster waiting to happen.

However mentioning CString if you happen to be using MFC and CString then you can call the method GetBuffer which returns a char * and allows you to specify the size of the buffer that the returned pointer points to (not forgetting to call ReleaseBuffer once finished)
Apr 17 '09 #4
Studlyami
464 Expert 256MB
I didn't see where he stated that he was going to write to it, but yeah is definitely something to look out for and I should have stated that. If the function only reads items in the char* it should work fine, but it isn't safe. Thanks for the tip about the GetBuffer, I'll have to try it out.
Apr 18 '09 #5
weaknessforcats
9,208 Expert Mod 8TB
char* = (char *) (LPCTSTR)CString;
Be careful of this code. CString be either a char* string or a wchar_t* string. And you can't tell which.

Once you start using TCHAR mappings, like LPCTSTR, then you can no longer use the natives types directly. Everything must now be TCHAR.

The char* would become a PTCHAR.
Apr 19 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

27
by: Trep | last post by:
Hi there! I've been having a lot of difficult trying to figure out a way to convert a terminated char array to a system::string for use in Visual C++ .NET 2003. This is necessary because I...
5
by: IamZadok | last post by:
Hi I was wondering if anyone knew how to convert a string or an integer into a Static Char. Thx
7
by: MilanB | last post by:
Hello How to convert char to int? Thanks
15
by: Yifan | last post by:
Hi Does anybody know how to convert System::String* to char*? I searched the System::String class members and did not find any. Thanks Yifan
12
by: Brian Henry | last post by:
first question... I have a flat file which unfortinuatly has columns seperated by nulls instead of spaces (a higher up company created it this way for us) is there anyway to do a readline with this...
2
by: Steve Kershaw | last post by:
I can convert from a string to a char array (char) by using string.ToCharArray() function. Now how do I convert it back? The string looks like this: "000457". I've converted it to a char array:...
5
by: Zytan | last post by:
I am surprised that a single character string is not auto-created from a single char. It is hard to find information on converting a char into a string, since most people ask how to convert char...
4
by: Man4ish | last post by:
HI , I am trying to convert string into char array of characters.but facing problem. #include <iostream> #include <string> using namespace std; int main() { string t="1,2,3,4,5,6";
12
by: Peter | last post by:
Trying to convert string to byte array. the following code returns byte array of {107, 62, 194, 139, 64} how can I convert this string to a byte array of {107, 62, 139, 65} ...
14
by: rtillmore | last post by:
Hello, I did a quick google search and nothing that was returned is quite what I am looking for. I have a 200 character hexadecimal string that I need to convert into a 100 character string. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.