473,804 Members | 3,200 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

char conversion problem

is string converted to its integer equivalent by minusing it by 48?
the function is suppose to check the fifth digit of struct member
using the formula contained within the function.
The function isn't doing that at the moment, hence this post and the
querie above. string must be of type char.

bool checkdigitforcu stomercode( char* string )
{

int weightingfactor = 5;
int checkdigit;
int remainder;
int check1;

for(int i =0; i < 5; ++i)
check1 += (string[i] - 48) * weightingfactor ;/* problem area */
weightingfactor --;
remainder = check1 % 11;

checkdigit = 11 - remainder;

for(i=4; i<=5; i++)
if(!string[i] == checkdigit)
return false;
if(checkdigit == 11){
checkdigit = 'X';
}
else
if(checkdigit = 10){
checkdigit = '0';
}

return true;
}
Jul 22 '05 #1
18 1851

"muser" <ch**********@h otmail.com> wrote in message
news:f9******** *************** ***@posting.goo gle.com...
is string converted to its integer equivalent by minusing it by 48?


If you want to strip numbers from the ASCII code, I'd think you would want
to do it as follows...first , check that the char is in the range 48-57, then
take the char and "bit_and" it with binary integer 1111. The result will be
the decimal numeric equivalent to the ascii number. There's prob many
"better" ways to do this. However, if you need to process a bunch of data,
some similar method is likely efficient.

I'd use something like:

#include<iostre am>

using namespace std;

int num_seeker(char charin){
return ((47 < charin) && (charin < 58)) ? charin & 15 : -1;
}

int main(){
char g;

for(int i = 0; i < 256; ++i){
g = i;
cout << i <<" "<< g <<" "<< num_seeker(g) << endl;
}

return 0;
}
Jul 22 '05 #2
"muser" <ch**********@h otmail.com> wrote...
is string converted to its integer equivalent by minusing it by 48?
No. But to convert a decimal digit in most codes (all codes that
I know of) to its decimal "value" you need to subtract '0' (zero
digit).

If you need to convert a string (are you talking of std::string?)
into its integer "equivalent ", use 'std::istringst ream'.
the function is suppose to check the fifth digit of struct member
using the formula contained within the function.
The function isn't doing that at the moment, hence this post and the
querie above. string must be of type char.
"Isn't doing" WHAT at the moment? What "formula" should it use?
If the code doesn't do what's expected of it, how should we be able
to recommend anything?

bool checkdigitforcu stomercode( char* string )
{

int weightingfactor = 5;
int checkdigit;
int remainder;
int check1;

for(int i =0; i < 5; ++i)
check1 += (string[i] - 48) * weightingfactor ;/* problem area */
If it's only the _fifth_ digit you need to check, why are you subtracting
_all_ of them?

If what you're trying to do here is to convert the five leading characters
of the 'string' array into a decimal number _manually_, that's not the right
way. The right way would be

check1 = (check1 * 10) + string[i] - '0';

(and (a) don't forget to initialise 'check1' to 0, and (b) there is a strong
assumption that all 'string' elements contain _decimal_digits _).
weightingfactor --;
remainder = check1 % 11;
Why "% 11"? Shouldn't it be "% 10"? And if you just need the 5th digit,
why not just take string[4] and look at it?

Your programming exercises are beyond me, to be honest with you. Where
are you getting all that stuff?

Try to do this: if you need something _corrected_, _describe_caref ully_
what you think it should do. Please try to avoid using "the formula
contained within the function" description. If you didn't do it right,
there is _no_ formula.

checkdigit = 11 - remainder;

for(i=4; i<=5; i++)
if(!string[i] == checkdigit)
return false;
if(checkdigit == 11){
checkdigit = 'X';
}
else
if(checkdigit = 10){
checkdigit = '0';
}

return true;
}

Jul 22 '05 #3
"Joe C" <jk*****@bellso uth.net> wrote...

"muser" <ch**********@h otmail.com> wrote in message
news:f9******** *************** ***@posting.goo gle.com...
is string converted to its integer equivalent by minusing it by 48?
If you want to strip numbers from the ASCII code, I'd think you would want
to do it as follows...first , check that the char is in the range 48-57,

then take the char and "bit_and" it with binary integer 1111. The result will be the decimal numeric equivalent to the ascii number. There's prob many
"better" ways to do this. However, if you need to process a bunch of data, some similar method is likely efficient.

I'd use something like:

#include<iostre am>

using namespace std;

int num_seeker(char charin){
return ((47 < charin) && (charin < 58)) ? charin & 15 : -1;
NEVER use straight values when working with characters, it's completely
unportable. You _ought_ to just use '0' and '9' (to form the expression

('0' <= charin && charin <= '9')

Better yet, use 'std::isdigit' function from <cctype>.
}

int main(){
char g;

for(int i = 0; i < 256; ++i){
g = i;
cout << i <<" "<< g <<" "<< num_seeker(g) << endl;
}

return 0;
}

Jul 22 '05 #4
muser wrote:

is string converted to its integer equivalent by minusing it by 48?
You mean: a single character, not a string.

Yes, you can do that. But do yourself a favour and replace 48
with '0'.

int AsInt;
char AsChar = '8';

AsInt = AsChar - '0';

AsInt now contains the value 8.
the function is suppose to check the fifth digit of struct member
using the formula contained within the function.
The function isn't doing that at the moment, hence this post and the
querie above. string must be of type char.

bool checkdigitforcu stomercode( char* string )
{

int weightingfactor = 5;
int checkdigit;
int remainder;
int check1;

for(int i =0; i < 5; ++i)
check1 += (string[i] - 48) * weightingfactor ;/* problem area */
weightingfactor --;
That looks strange. What is it supposed to do?

If you want to convert "32078" into an integer, then
you simply can do:

sscanf( string, "%d", &check );

But note: a 5 digit string, eg. "78542", may overflow
an int on some systems, noteable those where the maximum
signed int is 32768. So you'd better check that.

If you want to do it the hard way (by hand, insted of
sscanf) it is done this way:

check1 = 0;
for( int i = 0; i < 5; ++i )
{
check1 = 10 * check1;
check1 += string[i] - '0';
}

Consider the string "29671"
Check1 starts with a value of 0. Now the for
loop begins:

i = 0
Check1 * 10 -> 0
'2' - '0' -> 2 + Check1 -> 2

i = 1
Check1 * 10 -> 20
'9' - '0' -> 9 + Check1 -> 29

i = 3
Check1 * 10 -> 290
'6' - '0' -> 6 + Check1 -> 296

i = 4
Check1 * 10 -> 2960
'7' - '0' -> 7 + Check1 -> 2967

i = 5
Check1 * 10 -> 29670
'1' - '0' -> 1 + Check1 -> 29671

Finshed: The string "29671" has been converted to its
numerical equivalent 29671
The other thing your code: for(int i =0; i < 5; ++i)
check1 += (string[i] - 48) * weightingfactor ;/* problem area */
weightingfactor --;


could mean, is some sort of checksum over the digits, where
each digit has a different weight.
But then I guess it has to read:

for(int i =0; i < 5; ++i)
{
check1 += (string[i] - 48) * weightingfactor ;/* problem area */
weightingfactor --;
}

Note that the decrement of the weightingfactor is now enclosed
in the loop and each digit gets a different weightingfactor .
(Without that, the whole concept of weightingfactor wouldn't make
any sense, since the weightingfactor isn't used anywhere else in
your function).

Another tip: If you think that some computation doesn't work the
way you think it should, it is always a good idea to either
* use a debugger to check the variables
* or insert some outputstatement s which shed some light on what
is going on:

for( int i = 0; i < 5; ++i )
{
printf( "Check1: %d, String[%d] %c, Weight %d -> ",
check1, i, string[i], weightingfactor );

check1 += (string[i] - 48) * weightingfactor ;/* problem area */

printf( "%d\n", check1 );
}

Then you can see on your monitor what is going on and will be able to
check the computation. Once you understand what is going on and what
is wrong with that: fix the computation, use the output to verify that
it now works as you expect it to work and remove those additional output
statements.

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #5

"Victor Bazarov" <v.********@com Acast.net> wrote in message >
NEVER use straight values when working with characters, it's completely
unportable. You _ought_ to just use '0' and '9' (to form the expression

('0' <= charin && charin <= '9')

Better yet, use 'std::isdigit' function from <cctype>.


Victor, Thanks for the kick in the tail...again ;-)

I actually misread the original post...I thought he was stripping numbers
from a serial number/barcode type thingie...where portability would be
non-important.

on my original code...if you had a large file that contained a boatload of
ascii data to sort through...you could speed up my function a bit by using:

int num_seeker(char charin){
return (charin > 57) ? -1 : ((charin < 48) ? -1 : charin & 15);
}
That way you'd get your return value after just 1 comparison for all
characters (likely to be the most frequent) performing the second comparison
only if needed.

Victor...I respect your thoughts a lot. Would you ever do something like
this, or is it simply offensive to your programming sensibilities?

Jul 22 '05 #6
Joe C wrote:

"Victor Bazarov" <v.********@com Acast.net> wrote in message >
NEVER use straight values when working with characters, it's completely
unportable. You _ought_ to just use '0' and '9' (to form the expression

('0' <= charin && charin <= '9')

Better yet, use 'std::isdigit' function from <cctype>.


Victor, Thanks for the kick in the tail...again ;-)

I actually misread the original post...I thought he was stripping numbers
from a serial number/barcode type thingie...where portability would be
non-important.

on my original code...if you had a large file that contained a boatload of
ascii data to sort through...you could speed up my function a bit by using:

int num_seeker(char charin){
return (charin > 57) ? -1 : ((charin < 48) ? -1 : charin & 15);
}
That way you'd get your return value after just 1 comparison for all
characters (likely to be the most frequent) performing the second comparison
only if needed.

Victor...I respect your thoughts a lot. Would you ever do something like
this, or is it simply offensive to your programming sensibilities?

Can't talk for Victor.
But even then I would do:

return (charin > '9') ? -1 : ((charin < '0') ? -1 : charin - '0');

Much simpler to not have to remember the code values for the digits.
(On most CPU's I think its reasonable to assume that a subtraction
runs in the same time as an and masking).

--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #7
"Karl Heinz Buchegger" <kb******@gasca d.at> wrote...
Joe C wrote:

"Victor Bazarov" <v.********@com Acast.net> wrote in message >
NEVER use straight values when working with characters, it's completely unportable. You _ought_ to just use '0' and '9' (to form the expression
('0' <= charin && charin <= '9')

Better yet, use 'std::isdigit' function from <cctype>.


Victor, Thanks for the kick in the tail...again ;-)

I actually misread the original post...I thought he was stripping numbers from a serial number/barcode type thingie...where portability would be
non-important.

on my original code...if you had a large file that contained a boatload of ascii data to sort through...you could speed up my function a bit by using:
int num_seeker(char charin){
return (charin > 57) ? -1 : ((charin < 48) ? -1 : charin & 15);
}
That way you'd get your return value after just 1 comparison for all
characters (likely to be the most frequent) performing the second comparison only if needed.

Victor...I respect your thoughts a lot. Would you ever do something like this, or is it simply offensive to your programming sensibilities?

Can't talk for Victor.
But even then I would do:

return (charin > '9') ? -1 : ((charin < '0') ? -1 : charin - '0');

Much simpler to not have to remember the code values for the digits.
(On most CPU's I think its reasonable to assume that a subtraction
runs in the same time as an and masking).


Karl, you read my mind. Couple more of these and I'll let you talk
for me. :-)))

Victor
Jul 22 '05 #8
On Wed, 17 Dec 2003 20:26:06 +0100, Karl Heinz Buchegger
<kb******@gasca d.at> wrote:

[snip]
Can't talk for Victor.
But even then I would do:

return (charin > '9') ? -1 : ((charin < '0') ? -1 : charin - '0');

Much simpler to not have to remember the code values for the digits.
(On most CPU's I think its reasonable to assume that a subtraction
runs in the same time as an and masking).


It also has the advantage that it does not depend on the
character set being ASCII.

Sincerely,

Gene Wirchenko

Jul 22 '05 #9
In article <f9************ **************@ posting.google. com>,
ch**********@ho tmail.com says...
is string converted to its integer equivalent by minusing it by 48?


No. A single character might be, depending on the character set in use,
but a character is not a string.

If I'm reading your code correctly, I'd use something like this:

for (i=0; i<5;i++)
if ( isdigit(string[i]))
check1 += (string[i]-'0') * weightingfactor--;

The part where you have 'if (checkdigit = 10)' is almost certainly an
error -- you're doing an assignment instead of a comparison. In any
case, I'd probably cheat, and make this table-driven:

char digits[] = "0123456789 0X";

// loop from above.

checkdigit = digits[11 - (check1 % 11)];

I'm not quite sure I follow your 'if (!string[i] == checkdigit)'.
Perhaps you wanted: 'if (string[i] != checkdigit)'? comparison ('==')
has lower precedence than logical negation ('!'), so as-is, you're doing
logical negation on 'string[i]', and then comparing the result of that
to checkdigit. Logical negation is _normally_ done on a logical value,
and it looks like string[i] will be a character rather than boolean.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
5075
by: Tim Johansson | last post by:
I'm new to C++, and tried to start making a script that will shuffle an array. Can someone please tell me what's wrong? #include <iostream.h> #include <string.h> int main () { srand(time(0)); int array_length; int count;
5
7432
by: Anton Pervukhin | last post by:
Hello! Imagine the situation you have a class that interprets the command line of the program you are executing. This class is written by third party and has a main function parse with arguments the same as main: ClassA::parse(char **argv, int argc) While trying to write test cases for this function I came across the warnings appeared when I tried to initialize 'char** argv' straightly
2
3430
by: Peter Nilsson | last post by:
In a post regarding toupper(), Richard Heathfield once asked me to think about what the conversion of a char to unsigned char would mean, and whether it was sensible to actually do so. And pete has raised a doubt in my mind on the same issue. Either through ignorance or incompetence, I've been unable to resolve some issues. 6.4.4.4p6 states...
10
3286
by: kevin.hall | last post by:
GCC 3.3 and MSVS 6.0 have no problem converting char* to const char* (not even a warning), but MS's WinCE compiler generated an error complained that this was not possible. MS's WinCE compiler did allow a conversion from char* to const char** though. I'm interested in what the standard would say about this. (I don't have a copy.) Many thanks,
33
3669
by: Mark P | last post by:
A colleague asked me something along the lines of the following today. For some type X he has: X* px = new X; Then he wants to convert px to a char* (I'm guessing for the purpose of serializing the object array). I can think of three ways to do this:
10
1823
by: yaniv.dg | last post by:
hi all, i'm bumping into smething very starnge its very simple code but from some reason the program is acting strange this is my code: #include<stdio.h> #include<conio.h> void main(void) { int a,c; char b;
3
9530
by: Kevin Frey | last post by:
I am porting Managed C++ code from VS2003 to VS2005. Therefore adopting the new C++/CLI syntax rather than /clr:oldSyntax. Much of our managed code is concerned with interfacing to native C++ code (ie. wrappers etc). In Managed C++ there was an automatic conversion between const char* and String^. This was useful to us for two reasons: 1. We declared our string constants as eg. const char* const c_My_Constant = "blah", and these...
10
9089
by: Dancefire | last post by:
Hi, everyone, I'm writing a program using wstring(wchar_t) as internal string. The problem is raised when I convert the multibyte char set string with different encoding to wstring(which is Unicode, UCS-2LE(BMP) in Win32, and UCS4 in Linux?). I have 2 ways to do the job:
0
27265
by: maheshmohta | last post by:
Background Often while remodeling legacy application, one of the important tasks for the architects is to have an optimum usage of storage capabilities of database. Most of the legacy applications are constrained by the technology available at the time of their development and hence aren’t optimum as per current scenario. One of such cases is the extensive usage of CHAR fields, which aren’t optimum solution for space storage now. This paper...
9
26358
by: Eric | last post by:
I am working on a large, old code base and attempting to move it to GCC 4.2. Throughout the code, there is stuff like: char *aVar = "aString"; or void aFunc( char *aVar) { ... } aFunc( "aString" );
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10583
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10323
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9160
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7622
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6854
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5654
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2995
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.