473,385 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

convert string to integer

1
hi,

i have a string variable (e.g. R 005) and i like to convert it (numeric part only) to integer. how can i do that?

tnx...
Sep 21 '06 #1
7 30124
sin
13
You can filter all the numbers into another char[] and then do atoi();
For example

int i, ii;
int result;
char str1[] = "R12R21";
char str2[strlen(str1)];

memset(str2, 0, sizeof str2);

for (ii = 0, i = 0; i < strlen(str1); i++)
if (isdigit(str1[i]))
str2[ii++] = str1[i];

result = atoi(str2);

----------------

You can't use atoi() without filtering because it is picking up numbers from beginning till any other char except number (ascii 48-57) occurs, so if first is 'R', the answer would be 0;
Sep 21 '06 #2
risby
30
You can filter all the numbers into another char[] and then do atoi();
For example
Expand|Select|Wrap|Line Numbers
  1. int i, ii;
  2. int result;
  3. char str1[] = "R12R21";
  4. char str2[strlen(str1)];
  5.  
  6. memset(str2, 0, sizeof str2);
  7.  
  8. for (ii = 0, i = 0; i < strlen(str1); i++)
  9.     if (isdigit(str1[i]))
  10.         str2[ii++] = str1[i];
  11.  
  12. result = atoi(str2);
  13.  
----------------

You can't use atoi() without filtering because it is picking up numbers from beginning till any other char except number (ascii 48-57) occurs, so if first is 'R', the answer would be 0;
Unfortunately, you can't allocate str2 like that in C. You have to use malloc() to explicitly allocate memory at runtime (the compiler can't run the strlen function for you at compile time). You must then check that the allocation has worked before using the pointer.
Expand|Select|Wrap|Line Numbers
  1.      unsigned int i, ii;
  2.         int result;
  3.         char str1[] = "R12R21";
  4.         char * str2 = malloc(strlen(str1));
  5.  
  6.     if (str2 != NULL)
  7.    {
  8.         memset(str2, 0, sizeof str1);
  9.  
Also, if changed so that it compiles, this code will convert "R12R21" to
the integer value 1221 but surely the string contains two numeric values of 12 and 21.
Sep 22 '06 #3
There is simple way around to achieve this....

Expand|Select|Wrap|Line Numbers
  1. char _szVal[] = "R12R21";
  2. int _a,_b ;
  3. sscanf(_szVal,"R%dR%d",&_a,&_b);
Sep 22 '06 #4
sin
13
Risby, actually I can allocate str2 like this in C.
Did you tried that code ?
and it won't be 12 and 21
the result in str2 will be 1221
you can see that if you look closer in the program loop
Sep 23 '06 #5
Banfa
9,065 Expert Mod 8TB
Risby, actually I can allocate str2 like this in C.
Did you tried that code ?
Dynamic array allocation like that is a feature of the C99 standard. Generally most people/compilers today still seem to stick to the C89 standard for some reason that is not clear to me.

Since this method of string allocation is also not compatible with C++ in my opinion it might be best avoided.

However like I said it is valid for C99.
Sep 23 '06 #6
risby
30
Dynamic array allocation like that is a feature of the C99 standard.
Neat. I didn't know that.

Risby, actually I can allocate str2 like this in C.
Did you tried that code ?
and it won't be 12 and 21
the result in str2 will be 1221
you can see that if you look closer in the program loop
Steady on mate, I was only trying to be helpful.

Yes, I tried it, it gives error "Constant expression required in function main" with my ridiculously old fashioned compiler.

And yes, I know it won't be 12 and 21 but 1221. That was precisely what I was wondering about. I expect you couldn't read my post due to the steam coming out of your ears?

I'm assuming that a string with two sets of contiguous digits separated by a non-digit should be interpreted as two separate numeric values. It just seems odd to me that anyone in their right mind or any program would output a single four digit value with a great big R in the middle of it. I look forward to you enlightening me on the necessity for this.
Sep 23 '06 #7
sin
13
When I read first post by azrael, I assumed that he wants to convert "numeric part only" to an interger (one integer).
Actually, there was a time when I needed precisely the same behaviour of program (pick all numbers from char[] and convert to int), so I gave an answer to him.
And there wasn't any steam from my ears at all =)
With gcc 3.3.5 I have no complains about that code. And I don't think that anyone must use exactly byte-by-byte code that is posted here.
The idea was to show that destination char[] must have at least same size as source char[].
Sep 25 '06 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: IamZadok | last post by:
Hi I was wondering if anyone knew how to convert a string or an integer into a Static Char. Thx
3
by: Convert TextBox.Text to Int32 Problem | last post by:
Need a little help here. I saw some related posts, so here goes... I have some textboxes which are designed for the user to enter a integer value. In "old school C" we just used the atoi function...
8
by: Carlos | last post by:
I have a strinf with comma separated field. Is is possible to convert it to a Structure ? Thanks
6
by: MrKrich | last post by:
I want to convert Hexadecimal or normal integer to Binary. Does VB.Net has function to do that? I only found Hex function that convert normal integer to Hexadecimal.
5
by: Mika M | last post by:
Hi! I've made little code to convert string into hex string... Public ReadOnly Property ToHexString(ByVal text As String) As String Get Dim arrBytes As Integer() = CharsToBytes(text) Dim sb...
14
by: Drew | last post by:
Hi All: I know I am missing something easy but I can't find the problem! I have a program which reads an integer as input. The output of the program should be the sum of all the digits in the...
20
by: Niyazi | last post by:
Hi all, I have a integer number from 1 to 37000. And I want to create a report in excel that shows in 4 alphanumeric length. Example: I can write the cutomerID from 1 to 9999 as: 1 ---->...
3
by: priyanka | last post by:
Hi there, I want to convert a String into integer. I get the string from a file using : string argNum; getline(inputStream,argNum); I now need to convert argNum into integer.
7
by: elliotng.ee | last post by:
I have a text file that contains a header 32-bit binary. For example, the text file could be: %%This is the input text %%test.txt Date: Tue Dec 26 14:03:35 2006...
7
by: shellon | last post by:
Hi all: I want to convert the float number to sortable integer, like the function float2rawInt() in java, but I don't know the internal expression of float, appreciate your help!
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.