Solved

C++ Builder newbie ?

Posted on 1998-09-01
5
363 Views
Last Modified: 2008-02-01
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
Comment
Question by:tchris
  • 2
  • 2
5 Comments
 
LVL 2

Accepted Solution

by:
duneram earned 50 total points
Comment Utility
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
 
LVL 1

Author Comment

by:tchris
Comment Utility
Great!  Thank you... How about going the other way?

i.e.

long Val1;

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

Thanks,
Terry
0
 
LVL 2

Expert Comment

by:duneram
Comment Utility
you could use sscanf or if val1 was float

val1 = atof(control->text);


0
 
LVL 1

Author Comment

by:tchris
Comment Utility
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
 

Expert Comment

by:dj1encore
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now