Converting char to ascii value

H.Ghassami

I know it is stupid question but it does not resolve, and I am googling but that didn't help either. I want to select the sub string from my string and then convert it to ASCII value, but it shows me an error:

int a=char(S.substr(i-1,1));
        int b=S.substr(i ,1);
      if (( a== 13) && (b== 10))
        break;  

this is my error :

pdusms.cpp:1020: error: invalid cast from type 'std::basic_string' to type 'char' int a=char(S.substr(i-1,1));
pdusms.cpp:1021: error: invalid conversion from 'const char*' to 'int' [-fpermissive] int b=S.substr(i ,1).c_str(); ^
How can i do that? how can i change the char to int (show ASCII value)

molbdnilo

The result of substr is a string, not a character.
A one-character string is not a character.

Use indexing to get the characters.

You want

int a = S[i-1];
int b = S[i];

but if you're looking for line delimiters you shouldn't compare to ASCII values - use

if (S[i-1] == '\r' && S[i] == '\n')

which is portable.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Converting integer value to ASCII char

Converting char to ASCII symbol

C++ converting char variable to string variable is returning ASCII value

Converting ascii value to correspondence char in a string (Ex - T104a110k115) - java

Converting ASCII value in the middle of a string

Converting to Char/String from Ascii Int in Swift

Converting char to ASCII and display in C#?

Get decimal ascii value of a char

representing a hexadecimal value by converting it to char

simple way of converting ASCII into HEX with no change in value

Converting QString to Ascii value & Vice Versa in Qt

Converting between string bit and string char out to ASCII

c++ char display only ascii value?

How to convert char to ascii value in kotlin language

How integer automatically convert char into ASCII value?

Save Char value from ASCII in a bash variable

Is there a way to convert a char to its ASCII value in assembly?

Converting Char value to money for concatenated variable

Converting Binary to ASCII, and ASCII to Binary

How can I add to the ascii value of a char letter?

How to put the char with the smallest ASCII value at the end of the String recursively?

Convert a shifted ASCII value to char inside the abc scope

convert ASCII value to a character based on specific char in php

Haskell converting [Char] to Char

Converting char[,] array to char**

Arduino converting value from file.read() to const char*

Converting Char to its Numerical value without using getNumericValue

Converting int value to char pointer difference in C and C++

Converting a substring char to an int from a string value then assigning it to an int variable