Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HELP NEEDED FOR CONVERTING FROM CHAR TO INTEGER!!!

Status
Not open for further replies.

sergelaurent

Technical User
May 30, 2005
52
FR
I have got a variable which contains a char. I want to convert it into a interger to be able to use it in the following espression
expr $a*56 where a is my variable

Can someone give me some tips to do it?

Thanks,
lolo
 
If I understand the question, you want to turn a character into its 8-bit integer representation.
scan $a %c var will take the character (must be a single character0 in the variable a and put its decimal value in the variable var.

_________________
Bob Rashkin
rrashkin@csc.com
 
Thanks Bong, I tried your code but it does not work for every caracters.

For example, if I have a Unicode caracter, the decimal returned by scan is not exact. one example is
set num "?"
scan $num %c conver
puts $conver
ans: 63 instead of 2

So, I wanted to know if you could help me to solve this problem.

Cheers,
lolo
 
Do you want to obtain the ASCII code of your char?
Then use:
Code:
binary scan c $conver result
puts "ASCII of $conver is $result"

Tobi
 
According to my notes, 63 IS the character code for "?".

_________________
Bob Rashkin
rrashkin@csc.com
 
Hello Bong,
Your notes are right, 63 corresponds to the caracter "?"
I think that when saving the document, the caracter ? for example is replaced by ? as when saving using notepad, I got a warning saying that unicode caracters will be lost when saving under ASCII format

cheers
lolo
 
I've never worked with unicode characters. Are they 2 bytes? What does your code look like and what does it do?

_________________
Bob Rashkin
rrashkin@csc.com
 
In fact, the data are coded in binary. When I read and copy them, in the result, I noticed that it added an '\n' that is '0D0A' in hexadecimal. In fact, instead of adding a new line feed only, it writes a return carriage and a new line feed. Have you ever come upon this problem? Can you help me?

I was wondering if the caracter '\n' was not a special caracter as I was not able to dissociate the 0D octet from the 0A octet

Thanks
lolo
 
Yes I have. I'm not sure you're experiencing the same thing as I did but to read a file as BINARY, you need to specify the translation on the channel (which defaults to ASCII).
set fid [open <filename> r]
[red]fconfigure $fid -translation binary[/red]


This will suppress the reading of 0A as linefeed. Of course, you do the same thing on a "w" channel. Also, if your using puts, specify -nonewline to avoid adding new lines.

_________________
Bob Rashkin
rrashkin@csc.com
 
Thanks a lot Bong, it is just working fine now.
Cheers
lolo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top