Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > string parity module

Reply
Thread Tools

string parity module

 
 
snacktime
Guest
Posts: n/a
 
      08-12-2006
Anyone know of an existing module for settings odd/even/space parity on strings?

 
Reply With Quote
 
 
 
 
snacktime
Guest
Posts: n/a
 
      08-13-2006
If anyone has any input on how to write this better or a better way to
implement it let me know. In any case here is a class for setting
odd/even/space parity on strings.

class StringParity

def initialize
even_bits = "\0"
odd_bits = "\200"
codes = ""
ascii = []

@even_parity = []
@odd_parity = []
@space_parity = []

8.times do
even_bits << odd_bits
odd_bits = even_bits.clone
odd_bits.tr!("\0\200","\200\0")
end


(0..255).collect {|i| ascii << i}
codes = ascii.pack('C*')
(0..127).collect { |i| @space_parity[i + 128] = codes[i] }
@even_parity = (0..codes.length-1).collect { |i| codes[i] ^ even_bits[i] }
@odd_parity = (0..codes.length-1).collect { |i| codes[i] ^ odd_bits[i] }
end

def setEvenParity(str)
new = []
str.each_byte {|b| new << @even_parity[b]}
new.pack("C*")
end

def setOddParity(str)
new = []
str.each_byte {|b| new << @odd_parity[b]}
new.pack("C*")
end

def setSpaceParity(str)
new = []
str = str.unpack("C*")
str.collect {|b| new << (@space_parity[b] || b)}
new.pack("C*")
end

end

s = StringParity.new
str = "testing12345678910()*&^%@!~"

str = s.setEvenParity(str)
print "#{str}\n"

str = s.setSpaceParity(str)
print "#{str}\n"

str = s.setOddParity(str)
print "#{str}\n"

str = s.setSpaceParity(str)
print "#{str}\n"

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Parity Check Ed VHDL 18 10-06-2009 12:43 PM
PA-2FE-TX Parity Bug ttrotter01@yahoo.com Cisco 1 03-30-2006 08:13 PM
Cisco 3640A: GT64010 Master Write Parity Error Dev Cisco 1 01-15-2006 12:47 PM
Set parity of a string snacktime Python 8 01-26-2005 08:47 PM
disable parity checking on cisco router how? Walter Roberson Cisco 3 01-15-2004 10:56 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57