write to hex, is it possible?

This is a discussion on write to hex, is it possible? within the C++ Programming forums, part of the General Programming Boards category; Please let me know if this is possible or even remotely possible! I need to write to a file, but ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    27

    write to hex, is it possible?

    Please let me know if this is possible or even remotely possible! I need to write to a file, but directly to hex. For example, if I write "2D" to the file, i want to be able to open the file in a hex editor and see "2D" in the hex portion, not the ASCII portion. the only way i have been able to get "2D" into the hex portion is by writing "-" to the file.

    i have been told this is possible, but so far have come up empty-handed. any ideas, hints, code, or suggestions would be greatly appreciated! Thanks!

    keith

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    std::fstream;
    //do initialization
    fout << std::hex << num;

    or the c way.
    fprintf(file, "%x", num);
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    wait, I misunderstood. I thought youwanted ASCII. you want binary.


    0x2D is 45 decimal.


    std::ofstream fout;
    unsigned char blah = 0x2d;
    fout.open("c:\\blah.txt", std::ios::binary); // I think this is right
    fout.write(&blah, 1);
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    27

    great!

    awesome, that worked! thanks much.

    followup question, though - this just converts a char. what if i had, say something that looked like this that needed converting:

    100000000100020080BB000000EE0200

    is there a more convenient way than just doing it character by character?

    please let me know, and thanks so much for the help!

    keith

  5. #5
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    fout.write(&blah, 1);

    The (1) in that expression is the number of bytes. so if you have a buffer:

    char buf[100];

    you can write the whole thing out:

    fout.write(buf,100);
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    27
    ok, this isn't working for me. i get an error when i compile. perhaps you could look at my code and see what i am doing wrong (something stupid, probably). here is a copy of the error for the following code:
    --------------------------------------------------------------------------------
    error C2664: 'write' : cannot convert parameter 1 from 'char (*)[100]' to 'const char *'
    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    --------------------------------------------------------------------------------
    and here is the code:


    main() {
    char blah[100];
    string userfilename;
    string useroutput;
    cout <<"type in output filename." <<endl;
    cin >> useroutput;

    std::ofstream fout;
    blah[100] = 0x2D;
    fout.open(useroutput.c_str(), std::ios::binary);
    fout.write(&blah,100);

    return 0;

    }

  7. #7
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    fout.write(&blah,100); //you don't want the address of the array pointer. You want the array pointer itself.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  8. #8
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    Code:
    main() {
       char blah[100];
       string userfilename;
       string useroutput;
       cout <<"type in output filename." <<endl;
       cin >> useroutput;
    
       std::ofstream fout;
       blah[100] = 0x2D;
       fout.open(useroutput.c_str(), std::ios::binary);
       fout.write(blah,100);
       return 0;
    }
    Remove the & from the write function. blah is already a pointer. Actually is a const pointer.

  9. #9
    Registered User
    Join Date
    Jul 2003
    Posts
    27
    ok. ALMOST there!

    when i compile this, i get gibberish for everything except for a 2D wherever i put 0x2D in the array. this is correct, but if i put anything longer than 2D, like

    0x2D003EE0

    it gives me an error...understandable. is there anyway to stick 0x2D003EE0---------- into more than one slot in the array? because by the looks of it, right now i still have to write out each char, i.e. blah[0] = , blah[1] = , and so on and so forth....

    any ideas? thanks so much.

  10. #10
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    well you're probably setting only one byte the way you're doing it. If you go:

    buf[0] = 0x2D003EE0;

    You're trying to set a char equal to a long. not enough room obviously.

    One way would be this:

    *(long*)buf = 0x2D003EE0;

    This would set the first four bytes to those values (in reverse order. Intel is lil-endian.)
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  11. #11
    Registered User
    Join Date
    Jul 2003
    Posts
    27
    Most excellent!

    Thanks SO much for all your help, this worked, and i think that's it for now.



    keith

  12. #12
    Registered User
    Join Date
    Jul 2003
    Posts
    27

    followup

    Say I have something like the following:

    int total_after_data = x - 44;

    is there anyway to make total_after_data convertible to hex, and printable like the following?

    *(long*)ib = 0x46464952;
    fout.write(ib,4);

    where "total_after_data_converted" would replace '46464952'?


    thanks,
    keith.

    p.s. - if this is too ambiguous i can describe in fuller detail. thanks.

  13. #13
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    "convertable to hex" isn't exactly the right way of looking at it. A hex editor just shows you a hexadecimal representation of binary data. When you store a number, its always in binary (well can be represented in ASCII which is really just a binary code) In other words if you were to store the number 1 in your file.

    int i=1;
    fout.write(&i,4);

    It is written out as:

    00000001 00000000 00000000 00000000

    the way you see it in a hex editor is:

    0100 0000

    but thats only because its easier on the eyes and hex editors are nice like that. Its not because its stored differently. So "stored as hex" isn't right. "stored as binary" is correct though.

    anyway, your question.

    int total_after_data = x - 4;
    fout.write(&total_after_data, 4);

    would work because an int is 4 bytes long.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Signed hex string to a int
    By naruto267 in forum C Programming
    Replies: 5
    Last Post: 05-19-2008, 12:43 AM
  2. read write lock in C#
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 04-16-2008, 09:49 AM
  3. Replies: 18
    Last Post: 03-26-2008, 10:01 AM
  4. Looking to extract a hex value out of a buffer
    By MMC in forum C Programming
    Replies: 9
    Last Post: 04-12-2002, 02:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21