Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

interesting

Status
Not open for further replies.

6839470

Programmer
Mar 7, 2004
45
US

proc binconv {} {
set key_size 8
set plaintext_size 100
set binary_size [expr $plaintext_size * 8]
set binarykey_size [expr $key_size * 8]
puts -nonewline "Enter the Plain Text :- "
puts -nonewline "Enter the Key:- "

set plain_text sumanthisgreat
set des_key jamesbond
set plain_text_length [string length $plain_text]
set key_length [string length $des_key]

puts "$plain_text_length"
puts "$key_length"

# converting the key to binary

set k 0
for { set i 0} {$i < $key_length} {incr i} {
for { set j 7} {$j >= 0} { incr j -2} {
if { {1 << $j} & $key_length } {
bin_key_array($k) = 1
} else {
bin_key_array($k) = 0
}
incr k;
}
}
puts "key array"
for {set i 0} {$i < $key_length * 8} {incr i} {
puts "$bin_key_array[$i]"
}
}



I want to convert plain to binary this isn't wrking
anyone to rescue
 
I'm not sure to understand what you want.
If you need to get bits from chars or chars from bits, here is the code:
Code:
  # getting bits from chars
  set str abcd
  set n 0
  foreach char [split $str {}]   {
    binary scan $char B8 var($n)
    puts "$char: $var($n)"
    incr n
  }
->a: 01100001
->b: 01100010
->c: 01100011
->d: 01100100
  # getting chars from bits
  foreach n [array names var]   {
    puts "$var($n): [binary format B8 $var($n)]"
  }
->01100001: a
->01100010: b
->01100011: c
->01100100: d
With the B8 option, binary scan gets a char and returns a binary.
With the B8 option, binary format gets a binary and returns a char.

More on the binary command at:
HTH

ulis
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top