Solved char array to integer

April 22, 2012 at 03:19:31
Specs: Windows XP
Playing around with Visual C++ 2010, I have a textbox from where I've extracted a char array, called "j", with the contents "1200", i.e., j[0]="1", j[1]="2", ,j[2]="0", j[3]="0". In fact I don't know if it is j[0]='1' or j[0]="1" ( difference in quotation ). The problem is that I want to convert the char array "1200" to the int 1200, i.e., the number 1200 in one mem position. Not byte1="1", byte2=2, and so on. Just the plain number 1200. Let me show a bit of the code:

When cycling through the array ( after subtracting the character '0' ):

for {i=0,i<4;i++){
textBox1->Text +="" + (char)j[i];

it presents, on textBox1, the caracters 1200.

If converting it to int with atoi it doesn't compile if "h=atoi(*j);", and using h=atoi(j); it converts only the first character. What I need is h=1200 for some math after this convertion.
First of all, I thank to the comunity for any effort helping. Secondly, for reference, I'm an experienced assembler programmer for many years and I'm forced to learn C++ ( sorry but I simply HATE IT ) for a bet with a friend from the upstairs department, so I'll give it a try.


See More: char array to integer

Report •

April 22, 2012 at 04:11:08
I'm a little puzzled as to how you don't know whether j is an array of characters or an array of strings. What is your declaration for j?

If j were an array of characters then atoi(j) should give the correct result.

Report •

April 22, 2012 at 05:38:00
✔ Best Answer
I suspect he doesn't know if his string is an array of chars or wchar_ts, and he doesn't know enough C/C++ to express that.

It's probably a wchar_t string, because that's VS 2010's default. As you're using Win32, use _ttoi() instead of atoi().

Side notes: If you're casting in C++, you're probably doing it wrong. Also, I agree C++ isn't what a die hard assembly programmer should be learning; it doesn't move you far enough outside of your box. You should learn C# or some other high level language instead.

How To Ask Questions The Smart Way

Report •

April 22, 2012 at 05:50:57
To all: a BIG THANKS!

In two posts I've learned more C than I could get! Problably I'm doing something wrong while casting. My little program is full of comented things I've tryed out and it is already a mess! My fault as I not used to so many comented lines. In Assembler every thing is very strait with the simplicity in mind. We work mem positions, pointers, stacks, etc and we don't have to "cast". I'll try again and follow your advice with _ttoi(). This became more than a bet: it transformed a bet in a challenge and I'm not giving up!

What a way to spent a weekend!

Thank you all!

Report •

Related Solutions

Ask Question