Unix/Linux Go Back    


Programming Post questions about C, C++, Java, SQL, and other programming languages here.

ascii to hex

Programming


Tags
hexadecimal, od

Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 03-18-2010
Kingbruce Kingbruce is offline
Registered User
 
Join Date: Mar 2010
Last Activity: 18 March 2010, 5:58 PM EDT
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
ascii to hex

Hello guys,

i want to convert a text file to hex and have written this code :


Code:
int main(int argc, char **argv)
{
            ifstream file;
            string fileName = "CODEZ";
     
            file.open(fileName.c_str()); //  oeffen im Text-Modus
            if(file)
            {
                        string text; // Haupttext
                        while(!file.eof())
                        {
                                    char buffer; // Buffer
                                    file.get(buffer);
                                    //text += buffer; // Zeichen zusammensetzen
                                    cout << "%" << hex <<(int)buffer  ;
                        }
                          file.close();
            }
            else
            {
                        cout << "Datei Fehler!";
            }
            cin.get();
            return 0;
}

My problem is to recognise where the line of the original document ends.
The input from text file is :

Code:
hello
world

The output is :

Code:
%68%65%6c%6c%6f%a%77%6f%72%6c%64%a%a

But my desired Output is:

Code:
%68%65%6c%6c%6f%a
%77%6f%72%6c%64%a

The Strings in the text file are of various length,...but in each line is just one word!
Any ideas how to solve this problem?
Sponsored Links
    #2  
Old Unix and Linux 03-18-2010
joeyg's Unix or Linux Image
joeyg joeyg is offline Forum Staff  
modérateur
 
Join Date: Dec 2007
Last Activity: 28 July 2015, 11:10 AM EDT
Location: Within two miles of a Dunkin donuts.
Posts: 2,360
Thanks: 85
Thanked 164 Times in 150 Posts
Tools Have a solution in script

My bad, I solved the problem before looking to see that you did not post this in the general unix area. But, since I went to the trouble...


Code:
cat infile.txt | od -An -t dC | tr -d "\n" | sed 's/ 10 / 10~ /g' | tr "~" "\n"

will take an input file, and provide all the hexadecimal representations of each character, putting output out on separate lines (for separate input lines).
Sponsored Links
    #3  
Old Unix and Linux 03-18-2010
Kingbruce Kingbruce is offline
Registered User
 
Join Date: Mar 2010
Last Activity: 18 March 2010, 5:58 PM EDT
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Bug


Code:
cat infile.txt | od -An -t dC | tr -d "\n" | sed 's/ 10 / 10~ /g' | tr "~" "\n"

will take an input file, and provide all the hexadecimal representations of each character, putting output out on separate lines (for separate input lines).[/QUOTE]

Thank your for your suggestion. But the code you provided generates a output like this :

Code:
104  101  108  108  111   10
119  111  114  108  100   10

But i need the strings in the above mentioned format like :

Code:
%68%65%6c%6c%6f%a
%77%6f%72%6c%64%a

Later on i want to feed a Java-Script with the output.
When i use the unescape() command i want to have the plain text again.
    #4  
Old Unix and Linux 03-18-2010
joeyg's Unix or Linux Image
joeyg joeyg is offline Forum Staff  
modérateur
 
Join Date: Dec 2007
Last Activity: 28 July 2015, 11:10 AM EDT
Location: Within two miles of a Dunkin donuts.
Posts: 2,360
Thanks: 85
Thanked 164 Times in 150 Posts
Hammer & Screwdriver You are right, my mistake

OK, it is ugly, but...

Code:
cat infile | od -An -t x2 | tr " " "\n" | tr -s "\n" | awk '{print "%"substr($1,1,2);print "%"substr($1,3,2)}' | sed 's/0a/0a~/g' | tr -d "\n" | tr "~" "\n"

Sponsored Links
    #5  
Old Unix and Linux 03-18-2010
Franklin52's Unix or Linux Image
Franklin52 Franklin52 is offline Forum Staff  
Moderator
 
Join Date: Feb 2007
Last Activity: 27 July 2015, 1:09 PM EDT
Location: The Netherlands
Posts: 7,731
Thanks: 129
Thanked 547 Times in 515 Posts
What about:


Code:
$ cat file
hello
world
$ od -An -t x1 file| awk '{gsub("0a","0a%\n");gsub(" ","%")}{printf("%s", $0)}'
%68%65%6c%6c%6f%0a%
%77%6f%72%6c%64%0a%

Sponsored Links
    #6  
Old Unix and Linux 03-18-2010
Kingbruce Kingbruce is offline
Registered User
 
Join Date: Mar 2010
Last Activity: 18 March 2010, 5:58 PM EDT
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
thx a lot Franklin, this is exactly what i needed Linux
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
convert ascii values into ascii characters sandeeppvk Shell Programming and Scripting 10 04-23-2009 11:23 PM
URL to ASCII imagiro1 Shell Programming and Scripting 5 06-11-2008 01:45 PM
ascii melkor UNIX for Dummies Questions & Answers 3 12-12-2003 09:33 PM
Ascii To Hex augustinep UNIX for Dummies Questions & Answers 1 04-26-2002 02:03 PM
ascii for EOF ??? strpeski UNIX for Dummies Questions & Answers 3 03-16-2002 08:33 PM



All times are GMT -4. The time now is 12:14 AM.