Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Binary to Hexadecimal Conversion

32 views
Skip to first unread message

rick

unread,
Jul 25, 2007, 3:31:59 AM7/25/07
to
Hi,

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.

skuh...@web.de

unread,
Jul 25, 2007, 4:14:54 AM7/25/07
to

rick wrote:
> Is there any direct command to convert binary to Hexadecimal. I dont
> think "format" command does this.

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

Donal K. Fellows

unread,
Jul 25, 2007, 4:27:18 AM7/25/07
to
rick wrote:
> Is there any direct command to convert binary to Hexadecimal. I dont
> think "format" command does this.

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.

rick

unread,
Jul 25, 2007, 5:02:40 AM7/25/07
to
Thanks Stephan and Donal,

Two superb solutions. I was doing in very raw way.

On Jul 25, 1:27 pm, "Donal K. Fellows"

Gerald W. Lester

unread,
Jul 25, 2007, 8:48:18 AM7/25/07
to

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|
+------------------------------------------------------------------------+

rick

unread,
Jul 26, 2007, 6:45:42 AM7/26/07
to

>
> Hmmm, someone asked this same question a couple of weeks ago. Is someone
> teaching a Tcl class and using this as a homework assignment?
>

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.

Cameron Laird

unread,
Jul 26, 2007, 11:50:25 AM7/26/07
to
In article <1185446742.6...@x35g2000prf.googlegroups.com>,

Do continue. Testing <URL: http://wiki.tcl.tk/testing > is a
GREAT domain for Tcl--and telecomm in particular needs the help.

sleb...@gmail.com

unread,
Jul 27, 2007, 3:32:42 AM7/27/07
to
On Jul 26, 6:45 pm, rick <vagabond_maver...@yahoo.com> wrote:
> > Hmmm, someone asked this same question a couple of weeks ago. Is someone
> > teaching a Tcl class and using this as a homework assignment?
>
> 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.
>

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.

0 new messages