ASCII v's hex

This is a discussion on ASCII v's hex within the C++ Programming forums, part of the General Programming Boards category; I'm trying to access a binary file that i have created very simple and straight forward Code: GlobalImageBuffer[0]=255; GlobalImageBuffer[1]=25; GlobalImageBuffer[2]='A'; ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    19

    ASCII v's hex

    I'm trying to access a binary file that i have created very simple and straight forward
    Code:
    GlobalImageBuffer[0]=255;
    GlobalImageBuffer[1]=25;
    GlobalImageBuffer[2]='A';
    GlobalImageBuffer[3]='B';
    GlobalImageBuffer[4]='100';
    
    fwrite( GlobalImageBuffer, 1, 200, fp);
    fclose(fp);
    Now when i try and view it in a hex editor i cant view all the characters correctly, It seems that some of the ascii codes share hex codes
    http://www.lookuptables.com/
    ie hex 41 could be the number "65" or the letter "A", could some one explain why i can't view the binary file correctly, what correct tools do i need currently using 010 editor

    cheers
    M

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There is no difference between "ascii" and "hex". It's simply a matter of you displaying it how you want to. All ascii characters have a decimal value. Oddly enough, decimal can be converted (read: displayed in) to hex. It's up to you do pick what way you want to display whatever it is you're displaying.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    ...ie hex 41 could be the number "65" or the letter "A"...
    EXACTLY!


    To expand a bit on what quzah said...

    As you know, your computer's RAM stores everything as binary numbers. That number might just be a number (integer) or, it might represent an ASCII character, it might be a CPU machine language instruction, it might represent the color of a pixel, it might be an unused memory location with "garbage" in it. There is nothing about the number itself to indicate what it's for!

    So, your program, or the operating system, or the BIOS... something... has to keep track of the meaning and purpose of the data at each memory address.

    The same is true for you hard disk, every file is just a sequence of numbers.


    ...could some one explain why i can't view the binary file correctly...
    What do you mean by "correctly"?

    The hex editor doesn't know what the "numbers" represent either. Whenever the hex editor sees any number between 32 and 127 (decimal), it displays the associated character.

    If you've written character-A to a file, your hex editor should display 41 in the hex-field, and 'A' in the ASCII-field. (Every hex editor I've seen displays ASCII values to the right of the hex values.) If you've saved the number-65 (decimal) to a file, the hex editor should again display 41 in the hex-field, and 'A' in the ASCII-field.

    I think the hex editor should keep everything straight, but things can get screwy due to byte-order. You are probably working on a 32 bit machine, with a 16 bit ATA/IDE drive connector, and your hex editor is converting bytes (8-bits) to ASCII.

    BTW - The only reason hex is used is because binary numbers are difficult (for humans) to read, and write (or speak). And, it's easier to convert between binary and hex, than binary and decimal. You can easily learn to do hex-binary conversions in your head.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Char to ASCII Hex Value Troubles
    By MattMik in forum C++ Programming
    Replies: 6
    Last Post: 03-25-2008, 02:17 PM
  2. Ascii to hex and hex to Ascii
    By beon in forum C Programming
    Replies: 1
    Last Post: 12-26-2006, 05:37 AM
  3. Replies: 11
    Last Post: 03-24-2006, 10:26 AM
  4. Encrypting text file with ASCII hex
    By supaben34 in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2005, 05:35 PM
  5. Hex to Ascii conversion
    By Ryno in forum C Programming
    Replies: 2
    Last Post: 03-24-2005, 08:16 AM

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