Get the ascii value of a string

Hi,

I am wanting to get the value of my string to send as a checksum.
(I found atoi will not work.  I have also tried __isascii but maybe I am not using it correctly.)

Can you please give me some help with this?

Thank you
drodgersAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cgallagherCommented:
drodgers,
To get the ascii value of a "char" the following will work:

int i;
char c='A';
i = (int)c;
cout<<i; //output the ascii char

Casting the char as an int causes the integer to take the ascii value of the character.

If you have a pointer/string of chars then simply this will work:


char *c="ABCD";
int i[sizeof(c)],j;
for(j=0;j<sizeof(c);j++)
{
    i[j]=(int)c[j];
    cout<<i[j]<<" "; //output the ascii chars
}

I don't see why the above code should work for you.
Leaving you with an array of integers (int i[]) which equal the ascii of the chars in "c".
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cgallagherCommented:
The last two lines were meant to read

"Leaving you with an array of integers (int i[]) which equal the ascii of the chars in "c".
I don't see why the above code shouldn't work for you. "

0
poohbear_68Commented:
As a replacement for atoi, you can use the following function:

DWORD StrToInt(const char *szNum)
{
   if(szNum == NULL)
      return 0;
   
   DWORD dwNum = 0;
   BOOL bNegative = FALSE;
   
   for(int iCnt = 0; szNum[iCnt]; iCnt++)
   {
      if(iCnt == 0 && szNum[0] == '-')
      {
         bNegative = TRUE;
         continue;
      }
     
      dwNum *= 10;
      dwNum += szNum[iCnt] - '0';
   }
   
   if(bNegative)
      dwNum = 0 - dwNum;
   
   return dwNum;
}
0
cgallagherCommented:
In case you wish to convert the int into a char then look at the following code.

int i;
char c='A';
char d[3];
i = (int)c;
_itoa(i,d,10);
cout<<"c = "<<c<<endl;
cout<<"i = "<<i<<endl;
cout<<"d = "<<d<<endl;

It converts the original 'A' into an int = 65 (ascii value) then converts this int into a character array = "65".
 Notice how the last array "d" had to be three chars long (char d[3]) to hold the int value.
This is becaus for a char to hold the full ascii set (up to 255) it needs to be 3 chars long for each ascii number.

I hope this has solved your problem.
Rgds,
C.

P.S Sorry for giving you fragments of code. I'm a bit tired and am not thinking straight.

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.