how to convert bit vectors to hex

H

hongyan

I have a ascii file has the format: each row has 32 bits.
00001001000101000000000000000000
00001001000010101000000000100000
10100001000011000000001000000000
10100010000011000000010000000000
..............
I want to change that to hex numbers and write it to another file as:
09140000
090a8020
a10c0200
a20c0400
..............
I tried pack and unpack but I can't get it.
Can you tell me a way to do it without installing the bit::vector
package? I am a novice to perl, thank you very much for your help,
Hongyan
 
J

jl_post

hongyan said:
I have a ascii file has the format: each row has 32 bits.
00001001000101000000000000000000
00001001000010101000000000100000
10100001000011000000001000000000
10100010000011000000010000000000
.............
I want to change that to hex numbers and write it to another
file as:
09140000
090a8020
a10c0200
a20c0400
.............
I tried pack and unpack but I can't get it.
Can you tell me a way to do it without installing
the bit::vector package?


Dear Hongyan,

This should be easy to do. Say that your inputfile is named
"in.txt" and you want your output file to be named "out.txt". You can
do this in one line at the DOS/Unix prompt:

# In Unix:
perl -lne 'chomp; print unpack "H*", pack "B*", $_' in.txt > out.txt

# In DOS/Win32:
perl -lne "chomp; print unpack 'H*', pack 'B*', $_" in.txt > out.txt

In both cases, we are packing the 0/1 bit string into a binary string,
then unpacking it into a hex string and printing its value.

I hope this helps you, Hongyan.

-- Jean-Luc
 
J

jl_post

Whoops! I forgot that using the "-l" switch makes the call to chomp()
unnecessary. Therefore, you can do the same thing with:

# In Unix:
perl -lne 'print unpack "H*", pack "B*", $_' in.txt > out.txt

# In DOS/Win32:
perl -lne "print unpack 'H*', pack 'B*', $_" in.txt > out.txt

Sorry about that.

-- Jean-Luc
 
J

Jeff Stampes

hongyan said:
I have a ascii file has the format: each row has 32 bits.
00001001000101000000000000000000
00001001000010101000000000100000
10100001000011000000001000000000
10100010000011000000010000000000
.............
I want to change that to hex numbers and write it to another file as:
09140000
090a8020
a10c0200
a20c0400
.............
I tried pack and unpack but I can't get it.

Please show us what you tried, and what didn't work.
Can you tell me a way to do it without installing the bit::vector
package? I am a novice to perl, thank you very much for your help,

Forgive the cynicism, but since you're posting from a .edu address, and
you're putting restrictions on such as "Don't use the bit::vector
module", I suspect you're looking for help on your homework.

I see Jean already answered your question, but in the future, please
show us your code that isn't working, so we can help you, instead of
coding for you.

~Jeff
 
H

hongyan

Jeff:

Thank you for you remind. This is my first time to post messages here
and it is my fault that I didn't put my own code here. Next time I
will put them.
Actually I am writing an simple assembler with perl for my program
which are going to run on my own designed processor in VHDL.
Again, thank you very much for your words.
 
J

Jeff Stampes

hongyan said:
Actually I am writing an simple assembler with perl for my program
which are going to run on my own designed processor in VHDL.

As you can tell from my email address, the people I work around know
something about VHDL and processors :)
Again, thank you very much for your words.

You're very welcome. As it was your first post, I'll point you at the
Posting Guidelines that are routinelty posted here in case you missed them:

http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.text

I'm glad Jean was able to help you with your problem. Best wishes for
you and your future coding efforts!

~Jeff
 
J

John W. Krahn

hongyan said:
I have a ascii file has the format: each row has 32 bits.
00001001000101000000000000000000
00001001000010101000000000100000
10100001000011000000001000000000
10100010000011000000010000000000
.............
I want to change that to hex numbers and write it to another file as:
09140000
090a8020
a10c0200
a20c0400
.............
I tried pack and unpack but I can't get it.
Can you tell me a way to do it without installing the bit::vector
package? I am a novice to perl, thank you very much for your help,

You can do it without pack() and unpack()


perl -lpe'$_ = sprintf "%08x", oct "0b$_"'




John
 
G

GreenLeaf

Jeff said:
As you can tell from my email address, the people I work around know
something about VHDL and processors :)

Now THAT was a funny coincidence :p
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top