Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » LineEdit, EditFields, DocEdit » EditString: how to convert data with "~" operator to char*?
EditString: how to convert data with "~" operator to char*? [message #1386] Wed, 01 March 2006 23:31 Go to next message
Cpu86 is currently offline  Cpu86
Messages: 3
Registered: March 2006
Junior Member
Hello,

how can I convert the data stored in an editstring from the type returned by applying the ~ operator to char *.
I want to manipulate the value of the editstring as same as an array of char.
It is possible?

Thanks

Marco

[Updated on: Sun, 30 April 2006 12:21] by Moderator

Report message to a moderator

Re: Editstring control [message #1387 is a reply to message #1386] Wed, 01 March 2006 23:36 Go to previous messageGo to next message
zsolt is currently offline  zsolt
Messages: 590
Registered: December 2005
Location: Budapest, Hungary
Contributor
String s = edit.GetData();
const char *cs = ~s;
Re: Editstring control [message #1389 is a reply to message #1387] Wed, 01 March 2006 23:55 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 12055
Registered: November 2005
Ultimate Member
zsolt wrote on Wed, 01 March 2006 17:36

String s = edit.GetData();
const char *cs = ~s;



BTW, instead of .GetData you can use ~ here too, and you do not need '~' for converting as there is operator const char*:

String s = ~edit;
const char *cs = s;

String::operator~ is reserved just for ambiguous cases like

printf("%s", ~s);

Mirek
Re: Editstring control [message #1390 is a reply to message #1389] Wed, 01 March 2006 23:59 Go to previous messageGo to next message
Cpu86 is currently offline  Cpu86
Messages: 3
Registered: March 2006
Junior Member
Ok, but I must declare the pointer as a const?
Re: Editstring control [message #1391 is a reply to message #1390] Thu, 02 March 2006 00:29 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 12055
Registered: November 2005
Ultimate Member
Cpu86 wrote on Wed, 01 March 2006 17:59

Ok, but I must declare the pointer as a const?


I see...

Yes. Constness is needed to give enough freedom for COW String implementations.

If you want to write those chars, you could use StringBuffer:

String x...

StringBuffer xb(x); //Clears x, takes the content

char *x = xb;

// do what you want, investigate StringBuffer interface to learn more

x = xb; // put it back, clears xb

Of course, another option is to simply copy String to separate buffer, however StringBuffer guarantees minimal copying.. (if possible, it performs none).

Mirek
Re: Editstring control [message #10161 is a reply to message #1391] Fri, 22 June 2007 17:56 Go to previous messageGo to next message
malaugh is currently offline  malaugh
Messages: 7
Registered: June 2007
Promising Member
I have been struggling with the same issue. I need to get a hex number from an edit box, so I used an EditField called editHex, and converted to a string using

char HexDigits[MAX_HEX_CHARACTERS + 1];
UINT8 DataOut[MAX_HEX_CHARACTERS/2 ];

strcpy(HexDigits, editHex.GetText().ToString());
GetHexValue(DataOut, HexDigits);

GetHexValue reads the string and does the conversion.

This works fine if I just execute the program. But if run in the debugger and set a breakpoint on the strcpy line, I get the Microsoft "crash" dialog box with the message.

"gdb.exe has encountered a problem and needs to close. We are sorry for the inconvenience."

If I comment out the strcpy, then gdb does not crash. I am using the windows version with the minGW compiler and the internal debugger. The EditField length is set to MAX_HEX_CHARACTERS.

Am I doing something wrong?





Re: Editstring control [message #10173 is a reply to message #10161] Sat, 23 June 2007 08:06 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 12055
Registered: November 2005
Ultimate Member
Most likely nothing (means your code is OK).

Unfortunately, there are bugs in gdb...

Well, while the bug you mention is caused by gdb, you code might have serious buffer overflow problem: Imagine what happens when you enter more than "MAX_HEX_CHARACTERS".

Anyway, I recommend you to investigate EditField::SetConvert, e.g.
look here:

http://www.ultimatepp.org/reference$Convert.html
Re: Editstring control [message #10198 is a reply to message #10173] Sun, 24 June 2007 23:16 Go to previous messageGo to next message
malaugh is currently offline  malaugh
Messages: 7
Registered: June 2007
Promising Member
Fantastic, thanks for your help. I tried your link and it works great. I was able to read out a hex value from the EditFiled after changing the code to do the hex conversion.

There is another issue though. How do I write to the EditField? I thought I could change the FormatIntBase function to base 16, but I ran into problems. Here is part of my code that I was using to debug the problem.

void HexEdit::OnPopulate()
{
int x = 1234;
char s[20];
Value y = 1234;
Value z;

sprintf(s,"%d",x);
z = FormatIntBase(y, 2, 0, 0, 0);
editOutput = s;
// editOutput = x;
// editOutput = y;
// editOutput = z;
}

editOutput is an EditField control. Here is what happens:

editOutput = s; works, it displays 1234
editOutput = x; crashes the program.
editOutput = y; crashes the program.
editOutput = z; works, it displays the binary conversion of 1234

I am not sure why y should fail and z should pass.

Also if I change the code by putting

editOutput.SetConvert(Single<ConvertBin>());

in the initialization (which should call FormatIntBase on a write to the control, and remove FormatIntBase from the populate function, the program crashes.

What am I doing wrong? Any suggestions?



Re: Editstring control [message #10201 is a reply to message #10198] Mon, 25 June 2007 03:21 Go to previous messageGo to next message
fudadmin is currently offline  fudadmin
Messages: 1298
Registered: November 2005
Location: London, UK
Senior Contributor
Administrator
If I remember well this is related to operator overloading.
My tip is always to try something like:
 editOutput<< ~x;

[Updated on: Mon, 25 June 2007 03:22]

Report message to a moderator

Re: Editstring control [message #10204 is a reply to message #10198] Mon, 25 June 2007 08:52 Go to previous message
mirek is currently offline  mirek
Messages: 12055
Registered: November 2005
Ultimate Member
Use editOutput <<= value...

Mirek
Previous Topic: EditInt callback?
Next Topic: DocEdit bug
Goto Forum:
  


Current Time: Mon Oct 21 19:24:37 CEST 2019

Total time taken to generate the page: 0.01072 seconds