String to ASCII
Klap
Posts: 65
I was wondering if there was an easy way to convert ASCII text into its equivalent numerical form?
Here is the spin code I used to do it (also see attachment)
I was thinking that there must be a better way. Maybe with Assembly?
Here is the spin code I used to do it (also see attachment)
{ ──────────────────────────────────────────────────────────────────────────────────────── File: StringToASCII.spin Programmer: Aaron Klapheck Date: 31-Mar-09 Code that may need to be changed is bracketed by "***" and is located in the CON section only. In the PUB init block the speaker code may be deleated if a speaker is not being used. Run this program by itself to test to see if it is working. If calling this program as a subroutine (its primary use) then don't worry about changing any of the code. ──────────────────────────────────────────────────────────────────────────────────────── } CON ' These are constants I include in most code I write because of there universality and usefulness. 'Clock ***May need to change this section depending on crystal being used*** _clkmode = xtal1 + pll16x _xinfreq = 5_000_000 'Timing clockfreq = ((_CLKMODE - XTAL1) >> 6) * _XINFREQ _1uS = clockfreq / 1_000_000 'Divisor for 1 uS _1mS = clockfreq / 1_000 'Divisor for 1 mS _1S = clockfreq / 1 'Divisor for 1 S 'Serial Terminal STrxpin = 31 STtxpin = 30 STmode = 0 STbaud = 9600 '***may need to change*** 'Speaker ***Can Delete this section if not using piezospeaker*** frequency_audible = 2093 ' 2093Hz. C7 note piezospeaker = 0 '***may need to change*** CON ProgramSpecific ' These are constants that change depending on the rest of the program. obj ' General routines Debug : "FullDuplexSerialPlus" SqrWave : "SquareWave" PUB init ' Initialization Routine ' Piezospeaker Beeps for 2s then waits for 1/4s. '***Start of Piezospeaker code*** SqrWave.Freq(0, piezospeaker, frequency_audible) waitcnt(clkfreq*2 + cnt) dira[noparse][[/noparse]piezospeaker] := outa[noparse][[/noparse]piezospeaker] := frqa := ctra := 0 waitcnt(clkfreq/4 + cnt) '***End of Piezospeaker code*** ' Activate and clear Serial Terminal Debug.start(STrxpin, STtxpin, STmode, STbaud) Debug.tx(Debug#CLS) StringToAscii pub StringToAscii | index, stringptr ' Run this to test the program. Good examples. Debug.str(string("Testing String To ASCII Code", Debug#CR)) Debug.str(string("Number 8 in ASCII (should be 56) = ")) Debug.Dec( Converter("8")) Debug.str(string(Debug#CR, "Letter A in ASCII (should be 65) = ")) Debug.Dec(Converter("A")) Debug.str(string(Debug#CR, "Letter a in ASCII (should be 97) = ")) Debug.Dec(Converter("a")) Debug.str(string(Debug#CR, "Should say hi there 9 times (in ASCII): ")) stringptr := string("hi there 9 times") repeat strsize(stringptr) Debug.Dec(Converter(byte[noparse][[/noparse]stringptr++])) Debug.tx(" ") Debug.str(string(Debug#CR, "This is what the user should see: 104 105 32 116 104 101 ...(and so on)")) Debug.str(string(Debug#CR, "End of program", Debug#CR)) PUB Converter(sym) : ascii ' All Characters except case sym 'Numbers 'originally: 0..9 : ascii := sym + 48 "1" : ascii := 48 "2" : ascii := 49 "3" : ascii := 50 "4" : ascii := 51 "5" : ascii := 52 "6" : ascii := 53 "7" : ascii := 54 "8" : ascii := 55 "9" : ascii := 56 'Special Characters/Symbols " " : ascii := 32 "!" : ascii := 33 'skip " "#" : ascii := 35 "$" : ascii := 36 "%" : ascii := 37 "&" : ascii := 38 "'" : ascii := 39 "(" : ascii := 40 ")" : ascii := 41 "*" : ascii := 42 "+" : ascii := 43 "," : ascii := 44 "-" : ascii := 45 "." : ascii := 46 "/" : ascii := 47 ":" : ascii := 58 ";" : ascii := 59 "<" : ascii := 60 "=" : ascii := 61 ">" : ascii := 62 "?" : ascii := 63 "@" : ascii := 64 "[noparse][[/noparse]" : ascii := 91 "\" : ascii := 92 "]" : ascii := 93 "^" : ascii := 94 "_" : ascii := 95 "'" : ascii := 96 "{" : ascii := 123 "|" : ascii := 124 "}" : ascii := 125 "~" : ascii := 126 'skip delete case sym 'Upper Case Letters "A" : ascii := 65 "B" : ascii := 66 "C" : ascii := 67 "D" : ascii := 68 "E" : ascii := 69 "F" : ascii := 70 "G" : ascii := 70 "H" : ascii := 72 "I" : ascii := 73 "J" : ascii := 74 "K" : ascii := 75 "L" : ascii := 76 "M" : ascii := 77 "N" : ascii := 78 "O" : ascii := 79 "P" : ascii := 80 "Q" : ascii := 81 "R" : ascii := 82 "S" : ascii := 83 "T" : ascii := 84 "U" : ascii := 85 "V" : ascii := 86 "W" : ascii := 87 "X" : ascii := 88 "Y" : ascii := 89 "Z" : ascii := 90 'Lower Case Letters "a" : ascii := 65 + 32 "b" : ascii := 66 + 32 "c" : ascii := 67 + 32 "d" : ascii := 68 + 32 "e" : ascii := 69 + 32 "f" : ascii := 70 + 32 "g" : ascii := 70 + 32 "h" : ascii := 72 + 32 "i" : ascii := 73 + 32 "j" : ascii := 74 + 32 "k" : ascii := 75 + 32 "l" : ascii := 76 + 32 "m" : ascii := 77 + 32 "n" : ascii := 78 + 32 "o" : ascii := 79 + 32 "p" : ascii := 80 + 32 "q" : ascii := 81 + 32 "r" : ascii := 82 + 32 "s" : ascii := 83 + 32 "t" : ascii := 84 + 32 "u" : ascii := 85 + 32 "v" : ascii := 86 + 32 "w" : ascii := 87 + 32 "x" : ascii := 88 + 32 "y" : ascii := 89 + 32 "z" : ascii := 90 + 32 DAT 'Use what is bellow only as a reference (doesn't accutally do anything). This was a failed program I was working on. { CharactersTo47 Long 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47 ' space ! " # $ % & ' ( ) * + , - . / Numbers Long 48, 49, 50, 51, 52, 53, 54, 55, 56, 57 ' 0 1 2 3 4 5 6 7 8 9 CharactersTo64 Long 58, 59, 60, 61, 62, 63, 64 ' : ; < = > ? @ UCLetters Long 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90 ' A B C D E F G H I J K L M N O P Q R S T U V w X Y Z CharactersTo96 Long 91, 92, 93, 94, 95, 96 ' [noparse][[/noparse] \ ] ^ _ ` LCLetters Long 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122 'a b c d e f g h i j k l m n o p q r s t u v w x y z CharactersTo127 Long 123, 124, 125, 126, 127 ' { | } ~ DEL } {{ASCII Character Chart 32 space 33 ! 34 " 35 # 36 $ 37 % 38 & 39 ' 40 ( 41 ) 42 * 43 + 44 , 45 - 46 . 47 / 48 0 49 1 50 2 51 3 52 4 53 5 54 6 55 7 56 8 57 9 58 : 59 ; 60 < 61 = 62 > 63 ? 64 @ 65 A 66 B 67 C 68 D 69 E 70 F 71 G 72 H 73 I 74 J 75 K 76 L 77 M 78 N 79 O 80 P 81 Q 82 R 83 S 84 T 85 U 86 V 87 w 88 X 89 Y 90 Z 91 [noparse][[/noparse] 92 \ 93 ] 94 ^ 95 _ 96 ` 97 a 98 b 99 c 100 d 101 e 102 f 103 g 104 h 105 i 106 j 107 k 108 l 109 m 110 n 111 o 112 p 113 q 114 r 115 s 116 t 117 u 118 v 119 w 120 x 121 y 122 z 123 { 124 | 125 } 126 ~ 127 DEL }}
I was thinking that there must be a better way. Maybe with Assembly?
Comments
"a" is not a single character string, its the Spin notation for: Ascii value of a. So what you do here is compare each Ascii value
with the Ascii value you have passed to the Convert methode and set then then Ascii value as return value. Or in other words you can write:
This methode makes not much sense, you pass the ascii value and return it. You simply can write:
That's the same.
Andy
Asciis are just numbers which you give a different meaning.
string byte $31,$35,$33,$30,$00
is the same as
string byte 48, 52,50,47,0
and is the same as
"1530"
It's all a matter of interpretation of the numbers.
So, your code is in the end doing nothing. It converts a number to itself with an expensive case statement.
ascii := sym should do the same
Your code is of course helpful if you want to convert an ASCII to another code. But then the easiest way is to use the lookup/lookdown SPIN commands.
the '0' is missing and the values for '1'-'9' are -1 off.
regards
adrian
Singer code was an octal based character set. The guy I worked for wouldn't tell me anything about it;
I swear he was laughing under his breath while I was playing around with different bit combinations.
Have a look at the Propeller Tool Help->View Character Chart. It shows ASCII and extentions of the Propeller font.
One of the easiest ways to convert a character like 9 to a number in spin is:
number := char - "0" ' if number > -1 and number < 10, it is a valid natural number
Hex numbers can be derived the same way ... assuming char is lower case,
hexnum := char - "a"
You can use char |= $20 to force char to lower case ... this also preserves the number if it began as one.
Google "ASCII code" to find more about this stuff.
Enjoy your computing journey Klap.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
--Steve
Propalyzer: Propeller PC Logic Analyzer
http://forums.parallax.com/showthread.php?p=788230
I'm with you... very special. I'm glad you asked and tonight when I actually get a chance to read it all, I'm going to enjoy it and learn a little.
Rich