Thread: BYTE, char data corruption

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    206

    BYTE, char data corruption

    -- If you don't know what a byte is, don't bother reading. --

    My program reads/writes the bytes of a program. I specify the bytes to be written [e.g. 0x01 0x02 0x03 0x04] and it writes them to the address.

    The problem I'm having is: They must be formatted as char when calling my write function, they can't be BYTEs. My bytes get corrupted, even if I initially declare them char.

    I'll have a byte, 0x8E. The char will make it 0xFFFFFFFFFFFFFF8E.

    I've tried to Variable = Variable%256; it, but it doesn't change the outcome. 0xFFFFFFFFFFFFFF8E is -24 as an integer, which isn't a valid char. This makes my function fail.

    I need a way to either preserve the byte, or rid the char of the additional Fs.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    0xFFFFFFFFFFFFFF8E is an integer, not a char or a BYTE (unsigned char). you need to post some code. How are you writing them to the file?

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    -- If you don't know what a byte is, don't bother reading. --
    Nice way to get people to help you.

    The problem is really simple.

    Code:
    int i = 0xFFFFFFFFFFFFFF8E;
    char *byte = (char*)&i;
    for(int j=0; j < 4; j++)
      cout<<byte[j];

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >-- If you don't know what a byte is, don't bother reading. --
    I'm willing to bet that we know more about what a byte is than you do.
    My best code is written with the delete key.

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I prefer nibbles myself

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    nybbles, I think.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I've seen it spelt both ways so /shrug

  8. #8
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Quote Originally Posted by Thantos
    Code:
    int i = 0xFFFFFFFFFFFFFF8E;
    char *byte = (char*)&i;
    for(int j=0; j < 4; j++)
      cout<<byte[j];
    Isn't 0xFFFFFFFFFFFFFF8E a 64-bit value?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  9. #9
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    >> >-- If you don't know what a byte is, don't bother reading. --
    >> I'm willing to bet that we know more about what a byte is than you do.

    Not everyone here on this board knows what a byte is.

  10. #10
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    0xFFFFFFFFFFFFFF8E is indeed a 64 bit value.
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  11. #11
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by JaWiB
    Isn't 0xFFFFFFFFFFFFFF8E a 64-bit value?
    *butt cover* yeah but I was supposing a system with a 16bit BYTE....

    and besides its easily fixed:
    Code:
    for(int j=0; j < sizeof i; j++)

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Not everyone here on this board knows what a byte is.
    That much is obvious, judging from the frequency of questions about it and incorrect responses.
    My best code is written with the delete key.

  13. #13
    Registered User
    Join Date
    Dec 2001
    Posts
    206
    I added that comment because last time I spoke of a byte here, the only responses I got were "What's a byte"?

  14. #14
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    That's because a BYTE is not a native type. And it does not necessarily mean an octet. So without adding context, it's marginally as informative as FOO.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  15. #15
    Registered User
    Join Date
    Dec 2001
    Posts
    206
    Anyway, I could bypass this problem if I could get my struct to accept a byte array instead of a char array.

    Code:
    struct input
    {
    	UINT_PTR processid;
    	void *address;
    	unsigned short int bytestowrite; 
    	BYTE bytes[300];
    };
    Code:
    input Line = { GetCurrentProcessId(), Address, Length, _Buffer };
    Buffer is a BYTE[300].

    error C2440: 'initializing' : cannot convert from 'unsigned char [300]' to 'unsigned char'

    Changing both types to char[300] is accepted, but "corrupts" it.

    This gets sent through DeviceIoControl, so I can't declare them pointers, I need the actual value.
    Last edited by Denethor2000; 11-12-2005 at 05:23 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Need help fixing bugs in data parsing program
    By daluu in forum C Programming
    Replies: 8
    Last Post: 03-27-2003, 06:02 PM
  4. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM