split a string and convert to binary


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting split a string and convert to binary
# 1  
Old 09-08-2011
split a string and convert to binary

Hi All,
Iam new to unix scripting and I want a split a string into 4 characters each, leaving the last two characters and convert the splitted values into binary.
For example:
string='ffd80012ffe20000ffebfffeffea0007fff0ffd70014fff1fff0fff0fff201'
this should split as
ffd8
0012
ffe2
.
.
.
fff2
leaving 01 and each value should be converted to binary
Please provide a shell script for this.
Thanks inadvance
# 2  
Old 09-08-2011
Assuming the values in string are hexanumbers ..
Code:
$ cat filename
#!/bin/ksh
string='ffd80012ffe20000ffebfffeffea0007fff0ffd70014fff1fff0fff0fff201'
echo $string | fold -w 4 | sed '$d' > hexanumbers.txt
for i in `cat hexanumbers.txt | awk '{ print "16#"$0}'`
do
typeset -i10 i
echo $i >> decimals_list.txt
done
for i in `cat decimals_list.txt`
do
        echo "obase=2;$i" | bc
done
rm decimals_list.txt
$ ksh filename
1111111111011000
10010
1111111111100010
0
1111111111101011
.......
.......

---------- Post updated at 01:26 PM ---------- Previous update was at 01:25 PM ----------

https://www.unix.com/shell-programmin...t-command.html
# 3  
Old 09-08-2011
Quote:
Originally Posted by jayan_jay
Assuming the values in string are hexanumbers ..
Code:
$ cat filename
#!/bin/ksh
string='ffd80012ffe20000ffebfffeffea0007fff0ffd70014fff1fff0fff0fff201'
echo $string | fold -w 4 | sed '$d' > hexanumbers.txt
for i in `cat hexanumbers.txt | awk '{ print "16#"$0}'`
do
typeset -i10 i
echo $i >> decimals_list.txt
done
for i in `cat decimals_list.txt`
do
        echo "obase=2;$i" | bc
done
rm decimals_list.txt
$ ksh filename
1111111111011000
10010
1111111111100010
0
1111111111101011
.......
.......

A simpler, more efficient solution utilizing the same basic utilities:
Code:
echo $string | fold -w4 | sed '$d; y/abcdef/ABCDEF/; 1s/^/obase=2; ibase=16; /' | bc

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 4  
Old 09-08-2011
thanx alister and jayan for the solution.
# 5  
Old 09-09-2011
Here is a nice little solution just using the bash shell:

Code:
a=( 0000 0001 0010 0011 0100 0101 0110 0111
    1000 1001 1010 1011 1100 1101 1110 1111 )
 
string='ffd80012ffe20000ffebfffeffea0007fff0ffd70014fff1fff0fff0fff201'
 
for((i=0; $i <${#string}; i++))
do
    printf ${a[16#${string:i:1}]}
    [ $((i%4)) -eq 3 ] && echo
done
echo


Output:
Code:
1111111111011000
0000000000010010
1111111111100010
0000000000000000
1111111111101011
1111111111111110
1111111111101010
0000000000000111
1111111111110000
1111111111010111
0000000000010100
1111111111110001
1111111111110000
1111111111110000
1111111111110010
00000001

# 6  
Old 09-09-2011
And here's one using Perl:

Code:
$

$ echo $str
ffd80012ffe20000ffebfffeffea0007fff0ffd70014fff1fff0fff0fff201
$
$
$ echo $str | perl -lne 's/..$//; print for map {unpack("B16",pack("H4",$_))} unpack("(A4)*",$_)'
1111111111011000
0000000000010010
1111111111100010
0000000000000000
1111111111101011
1111111111111110
1111111111101010
0000000000000111
1111111111110000
1111111111010111
0000000000010100
1111111111110001
1111111111110000
1111111111110000
1111111111110010
$
$

tyler_durden

Last edited by durden_tyler; 09-09-2011 at 08:29 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Convert files from binary to ASCII

I have a huge files in binary format thanks to help me in finding a way to convert these files from Binary format to ASCII format. (0 Replies)
Discussion started by: PRINCESS_RORO
0 Replies

2. Shell Programming and Scripting

Convert binary file to csv and then back to the binary format

Hello *nix specialists, Im working for a non profit organisation in Germany to transport DSL over WLAN to people in areas without no DSL. We are using Linksys WRT 54 router with DD-WRT firmware There are at the moment over 180 router running but we have to change some settings next time. So my... (7 Replies)
Discussion started by: digidax
7 Replies

3. AIX

Convert clear text into binary

Hi I need to display the binary text of an clearn text which is sent as input to the shell script (c shell); ex: I will pass "HELLO" as input, and i should get the binary format of the text Thanks in advance Mohan (1 Reply)
Discussion started by: mKarri
1 Replies

4. UNIX for Dummies Questions & Answers

convert a .tr file to binary in cygwin

I would like to convert a .tr file to a binary file to use for trace purposes. Can anyone provide any insight? (0 Replies)
Discussion started by: sparklezilla3
0 Replies

5. Shell Programming and Scripting

Split binary file with pattern

Hello! Have some problem with extract files from saved session. File contains any kind of special/printable characters. DATA NumberA DATA DATA Begin DATA1.1 DATA1.2 NumberB1 DATA1.3 DATA1.4 End DATA DATA DATA Begin DATA2.1 DATA2.2 NumberB2 DATA2.3 DATA2.4 End DATA DATA ... (4 Replies)
Discussion started by: vvild
4 Replies

6. Shell Programming and Scripting

dynamically convert last binary byte

Hello! Everytime when a specific binaryfile grows (in 4 byte steps) I want to get the newest 2 bytes immediately when they occur. That can be done by tail -f binfile but I need the bytes in hex format. Therefore I use hexdump tail -f binfile | hexdump -xwhat only prints out complete lines of... (6 Replies)
Discussion started by: maloe
6 Replies

7. Shell Programming and Scripting

Split a binary file into 2 basing on 2 delemiter string

Hi all, I have a binary file (orig.dat) and two special delimiter strings 'AAA' and 'BBB'. My binary file's content is as follow: <Data1.1>AAA<Data1.2>BBB <Data2.1>AAA<Data2.2>BBB ... <DataN.1>AAA<DataN.2>BBB DataX.Y might have any length, and contains any kind of special/printable... (1 Reply)
Discussion started by: Averell
1 Replies

8. Programming

trying to convert binary data to text using C++

i hav tried to convert binary 2D data into text using binreader and writing into text file using streamwriter. i use ReadSingle() function to convert from binary to ascii, although it works good in 1D data but not in more dimensions. the kind of values i get are -1.265369923E+038 and like ... (2 Replies)
Discussion started by: geet
2 Replies

9. UNIX for Advanced & Expert Users

Need to convert Binary files to ascii

Dear Experts I need to read a binary file. I know for example in byte number 3801-3804 there is a 4 byte number embeded. Is there a way to extract this number from this file and then convert it to ascii via unix?? Your help would be highly appreciated. Very Best Regards Reza (5 Replies)
Discussion started by: Reza Nazarian
5 Replies

10. UNIX for Advanced & Expert Users

Convert ASCII to BINARY

Here is what I did . . . . I FTP'd several *.pdf files from a web site to a UNIX server, and did not set the transfer mode to BIN, now Adobe thinks that the documents are corrupted. Is there a way to convert the *.pdf files to Binary so that Adobe can open them again. I would just re-download... (2 Replies)
Discussion started by: pc9456
2 Replies
Login or Register to Ask a Question