Bash - binary data to ascii code


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash - binary data to ascii code
# 1  
Old 10-01-2014
Bash - binary data to ascii code

Hello,

With bash-script (ubunto server) I'm trying to read a binary file and, for each character, give back its ascii code (including extended ascii). For example:

HEX => ASCII => PRINT
f5 => 245 => õ
50 => 80 => P

To load the binary file into a variable I tried in this way:
Code:
s=$(<"binaryfile.bin")

and for finding its length:
Code:
n=${#s}

However, $s does not seem to correspond to the actual content of the file and therefore also the length.

The exact number of characters I get in fact doing:
Code:
n=$(stat c%s "binaryfile.bin")

What I do not get is $s to match character for character the content of BinaryFile.

The equivalent vb6 works this way:

Code:
Open DataFile For Binary As #1 
s = Input (LOF (1), 1) 
n = len(s)
Close 1 

For i = 1 To n
sn(i) = Asc(Mid(s, i, 1))
Next

I also tried to work with hexdump ...
Code:
hexdump -C binaryfile.bin

but I can not print the ascii code (extended) characters in the file.

If the variable $s contain the exact text of the file, I could use the following functions to get each character its ascii code (which is what I need).

Code:
<cut>
 for(( i=0; i<=n; i++))
 do
    one_char=${s:i:1}
    sn[$i]=$(ord "$one_char");
 done
<cut>

#Asc() function
ord() {
  LC_CTYPE=C printf '%d' "'$1"
}

#Chr() function
chr() {
  [ "${1}" -lt 256 ] || return 1
  printf \\$(printf '%03o' $1)
}

Any help?

Thank you
math
# 2  
Old 10-01-2014
This request is locale dependent, e.g. in UTF-8 the "ö" char is not 245, but the 16 bit representation 195 182. So watch out!

One solution to your problem might be the use of od:
Code:
cat file
Padventure
öadventures
PadvÖüersary
od -An -tu1 file
  80  97 100 118 101 110 116 117 114 101  10 195 182  97 100 118
 101 110 116 117 114 101 115  10  80  97 100 118 195 150 195 188
 101 114 115  97 114 121  10

This User Gave Thanks to RudiC For This Post:
# 3  
Old 10-01-2014
Smilie There have been over TWO days (h24) to no avail.

I also saw the OD command but I was lost.
I am new to UNIX and I am converting some domestic services that I had done in windows.

It's perfect, thank you RudiC.
Math
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Creating data delimited by ASCII code 1

<Any suggestion how to create a file where the values are separated by ASCII code 1,with data extracted from a table using shell script The format is :/> <columnname1(binary1)columnvalue(binary1)columnname2(binary1)columnvalue(binary1)columnname3(binary1)columnvalue... 1st row/>... (6 Replies)
Discussion started by: dasun
6 Replies

2. Solaris

ASN Binary to ASCII

Dears, I need help to convert the binary file into ASCII format. Actually we have CDRs which is generated by telecom switch at this is in ASN1 format or binary format, I need to convert those binary formatted file into ASCII format using Perl, or shell scripting. Is there any way to solve... (3 Replies)
Discussion started by: PRINCESS_RORO
3 Replies

3. UNIX for Advanced & Expert Users

Conversion from ASCII to binary for physical simulation code in C/C++

Good evening, everybody A good math friend told me that it would be possible to shrink the size of the numerical datas I produce with a physical simulation code I programmed for my PhD. It usually writes at least 100 GB to complete the simulation, and it seems that it is too high. There are... (7 Replies)
Discussion started by: Cybertib
7 Replies

4. Shell Programming and Scripting

binary to ascii conversion

Hi, I have got a library file, created by compiling C code. The file information with "file" command, gives it a "application/x-archive" type file. I want to extract the release string of my software from this file, so that i can know which version of C files were used to create the lib. Can... (3 Replies)
Discussion started by: atulmt
3 Replies

5. UNIX for Dummies Questions & Answers

Ascii or Binary?

Hello all, I am working with ftp servers in unix, and always I have to get and put files but I don't know exactly if I have to get or put them as an ascii or binary. Some files that I use are: .txt, .sav, .fmb, .pct, .sh, .ksh, .dat, .log. Somebody can tell me what is the difference between... (2 Replies)
Discussion started by: Geller
2 Replies

6. Shell Programming and Scripting

binary to ascii

Hi, Is there a way to convert the binary file to ascii . the binary file is pipe delimited. from source the file(pipe delimited) is ftped to mainframe and from mainframe it is ftped to the unix box using binary format. Is there a way to change it back to ascii and view it? Thanks! (3 Replies)
Discussion started by: dnat
3 Replies

7. Shell Programming and Scripting

how to check the file data type(ascii or binary)

hi i am receiving a file from one system , i have to verify the format of the file data i.e whether the data is in acii format or binary format, please help thanks in advance satya (1 Reply)
Discussion started by: Satyak
1 Replies

8. Shell Programming and Scripting

Binary or ascii file

I want to verify the file is Binary or ascii file and accordingly I want to switch the program with ret code ie 0 or success and 1 for failure Can any one help me is this a correct syntex...i am getting error #!/bin/ksh $file filename if echo "ascii fie Found" else echo " binary... (6 Replies)
Discussion started by: u263066
6 Replies

9. UNIX for Advanced & Expert Users

Convert Binary data to ascii data

Friends, I've tried on solaris, but I could n't get ascii data dd if=binaryinputfile bs=1 skip=3800 count=4 | od -t u4 output : INDBU3:/usr/users/FTAMUSER/kk $ dd if=SMP20041006173649188151 bs=1 skip=3800 count=4 | od -t u4 4+0 records in 4+0 records out 0000000 0000000000 0000004... (4 Replies)
Discussion started by: krishna
4 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