www.digitalmars.com         C & C++   DMDScript  

c++ - string functions

reply Peter Chaffe <Peter_member pathlink.com> writes:
Hi all,
I'm new to this group so would like to introduce myself.
Still grappling with C++. Trying out every possible string asscotiated commands
at the moment but I've come to a problem.
How can I extract a decimal or hex value from a string containing letters?
The only fucntions I can find are ones like stoi which only work with string
numbers and ignore letters.  Please help!
Peter.
Dec 01 2002
parent reply "Matthew Wilson" <dmd synesis.com.au> writes:
Hi Peter

Try strtol(). It's the standard one -
http://www.mkssoftware.com/docs/man3/strtol.3.asp

some simple examples

#include <stdlib.h>

int main(int, char **)
{
    int    i1    =    strtol("16", NULL, 0);    // i == 16 (base 10 deduced)
    int    i2    =    strtol("16", NULL, 10);    // i == 16 (base 10
stipulated)
    int    i3    =    strtol("16", NULL, 16);    // i == 22 (base 16
stipulated)

    int    i4    =    strtol("011", NULL, 0);    // i == 9 (base 8 deduced)
    int    i5    =    strtol("011", NULL, 10);    // i == 11 (base 10
stipulated) - actually, this should be an error, since leading 0 means octal
    int    i6    =    strtol("011", NULL, 16);    // i == 17 (base 8
stipulated)


    int    i7    =    strtol("0x11", NULL, 0);    // i == 9 (base 16
deduced)
    int    i8    =    strtol("0x11", NULL, 10);    // i == 0 (base 10
stipulated) - fails, since base leading 0x means octal
    int    i9    =    strtol("0x11", NULL, 16);    // i == 17 (base 16
stipulated)

    return 0;
}

Hope that helps. If not, post more qs.

Matthew






"Peter Chaffe" <Peter_member pathlink.com> wrote in message
news:asf3f4$1n44$1 digitaldaemon.com...
 Hi all,
 I'm new to this group so would like to introduce myself.
 Still grappling with C++. Trying out every possible string asscotiated
commands
 at the moment but I've come to a problem.
 How can I extract a decimal or hex value from a string containing letters?
 The only fucntions I can find are ones like stoi which only work with
string
 numbers and ignore letters.  Please help!
 Peter.
Dec 02 2002
parent reply Peter <Peter_member pathlink.com> writes:
Hello Matthew,

thanks for the reply,

I've tried your code and it works fine for char(numbers) but char(letters) seem
to be an invalid input, returns 0.

As you may have gathered, I'm new to c++ so I keep experimenting.
This is the type of output I'm after:
Assume a char byte is saved. I am trying to get it's dec or hex value. ie:

char  hex   dec

"X" =  58    88 
"x" =  78    120
"y" =  79    121
"z" =  7A    122
---------------------------------------------------
Actually I've just solved it! see below:

char Letters. get loaded with "abcd" and the long int temp. accepts the values.
The problem now is I don't know if the values are in hex or dec as the cout<<
might be defaulting the output to dec.
Ok, I've added an xor with 1234h if the val in temp is hex then according to my
calculator the output should be either 13be-hex or 5054-dec depending on how <<
is affecting it.
abcd == 18A == 394 dec. Output = 394 ?
abcd xor 1234h = 1368 , don't add up!
abcd xor 4660 ie. dec equiv. , now I'm getting the right output but in dec so I
take it that the summing was in dec.. The right outputs should be either 13be or
5054 and I got 5054 so the cout<< must default to dec. see below:

Sorry for ranting on a bit. Just could'nt get my head around it to start with!
Hope it makes sense! Only beem writing C++ for about a fortnight, although I
used to mess with Quick basic some years ago so it's mainly the more powerful
commmands and syntax to get used to.
comment etc,

Peter. 





#include<iostream.h>

int main()
{
unsigned long temp=0, counter=0, OutPut=0;
char Letters[10]= "abcd";                        // letter characters

cout<<Letters;         
for (counter=1; counter<=strlen(Letters); counter++)  
{
temp = Letters[counter-1];          // get character of char

OutPut=OutPut +temp;                 // add result (temp) to OutPut
}

OutPut=OutPut ^ 1234;  //means1234 h  

cout<<OutPut<<"\n";   // decimal output by default
cout<<hex<<OutPut;    // Hex output

return 0;
}
Dec 02 2002
parent reply Al Bowers <abowers combase.com> writes:
I believe the problem is being caused by the way you are representing 
1234 hex. 1234 hex is represented by 0x1234.
so the xor statement should be:
Output = Output ^ 0x1234;

Run the following:

#include<iostream.h>
#include <string.h>

int main()
{
unsigned long temp=0;
char *s,Letters[10]= "abcd";                        // letter characters

cout  <<  Letters << endl;
for (s = Letters; *s;s++)  temp += *s;   // sum total of the characters

cout << "sum total of \"" << Letters << "\" give temp =  " << temp
      << " (dec)    " << hex << temp << " (hex)" << endl;

temp ^= 0x1234;  // xor temp with 1234 hex

cout << "Xor temp with 1234hex = \t" << temp
      << " (dec)    " << hex << temp << " (hex)" << endl;
return 0;
}




Peter wrote:

 Hello Matthew,
 
 thanks for the reply,
 
 I've tried your code and it works fine for char(numbers) but char(letters) seem
 to be an invalid input, returns 0.
 
 As you may have gathered, I'm new to c++ so I keep experimenting.
 This is the type of output I'm after:
 Assume a char byte is saved. I am trying to get it's dec or hex value. ie:
 
 char  hex   dec
 
 "X" =  58    88 
 "x" =  78    120
 "y" =  79    121
 "z" =  7A    122
 ---------------------------------------------------
 Actually I've just solved it! see below:
 
 char Letters. get loaded with "abcd" and the long int temp. accepts the values.
 The problem now is I don't know if the values are in hex or dec as the cout<<
 might be defaulting the output to dec.
 Ok, I've added an xor with 1234h if the val in temp is hex then according to my
 calculator the output should be either 13be-hex or 5054-dec depending on how <<
 is affecting it.
 abcd == 18A == 394 dec. Output = 394 ?
 abcd xor 1234h = 1368 , don't add up!
 abcd xor 4660 ie. dec equiv. , now I'm getting the right output but in dec so I
 take it that the summing was in dec.. The right outputs should be either 13be
or
 5054 and I got 5054 so the cout<< must default to dec. see below:
 
 Sorry for ranting on a bit. Just could'nt get my head around it to start with!
 Hope it makes sense! Only beem writing C++ for about a fortnight, although I
 used to mess with Quick basic some years ago so it's mainly the more powerful
 commmands and syntax to get used to.
 comment etc,
 
 Peter. 
 
 
 
 
 
 #include<iostream.h>
 
 int main()
 {
 unsigned long temp=0, counter=0, OutPut=0;
 char Letters[10]= "abcd";                        // letter characters
 
 cout<<Letters;         
 for (counter=1; counter<=strlen(Letters); counter++)  
 {
 temp = Letters[counter-1];          // get character of char
 
 OutPut=OutPut +temp;                 // add result (temp) to OutPut
 }
 
 OutPut=OutPut ^ 1234;  //means1234 h  
 
 cout<<OutPut<<"\n";   // decimal output by default
 cout<<hex<<OutPut;    // Hex output
 
 return 0;
 }
 
 
 
Dec 03 2002
parent Peter <Peter_member pathlink.com> writes:
Hello Al,

Much more eloquently written than my version!

I ran Your example and it brought to light what should have been obvious to me
all along. You see I was getting this hang-up over what the temp value was! If
it was in hex then to get a correct result it had to be xor'ed with your
(corrected) 1234h not 2660 dec. So why was yours AND my version working?
The penny finally dropped!. The hex and dec figures are only representative of
the same binary value meaning that it makes no difference what base you use at
the xor stage. ie. they can be mixed. Only the output is converting binary to
what readable form you require.

Thanks for the help!. I'll probably be asking more questions as I get deeper
into the C++ jungle.

Peter.
Dec 04 2002