The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
ascii to binary conversion in sco 5.0.5 sameek1211 SCO 1 12-17-2007 07:49 AM
Convert Binary data to ascii data krishna UNIX for Advanced & Expert Users 4 11-05-2004 04:12 PM
text files, ASCII files, binary files and ftp transfers Perderabo Answers to Frequently Asked Questions 0 04-08-2004 04:25 PM
Convert ASCII to BINARY pc9456 UNIX for Advanced & Expert Users 2 06-24-2002 06:46 AM
viewing binary files in ASCII manjunath UNIX for Advanced & Expert Users 2 04-02-2002 08:32 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 2.00 average. Display Modes
  #1 (permalink)  
Old 10-09-2004
Reza Nazarian Reza Nazarian is offline
Registered User
  
 

Join Date: Jun 2001
Location: Tehran, Iran
Posts: 61
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

Last edited by Reza Nazarian; 10-09-2004 at 08:24 AM..
  #2 (permalink)  
Old 10-11-2004
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
od will jump to an offset with the -j <offset> qualifier.
The -t u4 will tell od to format the output as an unsigned decimal.

You need to check your od man page because I think the -t qualifier is not always supported, and may only support 16 bit words, not 32 bit words.

Otherwise compile and try this
cc -o bfile bfile.c
bfile `head -c -n 3404 mybinaryfile`

Code:
/* bfile.c */
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
     if (*argv[1])
     {
         if(strlen(argv[1])>3403)
         {
                 char  *p=&argv[1][3401];
                 fprintf(stdout,"%d\n",*(int *)p);
         }
      }
      return 0;
}
You are limited in postion in the file to what _POSIX_ARG_MAX is defined to for your system (in limits.h) - Normally it is 4096.

Last edited by jim mcnamara; 10-11-2004 at 11:27 AM..
  #3 (permalink)  
Old 10-11-2004
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
"od -t u4" is fairly common. So try:

dd if=datafile bs=1 skip=3800 count=4 | od -t u4
  #4 (permalink)  
Old 10-11-2004
Reza Nazarian Reza Nazarian is offline
Registered User
  
 

Join Date: Jun 2001
Location: Tehran, Iran
Posts: 61
Thank you veru much.
It helps me a lot.
Very Best regards
Reza
  #5 (permalink)  
Old 10-20-2004
krishna krishna is offline
Registered User
  
 

Join Date: Aug 2001
Posts: 60
Friends,

I've tried on solaris, but I could n't find ascii

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


Pls help me to convert binary to ascii on solaris.

Thanks
Krishna
  #6 (permalink)  
Old 11-05-2004
A452917 A452917 is offline
Registered User
  
 

Join Date: Nov 2004
Location: Mumbai, India
Posts: 14
Quote:
dd if=binaryinputfile bs=1 skip=3800 count=4 | od -t u4
Krishna,

The above command will work only if:

1. In the binary file, there is a 4 byte number embeded in bytes 3801-3804.

And remember, it will just give you the Integer present at that location. It won't convert the binary file into text file.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:10 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0