How to convert char to int ?

sarkis

Member
Licensed User
Longtime User
Hi All,
How to convert char to int ?
and one more question

why this get an error?

Dim List1 as List
Dim c as Char
Dim i,j
i=0
j=0
c=List1.Get(i).charAt(j)

Thanks for answers
 
Last edited:

joseluis

Active Member
Licensed User
Longtime User
You know that i & j are strings, right?

You must declare them like this if you want them to be Int:
Dim i, j As Int
 
Upvote 0

sarkis

Member
Licensed User
Longtime User
how to convert char to int ,char to byte so on ...

I forget
of course i and j are int
and that doesn't work.'

What about conversion?
 
Upvote 0

Ricky D

Well-Known Member
Licensed User
Longtime User
if the c=List1.Get(i).charAt(j)

is a number then just do something like

B4X:
Dim res As Int
res = List1.Get(i).charAt(j)

no special conversion needed

or are you wanting the ascii number of the char?

regards, Ricky
 
Upvote 0

sarkis

Member
Licensed User
Longtime User
convert

Thanks for responding
But
Dim res As Int
res = List1.Get(i).charAt(j)
doesn't compile.
It says unknown charat.
I did folowing

Dim str1 As String
str1=List1.Get(i)
res= Asc(str1.CharAt(j))-48
if you fnow shorter solusion please let me know.
 
Upvote 0

specci48

Well-Known Member
Licensed User
Longtime User
Hello sarkis,

List1.Get(i) returns an object, not a string.
CharAt is only available for strings so if you code
B4X:
List1.Get(i).charAt(j)
you try to get a character directly from an object - which won't work.

Assing List1.Get(i) to a string is a form of type conversion (called "casting" in other languages).


specci48
 
Upvote 0

r2d4

Member
Licensed User
Longtime User
Just for understanding: Do you want the ascii code of a given char? Or just the char at a specific position in a string?
 
Upvote 0

sarkis

Member
Licensed User
Longtime User
get numbers from txt file

generally I was in need to read numbers from txt file.
in this time that were single numbers (0-9).
What I did you see but how do that in a simply way ,when numbers are bigger than 9 ?
 
Upvote 0
Top