Shop OBEX P1 Docs P2 Docs Learn Events
how can i get the value of ascii character? — Parallax Forums

how can i get the value of ascii character?

ayumeayume Posts: 19
edited 2011-11-04 10:28 in Propeller 1
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.

Comments

  • Dr_AculaDr_Acula Posts: 5,484
    edited 2011-11-01 22:05
    This sort of thing? http://www.asciitable.com/
  • Mike GreenMike Green Posts: 23,101
    edited 2011-11-01 22:06
    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"
  • SRLMSRLM Posts: 5,045
    edited 2011-11-01 22:47
    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.
  • MagIO2MagIO2 Posts: 2,243
    edited 2011-11-02 00:31
    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.
  • ayumeayume Posts: 19
    edited 2011-11-02 01:01
    MagIO2 wrote: »
    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_AculaDr_Acula Posts: 5,484
    edited 2011-11-02 04:47
    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*
  • ayumeayume Posts: 19
    edited 2011-11-02 10:42
    @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...
  • MagIO2MagIO2 Posts: 2,243
    edited 2011-11-02 13:21
    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.
  • MagIO2MagIO2 Posts: 2,243
    edited 2011-11-02 13:33
    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.
  • ayumeayume Posts: 19
    edited 2011-11-02 20:36
    @MagIO2, that's great! thank you :smile:


    if i want save the result of chiper/dechiper in a variable, it can't??
  • Duane DegnDuane Degn Posts: 10,588
    edited 2011-11-02 21:06
    ayume,

    You'll need to save the result of the cipher in an array. There's a recent thread with a lot of discussion on storing strings in an array.

    Duane
  • BitsBits Posts: 414
    edited 2011-11-02 21:27
    ayume wrote: »
    @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 DegnDuane Degn Posts: 10,588
    edited 2011-11-03 08:24
    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
    
  • MagIO2MagIO2 Posts: 2,243
    edited 2011-11-03 11:42
    @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?
  • ayumeayume Posts: 19
    edited 2011-11-03 21:30
    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:
  • SRLMSRLM Posts: 5,045
    edited 2011-11-03 21:59
    IIRC, an XBee can do transparent encryption without much effort. It certainly would be much better than a shift cipher.
  • ayumeayume Posts: 19
    edited 2011-11-04 02:06
    SRLM wrote: »
    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?
  • SRLMSRLM Posts: 5,045
    edited 2011-11-04 10:28
    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.
Sign In or Register to comment.