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...
7 30124
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;
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;
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. -
unsigned int i, ii;
-
int result;
-
char str1[] = "R12R21";
-
char * str2 = malloc(strlen(str1));
-
-
if (str2 != NULL)
-
{
-
memset(str2, 0, sizeof str1);
-
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.
There is simple way around to achieve this.... - char _szVal[] = "R12R21";
-
int _a,_b ;
-
sscanf(_szVal,"R%dR%d",&_a,&_b);
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
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.
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.
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[].
Sign in to post your reply or Sign up for a free account.
Similar topics
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
|
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...
|
by: Carlos |
last post by:
I have a strinf with comma separated field. Is is possible to convert it to
a Structure ?
Thanks
|
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.
|
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...
|
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...
|
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 ---->...
|
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.
|
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...
|
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!
|
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...
|
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...
|
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...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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
|
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...
| |