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

Question: convert binary (4-bits) to hex nibble or decimal

205 views
Skip to first unread message

dkush...@yahoo.com

unread,
Jul 11, 2007, 7:18:29 PM7/11/07
to
Hello Everyone,
I've been playing with this sfor a couple of hours and thought it
mighht be quicker to ask if anyone knows how to do this ....
I need to convert Binary to Hex or decimal. I know that I should use
binary scan but I'm not getting what I want out of it. Man, there is a
lot more to the binary format/scan commands that I realized. I've
tried about 50 things and I still don't get what I want out!

I want a proc:
## Convert bin to dec or hex
proc convBinToHexNib { binIn mode } {
?? code ??
if {$mode == dec} {return decimal}
if {$mode == hex} {return hex}
}

Syntax: convBinToHexNib 1100
decimal return: 12
or
hex return: C

Thanks,
- Daniel

wiede...@googlemail.com

unread,
Jul 11, 2007, 8:23:14 PM7/11/07
to

dkushne...@yahoo.com schrieb:

proc convBinToHexNib { binIn {mode hex} } {
set ret error
switch -glob -- $mode \
hex* {
set val [binary format B $binIn]
binary scan H $val ret
} dec* {
set val [binary format B* 0000$binIn ]
binary scan $val c ret

} default {
puts stderr "WHAT mode? $mode"
}
return $ret
}

dkush...@yahoo.com

unread,
Jul 11, 2007, 8:34:19 PM7/11/07
to
Daniel
>
> proc convBinToHexNib { binIn {mode hex} } {
> set ret error
> switch -glob -- $mode \
> hex* {
> set val [binary format B $binIn]
> binary scan H $val ret
> } dec* {
> set val [binary format B* 0000$binIn ]
> binary scan $val c ret
>
> } default {
> puts stderr "WHAT mode? $mode"
> }
> return $ret
> }
>

Thanks a million for that! Actually, it returns an error for hex mode
and displays a CTL-Char (square), rather than the hex code. The square
char doesn't print here on the browser, but I do see it in the TCL
shell window. What is this?:

% convBinToHexNib 0100 dec
4
% convBinToHexNib 0100 hex
bad field specifier "

Thanks,
- Daniel


sleb...@gmail.com

unread,
Jul 11, 2007, 9:50:10 PM7/11/07
to

You don't really want [binary scan/format] for this unless your're
doing [binary format] to convert the bit string to raw value and then
[binary scan] to convert it back to a number string. You can easily
convert the bitstring to a number using basic math + string
manupilation. Here's a straightforward implementation:

# This works for up to 32 bits.
# Any more and the value will roll over.
proc bin2int {binstring} {
set ret 0
foreach bit [split $binstring ""] {
set ret [expr {$ret << 1}]
if {[string is boolean $bit]} {
set ret [expr {$ret | $bit}]
} else {
error "string is not binary!"
}
}
return $ret
}

# Usage: convert nybble to decimal:
set dec [bin2int 1010]

# convert nybble to hex:
format %x [bin2int 1010]

Gerald W. Lester

unread,
Jul 12, 2007, 1:09:27 AM7/12/07
to

proc convBinToHexNib {binIn mode} {
binary scan [binary format B8 0000$binIn] c1 num
switch -exact -- $mode {
dec {
set result $num
}
hex {
set result [format %x $num]
}
default {
error "Unknown mode '$mode'"
}

}
return $result
}


--
+--------------------------------+---------------------------------------+
| Gerald W. Lester |
|"The man who fights for his ideals is the man who is alive." - Cervantes|
+------------------------------------------------------------------------+

dkush...@yahoo.com

unread,
Jul 12, 2007, 3:26:17 PM7/12/07
to
> format %x [bin2int 1010]- Hide quoted text -
>
> - Show quoted text -

Excellent!! I'm already using it. Thanks guys.
- Daniel

Kaitzschu

unread,
Jul 13, 2007, 9:55:51 AM7/13/07
to
On Wed, 11 Jul 2007, sleb...@yahoo.com wrote:

> # This works for up to 32 bits.
> # Any more and the value will roll over.
> proc bin2int {binstring} {
> set ret 0
> foreach bit [split $binstring ""] {
> set ret [expr {$ret << 1}]
> if {[string is boolean $bit]} {

# this is a one funky thing!


> set ret [expr {$ret | $bit}]
> } else {
> error "string is not binary!"
> }
> }
> return $ret
> }

Unfortunately, that funky thing doesn't really matter :)
% bin2int 10ftny
can't use non-numeric string as operand of "|"
Booleans are more than just zeros and ones.

--
-Kaitzschu
s="TCL ";while true;do echo -en "\r$s";s=${s:1:${#s}}${s:0:1};sleep .1;done

sleb...@gmail.com

unread,
Jul 14, 2007, 10:45:03 AM7/14/07
to
On Jul 13, 9:55 pm, Kaitzschu
<kaitzs...@kaitzschu.cjb.net.nospam.plz.invalid> wrote:

> On Wed, 11 Jul 2007, slebet...@yahoo.com wrote:
> > # This works for up to 32 bits.
> > # Any more and the value will roll over.
> > proc bin2int {binstring} {
> > set ret 0
> > foreach bit [split $binstring ""] {
> > set ret [expr {$ret << 1}]
> > if {[string is boolean $bit]} {
>
> # this is a one funky thing!
>
> > set ret [expr {$ret | $bit}]
> > } else {
> > error "string is not binary!"
> > }
> > }
> > return $ret
> > }
>
> Unfortunately, that funky thing doesn't really matter :)

I think you mean "fortunately" here ;-)

> % bin2int 10ftny
> can't use non-numeric string as operand of "|"
> Booleans are more than just zeros and ones.
>

In any case, passing a non binary string will generate an error. But
you prefer a consistent error message (not that the error thrown by
expr is not helpful) you can always change the check to the more
straightforward:

if {$bit == 0 || $bit == 1} {...

0 new messages