Writing hex to a file

This is a discussion on Writing hex to a file within the C++ Programming forums, part of the General Programming Boards category; Hello everyone, I'm in need of your help. I've always thought that files were made up of bytes which range ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    22

    Writing hex to a file

    Hello everyone, I'm in need of your help. I've always thought that files were made up of bytes which range from 0 to 255 in decimal value. But when reading the values of some mp3's and other audio files I found myself sometimes getting negative numbers like -29 or hex that start off 'FFFFFF' as in FFFFFF0A. I want to be able to edit files with such values on that level but I have no knowledge of how to write these values into a file. Assuming ** is a hex value the most I know is:

    Code:
     
    myfile << '\x**'
    I can write from 00 to FF in hex but not outside that range. If there is anyone who can help me I would greatly appreciate them showing me how to do it.

    Thanks a lot.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    Use unsigned char as the type, if you want to deal with the file as an array of bytes.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    22
    Thanks Salem. I get why I'd be doing that but I still have no idea how to enter negative values. Like the line of code I put above, what would I have to write?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'd say that you probably don't WANT to read/write binary files using the text version of streams. If you have binary data (e.g. mp3 files), then you should be using the binary form of streams. That means using read() and write() functions, and opening the file with binary form.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    22
    Thanks Mats, I'm pretty sure I do want to though. Do you know how I can do that?

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    9
    printf "%b" "\x$(printf "%x" 10)">a

    ref to::how to handle hex values in bash ? - Usenet Forums

  7. #7
    Registered User
    Join Date
    May 2008
    Posts
    22
    Bash? From Linux? I'm sure there is one for vista but I'd have to work all the way backwards and ATTEMPT to learn how to use it well as opposed to the one line of code I've been asking for from the beginning. I'm pretty much a noob, like a third world child who has just learnt how to read.

    For me, these answers only lead to more questions. I can't help but feel sexually frustrated right now.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by martinuk View Post
    printf "%b" "\x$(printf "%x" 10)">a

    ref to::how to handle hex values in bash ? - Usenet Forums
    Not a very C++ solution using printf, right? And the link refers to bash, the shell, rather than C or C++.

    And if you want to EDIT mp3 files, then you WILL need to read/write the file in binary - otherwise you may end up adding/removing characters that have a meaning in the file. E.g. if you happen to have a newline/carriage return (10 or 13) or CTRL-Z (26) in the file, it will most likely not work to read the file in Windows.

    As Salem said, you get values 0..255 if you use unsigned char instead of just char - char is in many compilers treated as a signed value, -128..127, and when you use it as a integer value, it will show as 0xFFFFFFxx when expanded to 32 bits.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    May 2008
    Posts
    22
    Thanks Mats

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. Trouble writing to file using fwrite()
    By yougene in forum C Programming
    Replies: 4
    Last Post: 12-30-2008, 04:13 PM
  3. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  4. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  5. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM

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