Using uchar for uint8?

This is a discussion on Using uchar for uint8? within the C Programming forums, part of the General Programming Boards category; Hello, Is it correct to use uchar for uint8 values as they are both unsigned, and 8 bit? Instead of ...

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

    Using uchar for uint8?

    Hello,

    Is it correct to use uchar for uint8 values as they are both unsigned, and 8 bit?

    Instead of getting 2005 I'm getting 8224. Thats using &u. I don't think any byte ordering is required.

    I'm also using:
    ushort for uint16
    UINT32 for uint32
    UCHAR for dstring < works

    I'm programming scsi commands to return data from a dvd disc. Sector 16 ISO PVD.
    UDF 1.02 Spec:
    Code:
    struct timestamp { /* ISO 13346 1/7.3 */
    Uint16 TypeAndTimezone;
    Uint16 Year;
    Uint8 Month;
    Uint8 Day;
    Uint8 Hour;
    Uint8 Minute;
    Uint8 Second;
    Uint8 Centiseconds;
    Uint8 HundredsofMicroseconds;
    Uint8 Microseconds;
    }
    C code:
    Code:
        typedef struct _timestamp { /* ECMA 167 1/7.3 */
        USHORT TypeAndTimezone;
        USHORT Year;
        UCHAR Month;
        UCHAR Day;
        UCHAR Hour;
        UCHAR Minute;
        UCHAR Second;
        UCHAR Centiseconds;
        UCHAR HundredsofMicroseconds;
        UCHAR Microseconds;
        }
    Returned:
    Type and timezone: 8224
    Year: 8224
    Month:32
    Day: 32
    Hour:32
    Min:32
    Sec:32 etc

    Thanks for any help!
    Last edited by Witchfinder; 04-20-2009 at 12:37 PM.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,262
    In this case byte ordering can't possibly matter. 8224 is 0x2020, so the 2 bytes are the same. If you change the types back to what Microsoft has defined, does the function work as expected?
    [edit]I am assuming this is Windows based code. This may not be the case though. [/edit]

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    25
    I don't think there is an "uint16" data type. Windows Data Types (Windows)

    I ecma spec says:
    16-bit unsigned numerical values
    A Uint16 value, represented by the hexadecimal representation #wxyz, shall be recorded in a two-byte field as
    #yz #wx.
    NOTE
    For example, the decimal number 4 660 has #1234 as its hexadecimal representation and shall be recorded as
    #34 #12.

    MSDN:
    SHORT: A 16-bit integer. The range is 32768 through 32767 decimal.
    USHORT: Unsigned SHORT. The range is 0 through 65535 decimal.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Location
    South Africa
    Posts
    20
    can you show your code that assigns to the time struct and read back from it?

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,272
    Although a char is not guaranteed to be 8 bits, any platform where it isn't is so different from the usual that almost all of your code would have to change anyway.

    I'd just specify in the requirements that a supported platform must have 8 bit characters, and use unsigned char as the type for "byte"
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User
    Join Date
    May 2008
    Posts
    25
    Forgot to say its just for windows xp. It seems to return data correctly otherwise text like "Jaws" or "Deluxe Digital Studio" wouldn't display. If I change one of the data types from its correct big length to something else the program crashes, thus I think all the field lengths are correct. Only thing I can thing of is the data types making it incorrect.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,267
    I suppose that if you wanted to make sure that users pay attention to the requirements, you could assert(CHAR_BIT == 8 && "Library requires 8 bit bytes") in the appropriate places.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Sep 2007
    Location
    South Africa
    Posts
    20
    It looks like the timestamp struct is un-initialized or has been written to incorrectly. It just seems to be a block of 0x20202020202020

    try initializing the values to some other pattern, then pass to the device IOCTL and see if the values are modified.

    Also, I saw your code pasted here for a short while then it disappeared?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. UCHAR array question
    By Witchfinder in forum C Programming
    Replies: 7
    Last Post: 04-27-2009, 08:13 PM
  2. Finding whether UCHAR is empty?
    By Witchfinder in forum C Programming
    Replies: 4
    Last Post: 04-25-2009, 11:19 AM
  3. SCSI read disc structure C++ help
    By Witchfinder in forum C++ Programming
    Replies: 0
    Last Post: 03-25-2009, 05:24 PM
  4. What does this do (Windows API)?
    By EVOEx in forum Windows Programming
    Replies: 4
    Last Post: 12-19-2008, 10:48 AM
  5. UCHAR conversion to char []
    By WaterNut in forum C++ Programming
    Replies: 8
    Last Post: 09-21-2005, 10:03 PM

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