Computer Science Canada

ASCII to binary

Author:  Thuged_Out_G [ Sun Dec 14, 2003 3:28 am ]
Post subject:  ASCII to binary

how do you figure out the binary value of an ASCII char/word?
i want to make a program to do this, but i cant do that until i know how to convert ASCII to binary

Author:  Tony [ Sun Dec 14, 2003 3:49 am ]
Post subject: 

well you get the ASCII value using ord()

code:

put ord("a")


then use intstr() fucntion to convert it to binary Laughing
code:

put intstr(ord("a"),0,2)

Author:  Thuged_Out_G [ Sun Dec 14, 2003 3:51 am ]
Post subject: 

code:
put intstr(ord("a"),0,2)


what does the ,0,2) mean? [/code]

Author:  Tony [ Sun Dec 14, 2003 4:06 am ]
Post subject: 

0 is the length out returning value... works the same as :# after a string, so 0 will not affect the output in any way.

2 is the base to which the number is converted to. 8 will make output a hex.

Author:  Dan [ Tue Dec 16, 2003 12:37 pm ]
Post subject: 

yes tony come to dark side with me, use the intstr Twisted Evil

just to let you know that if that is for school your teacher may not like intstr it kind of takes the calangen out of making a to any bases convertor Twisted Evil

Author:  Thuged_Out_G [ Tue Dec 16, 2003 4:03 pm ]
Post subject: 

no its not for school, i was just interested in how it was done after i downloaded tony's one in VB, i think it was tony who did it

Author:  santabruzer [ Tue Dec 16, 2003 5:02 pm ]
Post subject: 

so that's the simple of converting to binary... . Embarassed ..
i was using the div 2 and rem 2 dealy.. the same way as by hand.. now it's so simple..

Author:  PaddyLong [ Tue Dec 16, 2003 5:35 pm ]
Post subject: 

hex is base 16 .... base 8 is octal

Author:  Tony [ Tue Dec 16, 2003 6:11 pm ]
Post subject: 

for my VB one, you basically grab the ord() value of the letter and turn that number into binary.

start from 2^8 and move down. If the number is larger then or equals to 2^counter then bit is 1, subtract value from number, else bit is 0. And so on until 2^0.

Author:  Andy [ Tue Dec 16, 2003 8:01 pm ]
Post subject: 

why use rem when u can use div?

Author:  PaddyLong [ Tue Dec 16, 2003 8:42 pm ]
Post subject: 

you use the remainder because any number divided by 2 will give a remainder of 0 or 1 ... so to convert from base 10 (decimal) to base 2 (binary) you just keep dividing by two and keeping track of the remainder...
ex..
25 / 2 = 12 R 1
12 / 2 = 6 R 0
6 / 2 = 3 R 0
3 / 2 = 1 R 1
1 / 2 = 0 R 1

then you read from the bottom up and record the remainders...
so 25 base 10 = 11001 base 2

another way is to use the greatest power of 2 that's less than the number...
25-16 = 9
9 - 8 = 1
1 - 1 = 0
so this means you have the 1, 8 and 16 bits set...
11001

Author:  Andy [ Tue Dec 16, 2003 8:44 pm ]
Post subject: 

i meant to say mod


: