Thread: Hex to four unsigned bytes

  1. #1
    Stressed Student :(
    Join Date
    Feb 2008
    Location
    Berkeley, CA
    Posts
    73

    Hex to four unsigned bytes

    I don't know what an unsigned byte looks like. I'm assuming since its a byte it is a quarter of a word? A short would be two bytes correct?

    So what is this in four unsigned bytes

    0x811F00FE

    This is what it is in binary:

    1000 0001 0001 1111 0000 0000 1111 1110

    can someone give me any hints?

    Oh and also what is it in four two's complement bytes?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    A byte is eight bits (barring strange machines). Unsigned and two's complement is just a way of interpreting those bytes, it does not change the representation.

  3. #3
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Don't confuse yourself. I think you may be mixing and matching terms and what they mean to the point you don't even understand what you're asking.

    A byte is an 8-bit number. That's it.

    Signed and unsigned numbers have to do with how a number is interpreted. Usually a system is used where if a number is signed, if the most significant bit is set, then the value is interpreted to be a negative value. Otherwise, the number is interpreted to be positive. An unsigned number is a number always taken to be positive regardless of the sign bit.

    Octal, binary, and hexadecimal are all representations of numbers in bases 8, 2, and 16 respectively.

    A number like 3 is 11 in binary, 3 in octal, and 3 in hexadecimal. Is it signed or unsigned? Probably doesn't matter since it's not big enough to even care about. The sign bit will probably be 0 in either case.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Care to take a stab at it?

    A byte is an 8-bit number.
    Though I'd say that's a safe assumption about the assignment, the standard allows for other byte lengths.

    I'd say the answer involves inserting three space characters into the original post.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by iMalc View Post
    I'd say the answer involves inserting three space characters into the original post.
    I would claim the answer involves deleting three space characters from the original post, but I've always had a soft spot for binary.

  6. #6
    Stressed Student :(
    Join Date
    Feb 2008
    Location
    Berkeley, CA
    Posts
    73
    so to convert it to four unsigned bytes, i should subtract 11111111 from 811f00fe ? repeatedly? or take away the " " ?? because if i take a way the spaces i dont think that would be accurate, would it?

    for two's complement, do i have to interpret the hexadecimal as a negative and add the biggest byte possible until it reaches 0? is that right?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by NoobieGecko View Post
    so to convert it to four unsigned bytes, i should subtract 11111111 from 811f00fe ? repeatedly? or take away the " " ?? because if i take a way the spaces i dont think that would be accurate, would it?

    for two's complement, do i have to interpret the hexadecimal as a negative and add the biggest byte possible until it reaches 0? is that right?
    Not in the slightest. You have 0x811f00fe? Then as four unsigned bytes that is
    81 1f 00 f3
    and as four two's complement bytes that is
    81 1f 00 f3.

    Bytes is bytes is bytes.

  8. #8
    Stressed Student :(
    Join Date
    Feb 2008
    Location
    Berkeley, CA
    Posts
    73
    thanks.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    A byte, as explained is 8 bits. 8 ones and/or zeros that the computer can do stuff with. It can be used to represent a signed number, an unsigned number or a bitmap of one row of a chess board for example.

    One's complement, two's complement are the two most common ways to represent a negative number. Two's complement is preferred by most machine architects because it prevents negative 0 from being a second value for zero. [One's complement is a complete invert of the value, so a byte of zero 00000000 becomes 11111111 in ones complement. In two's complement 00000000 inverted becomes 11111111, add one and we get 10000000, but the one "falls off the edge", so we're back at zero - which means that we don't have two different values that could be zero. This makes the hardware easier to design, because a "compare with zero" is just one value to see if the register is equal to, rather than two compares that you get for a "negative zero" value].

    Unsigned numbers have no sign, so can never be negative [in computer terms, technically, an unsigned number could be negative too, but then ALL values that can be represented would be on the negative side of zero].

    The bit values are the same if the byte is signed or unsigned. How the computer WORKS with that number depends on whether you have told it "it's signed" or "it's unsigned". In the former case, it uses the highest bit to indicate negative numbers, in the latter case, all bits represent the number itself, and the sign is always positive.

    --
    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.

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by matsp View Post
    A byte, as explained is 8 bits.
    Yes, the book definition and all. But in C I like to work with the C definition. That way, as a bonus, you can make stuff work when the "8 bits" definition fails.
    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.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 08-11-2008, 11:02 PM
  2. 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
  3. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  4. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM