Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Transferring Data From String Array

string array string array data integer

  • Please log in to reply
8 replies to this topic

#1 jasonalien

jasonalien

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 194 posts
  • Programming Language:C, Java, C++, C#, PHP, JavaScript
  • Learning:C, Java, C++, Objective-C, C#

Posted 10 July 2012 - 12:33 AM

Hello everyone,
I have a string array with a length of 360 strings. But only a small amount of the strings are full, like 10-20 strings. And the data inside the strings is like that :

-91dBm
-93dBm
-88dBm
-89dBm
-88dBm
-89dBm
-92dBm
-91dBm
-92dBm
-90dBm
-90dBm
I need to make some calculations on this data. So How could I get rid of the dBms after numbers? And how could I conver them to negative integers.
Thanks in advance.
  • 0

#2 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 10 July 2012 - 12:44 AM

You mean all those above data contains in a single array of char? Or, only one of them will be in a string at a time (as example -91dBm)?

If an array of chars contains data like "-91dBm", you can use atoi function go get the data. As example,
int frequency = atoi("-91dBm"); /* here frequency variable will have -91 in it*/

  • 0

#3 jasonalien

jasonalien

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 194 posts
  • Programming Language:C, Java, C++, C#, PHP, JavaScript
  • Learning:C, Java, C++, Objective-C, C#

Posted 10 July 2012 - 04:52 AM

only one data is on one string, for example -91dBm is in one string.
  • 0

#4 Brandon W

Brandon W

    CC Mentor

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2092 posts
  • Location:Ipswich, Australia
  • Programming Language:C, Java
  • Learning:Java, C++, JavaScript

Posted 11 July 2012 - 08:42 PM

only one data is on one string, for example -91dBm is in one string.

What kernel mentioned will still work in this case, did you manage to get it working though, bahadirtr?
  • 0
I've returned...

#5 jasonalien

jasonalien

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 194 posts
  • Programming Language:C, Java, C++, C#, PHP, JavaScript
  • Learning:C, Java, C++, Objective-C, C#

Posted 12 July 2012 - 11:25 PM

                       string yirmidorty[360]

                        int baha[360];
                        for(int i = 0; i<360;i++)
                        {
                        baha[i]=atoi(yirmidorty[i]);
                        cout<<baha[i]<<endl; 
                        
                        }
My compiler says that I'm giving invalid arguments to atoi.. As I see atoi accepts char array but not strings. Is there a way to make atoi do that?
Thanks..
  • 0

#6 jasonalien

jasonalien

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 194 posts
  • Programming Language:C, Java, C++, C#, PHP, JavaScript
  • Learning:C, Java, C++, Objective-C, C#

Posted 13 July 2012 - 12:07 AM

I found istringstream function. It seems useful but I couldn't make this code work :
					    int baha[360];
					    for(int i = 0; i<360;i++)
					    {
					    istringstream(yirmidorty[i])>>baha[i];
						 cout<<baha[i]<<endl;
					    }

  • 0

#7 Brandon W

Brandon W

    CC Mentor

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2092 posts
  • Location:Ipswich, Australia
  • Programming Language:C, Java
  • Learning:Java, C++, JavaScript

Posted 16 July 2012 - 11:34 PM

I have never heard of the istringstream function before, if you wish to use it I'll look it up but to convert your string to a C string, simply use the function .c_str() on the string.

e.g.
char * cString= (char*)yirmidorty[i].c_str();

  • 0
I've returned...

#8 notes

notes

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 88 posts
  • Location:Poland
  • Programming Language:C++
  • Learning:Java, C#

Posted 17 July 2012 - 04:20 AM

It's nice to write own function which is really simple.I wote my function which was changing string like 22-3+(31*3) to two stacks one integer type with values second char type with operators. In your case it would be much simplier cause you have to read from '-' till next digit is not integer.
function that checks would look really simple like :

if (((int)num[0] > 47) && ((int)num[0] <58) )
return true;
return false;

You are comparing ASCII digits. Then you can append character to a temporary string where you'll have only numbers.
This is working prototype, I leave to you recognizing whenever its negative or positive int.
bool is_number(char c);
int str_to_int(string str);
int _tmain(int argc, _TCHAR* argv[])
{
string str ="-96dBm";
int result = str_to_int(str);
cout << "String: " << str<< "\nInt: " << result<< endl;
system("PAUSE");
return 0;
}
bool is_number(char c) {
if ( ((int)c > 47) && ((int)c <58) )
return true;
return false;
}
int str_to_int(string str) {
int myResult = 0; // IMPORTANT to initialize this int.
string temp;
int j = 1;
// Want to start from 1 cause first char is '-'
	 // You may want to change is_number function to omit this.
while (is_number(str[j]) ) {
temp += str[j];
j++;
}
// Now we have number in temp;
int tempSize=temp.length()-1;
for (int i = 0; i <tempSize; i++ ) {
myResult*=10; // if 0 still 0
myResult+=atoi(&temp[i]);
}
return myResult;
}

Edit : This is ms vc++ so to get this working youll need to include in stdafx.h:
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <string>
using namespace std;

Or just make propper includes in your IDE ;-)

Hope this was helpful,
notes.
  • 1
Remebre about KISS & DRY

#9 jasonalien

jasonalien

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 194 posts
  • Programming Language:C, Java, C++, C#, PHP, JavaScript
  • Learning:C, Java, C++, Objective-C, C#

Posted 17 July 2012 - 07:47 AM

thank you :)
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download