Thread: Inserting hex characters in the output

  1. #1
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257

    Inserting hex characters in the output

    Hey,

    I want to insert a character 0x0a after a string in my output to file. To look something like this:
    Code:
    one0atwo0athree
    I tried inserting a newline, that's what 0a is, but it write two bites instead of one. I asigned '\n' to an unsigned char and then print it, it doesn't work.

    The source:
    Code:
    #include<iostream>
    #include <cstdio>
    
    using namespace std;
    
    int
    main (void)
    {
    	char str1[] = "one", str2[] = "two";
    
    	char c = 0xa;
    
    	FILE *out;
        out = fopen("h:/c/HexTest.txt", "w"); 
    
    	fprintf(out, "%s%c%s%c", str1, c, str2, c);
    
    	return 0;
    }
    here's the current output, in ASCII:
    Code:
    one
    two
    Hex: 6F 6E 65 0D 0A 74 77 6F 0D 0A.
    I only want the 0A , without 0D.

    Is that doable?

    thanks.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    The problem you are having is because your file was opened in text mode and the character represented as hex 0xA gets converted in text mode into the two characters you are seeing. You need to open the file in binary mode.

    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main (void)
    {
        char str1[] = "one", str2[] = "two";
        char c = 0xA;
        ofstream out("h:/c/HexTest.txt", ios::out|ios::binary); 
    
        if( out.is_open() )
            out << str1 << c << str2 << c;
    
        return 0;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    I missed that. I adjusted my code, but it's still doing the same thing.
    Code:
    int
    main (void)
    {
    	char str1[] = "one", str2[] = "two";
    
    	unsigned char c = '\n';
    
    	FILE *out;
        out = fopen("h:/c/HexTest.bin", "wb");
    	
    	fwrite(&str1,sizeof(char), sizeof(str1), out);
    
    	fwrite(&c,1, 1, out);
    
    	return 0;
    }
    and output:
    Code:
    6F 6E65 00 0A
    . it adds the extra byte of 00.
    Last edited by earth_angel; 06-29-2005 at 09:51 AM.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    fwrite(&str1,sizeof(char), sizeof(str1), out);
    What is sizeof(str1)? Probably 4 which is not what you want. I think you meant strlen(str1) which would be 3. With 4 instead of 3, you would get the null terminating character output to the file which is that 0x00 that you are seeing. And I think sizeof(char) is guaranteed to be 1 so you can just replace sizeof(char) with 1.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > fwrite(&str1,sizeof(char), sizeof(str1), out);
    Use strlen(), not sizeof().
    The sizeof includes the \0 at the end of the array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    That's exactly what it was thanks guys.

    AS.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting hex byte into a data
    By jaqquery in forum C Programming
    Replies: 4
    Last Post: 02-25-2009, 02:18 AM
  2. Having trouble making a function do what I want.
    By Shamino in forum C++ Programming
    Replies: 9
    Last Post: 12-07-2007, 11:20 AM
  3. sorting characters and output to text file
    By odb1 in forum C++ Programming
    Replies: 1
    Last Post: 10-10-2003, 04:46 PM
  4. printing non-ASCII characters (in unicode)
    By dbaryl in forum C Programming
    Replies: 1
    Last Post: 10-25-2002, 01:00 PM
  5. output of characters
    By deleeuw in forum C++ Programming
    Replies: 1
    Last Post: 08-30-2001, 11:17 PM