Is there any direct command to convert binary to Hexadecimal. I dont
think "format" command does this.
My input will mostly be a 32 bit binary number and the output should
be in hex
eg : 11110000111100001111000011110000 should become F0F0F0F0.
One way is to take four bits a time and convert those to Hex, but I
was looking for something more logical or direct.
I don't know of such a command, but if I understand you correctly, you
do not want to convert real binary data to hex, but strings containing
1s and 0s to a hex-string, right? If so, try this, I use it for
programming as a simple bin2hex-converter:
---
proc bin2hex {bin} {
set result ""
set prepend [string repeat 0 [expr (4-[string length $bin]%4)%4]]
foreach g [regexp -all -inline {[01]{4}} $prepend$bin] {
foreach {b3 b2 b1 b0} [split $g ""] {
append result [format %X [expr {$b3*8+$b2*4+$b1*2+$b0}]]
}
}
return $result
}
---
It splits the string into groups of 4 digits and builds a hex-string
from them.
HTH, Regards
Stephan
No, but the [binary] command can do it.
> My input will mostly be a 32 bit binary number and the output should
> be in hex
> eg : 11110000111100001111000011110000 should become F0F0F0F0.
> One way is to take four bits a time and convert those to Hex, but I
> was looking for something more logical or direct.
Try this:
proc bitsToHex bits {
set binValue [binary format B32 $bits]
binary scan $binValue H8 hex
return $hex
}
puts [bitsToHex 11110000111100001111000011110000]
Donal.
Two superb solutions. I was doing in very raw way.
On Jul 25, 1:27 pm, "Donal K. Fellows"
Hmmm, someone asked this same question a couple of weeks ago. Is someone
teaching a Tcl class and using this as a homework assignment?
--
+--------------------------------+---------------------------------------+
| Gerald W. Lester |
|"The man who fights for his ideals is the man who is alive." - Cervantes|
+------------------------------------------------------------------------+
I searched in this form for binary to hex conversion, but couldn't
find a relevant thread, only after that I posted my query.I am sorry
if my query is a duplicate of the one posted a few weeks back.
I work in a telecom organization where there is no particular deptt.
doing Scripting work. I am in System Testing and also do some
scripting to automate test cases. The only help to me is this group,
and I dont have a development background, and hence keep asking
queries to do efficient scripting.
Do continue. Testing <URL: http://wiki.tcl.tk/testing > is a
GREAT domain for Tcl--and telecomm in particular needs the help.
Don't worry. The thread in question wasn't about "binary to hex"
conversion. It was about "convert binary (4-bits) to hex". With a
title like that I'm not surprised you didn't find it when searching
for "binary to hex conversion". Anyway, the thread in question was:
http://groups.google.com/group/comp.lang.tcl/browse_frm/thread/42a7dc067e17cb9a/6a6e1e84060a28dd
The code I posted works for numbers of up to 32 bits. Pretty straight
forward implemented by shifting through the binary string and doing
basic binary math. Though I suspect Donald's code in this thread is
much faster since most of the magic is done in C.