PDA

View Full Version : how can i get the value of ascii character?



ayume
11-02-2011, 05:10 AM
anybody can help me to get the value of ascii character?
i make some encryption/decryption with Caesar chiper, and i need the value of ascii and then shift it.

Dr_Acula
11-02-2011, 06:05 AM
This sort of thing? http://www.asciitable.com/

Mike Green
11-02-2011, 06:06 AM
What do you mean by "value of ascii character"? If you are referring to the characters "0" through "9", you just have to subtract the value of the character "0". In Spin, if the character is in the variable X, you just subtract "0" like in X - "0"

SRLM
11-02-2011, 06:47 AM
Make sure that when you are doing the rotate that you have your alphabet begin at 0. If you try and rotate on the ASCII value it won't work, since the base value of "a" does not equal 0. So, you'll have to convert the ASCII to a 0 based scheme, rotate, and convert the result back to ASCII.

MagIO2
11-02-2011, 08:31 AM
If you want to do caesar cipher you don't need to know the values of the ascii characters because you can compare and calculate with characters as well. Let's say you have the text "propeller 1" somewhere and you want to cipher that. I think what you want is that letters are always mapped to letters again like:

abcdefghijklmnopqrstuvwxyz0123456789 <- original order
3456789abcdefghijklmnopqrstuvwxyz012 <- ceasar order with "key" 29 or -7

So the result would be "hkgh7cc7k u"

What you do to cipher, you simply find the index of the actual character you want to cipher in the original order. Then you add the "key" and do a modulo according to the number of characters you have in the original character-set. Then you pick the character from the position you calculated.
I'm not sure, but for finding the index the lookdown instruction could be usefull.

The advantage of using this way is that you can easily modify the character-set. For example adding -,.;: <space> and so on would not change the code.

ayume
11-02-2011, 09:01 AM
If you want to do caesar cipher you don't need to know the values of the ascii characters because you can compare and calculate with characters as well. Let's say you have the text "propeller 1" somewhere and you want to cipher that. I think what you want is that letters are always mapped to letters again like:

abcdefghijklmnopqrstuvwxyz0123456789 <- original order
3456789abcdefghijklmnopqrstuvwxyz012 <- ceasar order with "key" 29 or -7

So the result would be "hkgh7cc7k u"

What you do to cipher, you simply find the index of the actual character you want to cipher in the original order. Then you add the "key" and do a modulo according to the number of characters you have in the original character-set. Then you pick the character from the position you calculated.
I'm not sure, but for finding the index the lookdown instruction could be usefull.

The advantage of using this way is that you can easily modify the character-set. For example adding -,.;: <space> and so on would not change the code.



if i want change 1 (49) become < (60)
i just write like this ?

a:= string ("1")
a:= a + 11

Dr_Acula
11-02-2011, 12:47 PM
Yes that will work. But you might want to limit the characters to ones that you can display. Look at that ascii table, space is 32, and everything less than 32 won't display. And 127 is delete and that won't display either.

So a:= a+11 ' caesar shift
then (in Basic pseudo code)
if a>126 then a=a -127 ' 127 maps to 0, 128 maps to 1

and then add so space is not included
a = a+33.

and you can combine that into one line
if a>126 then a=(a-127+33)

which simplifies to
if a>126 then a=a-94

You can use a similar technique to limit the character set further, eg leave out all the numbers and {}!@#$%^$ characters

He he, you can even do a proper Caesar cypher and leave out characters that Caesar would not have used, like U and J *grin*

ayume
11-02-2011, 06:42 PM
@Dr_Acula, that's so clear. thank you



if i want join A and B become AB, what should i write?

a:= A
b:= B
c:= a & b ' not work...

MagIO2
11-02-2011, 09:21 PM
Here is some code:



con
_CLKMODE = XTAL1 + PLL16X
_XINFREQ = 5_000_000

obj
term: "CogOS_IO(Term)"

var
byte alpha_len, key
byte cipher_buffer[255]

word clear_message

pub cesar | i,idx,tmp
' Initialize terminal
term.start( 0,0 )

' wait until any key has been pushed
tmp:=-1
repeat while tmp==-1
term.str( string( "Test Lookdown",$0d ) )
term.str( string( "== please press key to start ==",$0d ) )
tmp:=term.rxtime( 1000 )
term.tx( 0 )

' setup
alpha_len := strsize( @alphabet )
key := 11
clear_message := string( "this message should be encrypted" )

' loop over all characters of the message
repeat i from 0 to strsize( clear_message )-1
' find the index of the actual character in the alphabet
idx:=0
repeat while alphabet[ idx ] and alphabet[ idx ]<>byte[clear_message][ i ]
idx++

' write the ciphered character
cipher_buffer[ i ] := alphabet[ (idx + key)//alpha_len ]

cipher_buffer[ i ] := 0

term.str( clear_message )
term.tx( 13 )
term.str( @cipher_buffer )

repeat

dat
alphabet byte " abcdefghijklmnopqrstuvwxyz0123456789",0


CogOS_IO can easily be replaced by FullDuplexSerial and you have a working program.
This code is generic and you can add any character to the alphabet that you need, which is the advantage of checking against number ranges of valid ascii characters.

Of couse this is a quick hack and if you want to make it fool-proof you have to check the length of the message. It should not be longer than the cipher-buffer. And currently the message should not contain chracters that are not in the alphabet. But both are easy changes.

MagIO2
11-02-2011, 09:33 PM
Oh ... and here is the code for decipher again:


key := alpha_len-key
' loop over all characters of the message
repeat i from 0 to strsize( clear_message )-1
' find the index of the actual character in the alphabet
idx:=0
repeat while alphabet[ idx ] and alphabet[ idx ]<>byte[@cipher_buffer][ i ]
idx++

' write the ciphered character
cipher_buffer[ i ] := alphabet[ (idx + key)//alpha_len ]

term.str( clear_message )
term.tx( 13 )
term.str( @cipher_buffer )
term.tx( 13 )


Simply add it to the end of the previous code.

ayume
11-03-2011, 04:36 AM
@MagIO2, that's great! thank you :smile:


if i want save the result of chiper/dechiper in a variable, it can't??

Duane Degn
11-03-2011, 05:06 AM
ayume,

You'll need to save the result of the cipher in an array. There's a recent thread (http://forums.parallax.com/showthread.php?135484-Simple-spin-questions)with a lot of discussion on storing strings in an array.

Duane

Bits
11-03-2011, 05:27 AM
@Dr_Acula, that's so clear. thank you



if i want join A and B become AB, what should i write?

a:= A
b:= B
c:= a & b ' not work...

Hope this helps


Var
long C
long b
long a

pub start | answer
answer := PackAintoBasHaRDasyoucan

Pub PackAintoBasHaRDasyoucan
return C := A << 8 | B

Duane Degn
11-03-2011, 04:24 PM
I think it's much safer to use bytes when dealing with characters and strings.

Here's how I'd join A and B (assuming A and B are characters):


CON
_clkmode = xtal1 + pll16x 'Use the PLL to multiple the external clock by 16
_xinfreq = 5_000_000 '5MHz crystal (QuickStart and Propeller Protoboards)
_Baud = 57600
_ArraySize = 3

VAR
byte a, b, c[_ArraySize]

OBJ
Pst : "Parallax Serial Terminal"
PUB Main
Pst.start(_Baud)
waitcnt(clkfreq * 2 + cnt) ' a couple of seconds to open terminal window.
Pst.str(string(13, "Simple String Test", 13, 13))
a := "A"
b := "B"
c[0] := a
c[1] := b
' c[2] remains zero, which we need, since strings should have a terminating zero.

Pst.str(string("a = "))
Pst.char(a)
Pst.str(string(", b = "))
Pst.char(b)
Pst.str(string(13, "c = "))
Pst.str(@c)


Here's the output:


Simple String Test
a = A, b = B
c = AB

MagIO2
11-03-2011, 07:42 PM
@ayume:
You should tell us more about your problem. Why do you want to pack several values into a single variable (long) ?

It's common that texts are stored in byte-arrays or byte-buffers - except you deal with unicode. If you want to send it via serial interface, if you want to store it to SD-card .... whatever .... usually these are byte-streams. So, what's the reason to do it different?

ayume
11-04-2011, 05:30 AM
yeah, i want to store the result of dechiper to SD card
i get the data from x-bee, dechiper it, n store to SD card

@MagIO2, your sample codes are helpfull me. i get the idea
@Duane, that's great. i think about bytes and your code is safe me

thank you guys :smile:

SRLM
11-04-2011, 05:59 AM
IIRC, an XBee can do transparent encryption without much effort. It certainly would be much better than a shift cipher.

ayume
11-04-2011, 10:06 AM
IIRC, an XBee can do transparent encryption without much effort. It certainly would be much better than a shift cipher.


can you please tell me more about that?

SRLM
11-04-2011, 06:28 PM
www.jsjf.demon.co.uk/xbee/xbee.pdf page 4-3...

A shift cipher has almost no security from anybody who is even vaguely interested in getting to the information. It's good for an introduction to cryptography, but the 128 bit AES built into the XBees is much better.