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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to convert hex to bin? 1

Status
Not open for further replies.

egal

Programmer
Jun 5, 2001
4
0
0
DE
Hi there!

Is there a good procedure, where i can convert a hex number to a binary number? e.g. i want something like "convert FF" and the result should be "11111111".
Can anybody help me with a small bit of code? I have no clue other than to use many switch cases...

Thnx in advance
 
ok, i have done it on my own :)
if anyone wants to know...:
Code:
set hex 8C
set is_hex [regexp {^([0-9A-F]+)$} $hex]
set space " "
set nspace ""
if {$is_hex == 1} {
    set length_of_string [string length $hex]
    for {set i 0} {$i < $length_of_string} {incr i} {
        set number [string index $hex $i]
        switch $number {
            0 {set byte 0000 }
            1 {set byte 0001 }
            2 {set byte 0010 }
            3 {set byte 0011 }
            4 {set byte 0100 }
            5 {set byte 0101 }
            6 {set byte 0110 }
            7 {set byte 0111 }
            8 {set byte 1000 }
            9 {set byte 1001 }
            A {set byte 1010 }
            B {set byte 1011 }
            C {set byte 1100 }
            D {set byte 1101 }
            E {set byte 1110 }
            F {set byte 1111 }
        }
        lappend bin_list $byte
    }
    set bin [join $bin_list]
    regsub -all $space $bin $nspace bin 
} else {
    puts &quot;Please enter a hex number with uppercase characters&quot;
}
if anyone knows a better way, please feel free to post it
 
Also you could check outthe binary command,
binary format, binary scan...Your code looks good
though.
 
To follow up on what marsd suggested, here's a procedure built on the binary format and binary scan commands:

Code:
proc hex2bin {hex} {
  binary scan [binary format H* $hex] B* bin
  return $bin
}

Examples:

[tt]% hex2bin 3c
00111100
% hex2bin 0f3E8a
000011110011111010001010[/tt]

By the way, egal, if you're using Tcl version 8.1.1 or later, the string map command is a much simpler way to map from one character sequence to another in a string (similar to the Unix tr utility). The first argument to string map is a list mapping input character sequences to output character sequences, the second argument is the string to process, and return value is the mapped string. So, you could have written your procedure as:

Code:
set hex2bin_map {
  0  0000
  1  0001
  2  0010
  3  0011
  4  0100
  5  0101
  6  0110
  7  0111
  8  1000
  9  1001
  a  1010
  b  1011
  c  1100
  d  1101
  e  1110
  f  1111
}

proc hex2bin {hex} {
  global hex2bin_map
  if {![string is xdigit $hex]} {
    error &quot;value \&quot;$hex\&quot; is not a hex integer&quot;
  }
  return [string map -nocase $hex2bin_map $hex]
}
- Ken Jones, President, ken@avia-training.com
Avia Training and Consulting, 866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
That's an excellent example avia.
Thanks.
 
hi
this was my first script i made with tcl(only one part)

and it works ...
proc hex_to_bin {hex} {

set hex_to_bin(0) 0000
set hex_to_bin(1) 0001
set hex_to_bin(2) 0010
set hex_to_bin(3) 0011
set hex_to_bin(4) 0100
set hex_to_bin(5) 0101
set hex_to_bin(6) 0110
set hex_to_bin(7) 0111
set hex_to_bin(8) 1000
set hex_to_bin(9) 1001
set hex_to_bin(a) 1010
set hex_to_bin(b) 1011
set hex_to_bin(c) 1100
set hex_to_bin(d) 1101
set hex_to_bin(e) 1110
set hex_to_bin(f) 1111

set temp [split $hex &quot;&quot;]
foreach t $temp {
lappend bin $hex_to_bin($t)
}
set bin [join $bin &quot;&quot;]
return $bin
}
 
Thanks guys for your help!
I didn't thougt of the binary command.
Thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top