Byte, Word, and Dword

Ok, I know what they are. Byte is just one character(8 bit number), word is two characters(16 bit number), and dword is 4 characters(32 bit number). I know the ranges (assumes unsigned value)

byte - 0 to 255

word - 0 to 65535

dword - 0 to 4,294,967,295



I'm kind of confused on how I go about reading them. byte is simple, just get value, convert it into an ASCII code. Word is what throws me off. Get two chars, convert them into ascii codes, then multiply them together, right? Well,

255*255 = 65,025, NOT 65535.



What gives? I thought I had this figured out.



I think I'm close, but yet so far away. Any formulas are welcome. It's true I could take the two characters as their byte form, drop them to binary values, then combine the binary strings (keeping trailing 0's from both numbers, of course), and then convert that string back into a word value, but my code's pretty damn big as is, and that's like 20 more lines I'd have to write. I'd rather have a way to calculate the value right there on the spot, but if dropping to binary is the only way, it's cool. Just looking for a shortcut.



Thanks, C++ Gurus!!

Adam C.


Comments

  • : Ok, I know what they are. Byte is just one character(8 bit number), word is two characters(16 bit number), and dword is 4 characters(32 bit number). I know the ranges (assumes unsigned value)

    : byte - 0 to 255

    : word - 0 to 65535

    : dword - 0 to 4,294,967,295

    :

    : I'm kind of confused on how I go about reading them. byte is simple, just get value, convert it into an ASCII code. Word is what throws me off. Get two chars, convert them into ascii codes, then multiply them together, right? Well,

    : 255*255 = 65,025, NOT 65535.



    16 bits is the same as 2^16 and is the same as 2^8 * 2^8 which is the same as 256*256, not 255*255. You only subtract 1 from the total AFTER you've multiplied all your powers of two.



    (2^8 * 2^8) = (256*256) = 2^16 = 65536



    -Xotor-


  • : Ok, I know what they are. Byte is just one character(8 bit number), word is two characters(16 bit number), and dword is 4 characters(32 bit number). I know the ranges (assumes unsigned value)

    : byte - 0 to 255

    : word - 0 to 65535

    : dword - 0 to 4,294,967,295

    :

    : I'm kind of confused on how I go about reading them. byte is simple, just get value, convert it into an ASCII code. Word is what throws me off. Get two chars, convert them into ascii codes, then multiply them together, right? Well,

    : 255*255 = 65,025, NOT 65535.

    :

    : What gives? I thought I had this figured out.

    :

    : I think I'm close, but yet so far away. Any formulas are welcome. It's true I could take the two characters as their byte form, drop them to binary values, then combine the binary strings (keeping trailing 0's from both numbers, of course), and then convert that string back into a word value, but my code's pretty damn big as is, and that's like 20 more lines I'd have to write. I'd rather have a way to calculate the value right there on the spot, but if dropping to binary is the only way, it's cool. Just looking for a shortcut.

    :

    : Thanks, C++ Gurus!!

    : Adam C.

    :



    I'm not sure why you don't just typecast your values to the proper size when reading them, but to answer your question: If you're going to multiply the bytes, make sure you add 1 to each 0-based value before multiplying, then subtract 1 from the result for a 0-based result: 256*256 = 65536 and 65536*65536 = 4,294,967,296. Don't take my word for it though.






  • so for 1 byte = value * 256^0



    for 2 bytes:



    value = byte1 *(256^1) + byte0 * (256^0)



    for 512 bytes:



    value = byte511*(256^511) + byte510*(256^510) ... + byte0*(256^0)



    not sure if thats exactly what you're asking for, but the highest value that can be stored is one byte is 255 or ff hex. and so if you make all byte value=255 then you can workout the largest value.



    for 2 bytes therefore its gonna be:



    255*256 + 255*1



    for 4(dword):



    255*256^3 + 255*256^2 + 255*256^1 + 255*1



    which no doubt will=what it should.



    you get it?



    obviously when all bytes are 0 thats the lowest value, which will = 0.



    hope that helps.

    bye.




  • : Ok, I know what they are. Byte is just one character(8 bit number), word is two characters(16 bit number), and dword is 4 characters(32 bit number). I know the ranges (assumes unsigned value)

    : byte - 0 to 255

    : word - 0 to 65535

    : dword - 0 to 4,294,967,295

    :

    : I'm kind of confused on how I go about reading them. byte is simple, just get value, convert it into an ASCII code. Word is what throws me off. Get two chars, convert them into ascii codes, then multiply them together, right? Well,

    : 255*255 = 65,025, NOT 65535.

    :

    : What gives? I thought I had this figured out.

    :

    : I think I'm close, but yet so far away. Any formulas are welcome. It's true I could take the two characters as their byte form, drop them to binary values, then combine the binary strings (keeping trailing 0's from both numbers, of course), and then convert that string back into a word value, but my code's pretty damn big as is, and that's like 20 more lines I'd have to write. I'd rather have a way to calculate the value right there on the spot, but if dropping to binary is the only way, it's cool. Just looking for a shortcut.

    :

    : Thanks, C++ Gurus!!

    : Adam C.

    :



    The bytes should be or'd together:



    byte hi, lo;

    word w=((word)hi<<8)|lo;<br>


    dwords could be linked with performing that operation with two words (hiword shifted by 16) respectively.




  • To out say what all of these people have allready said in a short way:

    8 bits(one byte) can store 256 values, the highest value is 255, but dont forget the 0 is a value also.

    So you have:

    256 * 256 = 65536

    wich means that two bytes can store 65536 values

    wich gives an effective range of 0..65535.





    Thomas just wanted to show off.





    : Ok, I know what they are. Byte is just one character(8 bit number), word is two characters(16 bit number), and dword is 4 characters(32 bit number). I know the ranges (assumes unsigned value)

    : byte - 0 to 255

    : word - 0 to 65535

    : dword - 0 to 4,294,967,295

    :

    : I'm kind of confused on how I go about reading them. byte is simple, just get value, convert it into an ASCII code. Word is what throws me off. Get two chars, convert them into ascii codes, then multiply them together, right? Well,

    : 255*255 = 65,025, NOT 65535.

    :

    : What gives? I thought I had this figured out.

    :

    : I think I'm close, but yet so far away. Any formulas are welcome. It's true I could take the two characters as their byte form, drop them to binary values, then combine the binary strings (keeping trailing 0's from both numbers, of course), and then convert that string back into a word value, but my code's pretty damn big as is, and that's like 20 more lines I'd have to write. I'd rather have a way to calculate the value right there on the spot, but if dropping to binary is the only way, it's cool. Just looking for a shortcut.

    :

    : Thanks, C++ Gurus!!

    : Adam C.

    :






Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories