• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 403
  • Last Modified:

C++ Builder newbie ?

I am a moderately experienced C programmer with a good basic understanding of C++ in a DOS environment.  I am learning Borland C++ Builder and would like some help with string conversions.

I simply want to be able to use the VCL standard "AnsiString" you get from a text field, etc. with regular C conversion functions, such as atoi() or _ltoa().  Is there a data member of an AnsiString I should access, or are there VCL conversion functions I should use instead?

Example:

long Calc1=1.3, Calc2=0.25;

Control->Text=(_ltoa(Calc1/Calc2));  // Simply won't work

Thanks much in advance!
Terry
0
tchris
Asked:
tchris
  • 2
  • 2
1 Solution
 
duneramCommented:
in windows we typically use wsprintf something like this

First you should use float or double for your 'calc variables' otherwise they will be truncated

so (I'm just using c for this example)

float calc1=1.3;
float calc2=.25;


wsprintf(Control->Text, "%f", calc1/calc2);

This says the answer will be stored as a string/text in floating point format and to div the two for an answer and store the answer in string format in Control->Text;


0
 
tchrisAuthor Commented:
Great!  Thank you... How about going the other way?

i.e.

long Val1;

Val1 = atol(Control->Text); // Nope...

Thanks,
Terry
0
 
duneramCommented:
you could use sscanf or if val1 was float

val1 = atof(control->text);


0
 
tchrisAuthor Commented:
Thanks for the effort, but that doesn't work.  There is a VCL method called GetTextBuf() that allows you to access the actual char* for the text.  This allowed me to send it on through the atol() mill for further processing.

Also, as an aside, I used a different approach for the floating point-to-AnsiString conversion than you provided:

long Value1;
char buf[32];

_ltoa(Value1,buf,10);
AnsiString ABuf(buf);  // A constructor is provided for this...
Control->Text = Abuf;

It proved to be rather simple once I found the constructor.

However, since your answer got me in the right direction, I am happy with the answer anyway.

Thanks!

Terry
0
 
dj1encoreCommented:
I have no problem in doing it that way but how about getting the text from the user and manipulating it?

but to answer your answer try

Control->Text = atol(Buffer);


0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now