Word from 2 bytes

I am trying to extract two bytes from a 16-bit word, and to make a 16-bit word from two bytes. This is what I have tried (byte = unsigned char, word = unsigned short):

Split grpix word into 2 bytes:

word grpix; // Assume that the value has been initialized

byte grpixl = grpix & 0x00FF;
byte grpixh = grpix & 0xFF00;

Make grpix word from 2 bytes

byte grpixh; // Assume that the value has been initialized
byte grpixl; // Assume that the value has been initialized

word grpix = grpixh;
grpix <<= 8;
grpix += grpixl;

For some reason, my code doesn’t work as expected, and now I’m not sure if the «splitting» of the word is wrong, if the «making» of the word is wrong, or both… Could you give me some advice?

asked Feb 21, 2013 at 1:10

Fernando Aires Castello's user avatar

4

You’re not shifting when you split the word. So if grpix is 0x1234, then grpixl gets the expected 0x34 but grpixh ends up as 0x1200. You should say

byte grpixh = grpix >> 8;

Of course, you’re also ignoring any endianness concerns that may be present. You should probably convert your word to a known endian (with something like htons()) before attempting to split (and do the reverse conversion when joining).

answered Feb 21, 2013 at 1:15

Lily Ballard's user avatar

Lily BallardLily Ballard

181k29 gold badges378 silver badges343 bronze badges

1

Get to know: http://graphics.stanford.edu/~seander/bithacks.html for doing all manner of operations.

right_byte = short_val & 0xFF;
left_byte = ( short_val >> 8 ) & 0xFF

short_val = ( ( left_byte & 0xFF ) << 8 ) | ( right_byte & 0xFF );

I always do a &0xFF mask to assure I have no sign problems.

answered Feb 21, 2013 at 1:21

Gilbert's user avatar

The simple code that I use to solve this, is:

word=(msb<<8)+lsb;

cнŝdk's user avatar

cнŝdk

31.2k7 gold badges56 silver badges77 bronze badges

answered Jan 10, 2019 at 7:56

mohammad's user avatar

0

The following routines have proved very reliable for me:-

unsigned short get16int(a) char *a;{
   unsigned short hi,lo;
   hi = *a++ << 8;
   lo = *a  & 0x00ff; /* Needed to cater for sign extending when msb bit is set */
   return (hi | lo);
}
put16int(a,i) char *a; int i;{
 *a++ = i >> 8;
 *a = i;
}

answered Apr 24, 2020 at 9:21

When you mask out the high byte, you need to also shift down by 8 bits, otherwise you just end up with a 16bit number with the bottom eight bits cleared.

byte grpixh = (grpix & 0xFF00) >> 8

Also your composition can be more efficient by using or-equals instead of plus-equals:

grpix |= grpixh << 8

Suraj Rao's user avatar

Suraj Rao

29.3k11 gold badges96 silver badges103 bronze badges

answered Feb 21, 2013 at 1:16

Ian McMahon's user avatar

Ian McMahonIan McMahon

1,65011 silver badges13 bronze badges

1

word grpix = grpixl+256*grpixh;

Suraj Rao's user avatar

Suraj Rao

29.3k11 gold badges96 silver badges103 bronze badges

answered Dec 8, 2022 at 5:01

DJ JAG's user avatar

How many words in 2 bytes? 2 bytes is equal to 1 words.

This page provides you how to convert between bytes and words with conversion factor.

  • 2 B = 1 words
  • How many bytes in 2 words? — 2 words = 4 B

How to convert 2 B to words?

To convert 2 B into words, follow these steps:

We know that, 1 words = 2 B

Hence, to convert the value of 2 bytes into words, divide the data storage value 2B by 2.

2 B = 2/2 = 1 words

Thus, 2 B equals to 1 words

Bytes Conversion of Bytes to Words
1.99 B 1.99 B = 0.995 words
1.9 B 1.9 B = 0.95 words
2 B 2 B = 1 words
3 B 3 B = 1.5 words

Data storage

  1. Home
  2. Categories
  3. Data storage
  4. bytes to word (32-bits)
  5. 2 bytes

2 By

bytes

Scientific notation

2.0e+00


Engineering notation

0.2e1


+>

0.5 word.32

word (32-bits)

Scientific notation

5.0e+01

This value is an approximation to the real value.


Engineering notation

0.5e0


Free online Data storage conversion. Convert 2 bytes to word (32-bits) (By to word.32). How much is 2 bytes to word (32-bits)? Made for you with much by CalculatePlus.

Try out the inverse calculation words (32-bits) to bytes.

Conversion table

By word.32
1 0.25
2 0.5
3 0.75
4 1
5 1.25
6 1.5
7 1.75
8 2
9 2.25
10 2.5
100 25
1000 250

Related with bytes

2 bytes to microbit

2 bytes to mebibits

2 bytes to EBy

2 bytes to terabytes

2 bytes to hBy

2 bytes to mebibytes

2 bytes to Zb

2 bytes to Ebit

2 bytes to terabits

2 bytes to characters (8-bits)

Ezoicreport this ad

Ezoicreport this ad

The controller I’m talking to sends back a string of bytes. The 8-bit
bytes are actually 16-bit word values sent out in HI-LO order.

So I get: <HI1><LO1><HI2><LO2>….
So register 1 = <H21>*256 + LO1 Register 2 = <HI2>*256+<HI2>
etc….

But I don’t seem to be putting the values together correctly in my
code below:

/* THE INLINE FUNCTION getWord */
char
getWord( char HiByte, char LoByte){ return ((HiByte & 0xFF)<<8) |
(LoByte & 0xFF); }

/* The modPacket type */
class CModPacket
{
public:
CModPacket(void);
~CModPacket(void);

unsigned int File;
unsigned int Offset;
unsigned int Length;
unsigned int DataLength;
unsigned int UnitID;

std::vector<unsigned int> Data;

unsigned int TransRef;
};

#include «.modpacket.h»

CModPacket::CModPacket(void) :
File(4),
Offset(1),
Length(1),
DataLength(0)
{
Data.reserve(255);
}

CModPacket::~CModPacket(void)
{
}

/***************************************************************
* parseReadMultiple
***************************************************************/
int
CModbusTCP_Compile::parseReadMultiple( char *pStream,
unsigned int Length,
CModPacket *pPacket )
{

unsigned int byte_length;
unsigned int x;
unsigned int y;
char *p_stream;
std::stringstream mystring;

/* GET NUMBER OF BYTES THAT FOLLOW */
byte_length = pStream[0];

if ( (unsigned int)(byte_length+1) > Length )
{
MessageBox( NULL, «Length no good», «parseReadMultiple», MB_OK );
return false;
}

/* COPY THE DATA TO THE STRUCTURE */
p_stream = &pStream[1];
char myhi;
char mylo;
for( x=0,y=0;x<byte_length;x+=2,++y )
{
myhi = ( p_stream[x] & 0xFF );
mylo = ( p_stream[x+1] &0xFF );

/* At this point, myhi = 0x1D and mylo = 0xE6 */
pPacket->Data[y] = getWord( myhi, mylo );

/* pPacket->Data[y] ends up with the value 4294967270 */
/* Expected value is 0x1D *256 + 0xE6 */
/* So expected value is: 7654 */

mystring.str(«»);
mystring << «[» << y << «]» << pPacket->Data[y] << » = » << myhi <<
» , » << mylo;
MessageBox( NULL, mystring.str().c_str(), «parseReadMultiple «,
MB_OK );
}

pPacket->DataLength = y;

return true;

} /* parseReadMultiple */

As you can see from the code below, I’m expecting the value 7654, but
end up with 4294967270.
Huh? That’s not even a 16-bit number.

If I convert the character values to unsigned ints and put them in a
message box, like so:

mystring.str(«»);
mystring << «[» << y << «]» << pPacket->Data[y] << » = » <<
(unsigned int_myhi << » , » << (unsigned int)mylo;
MessageBox( NULL, mystring.str().c_str(), «parseReadMultiple «,
MB_OK );

The message returned is:

«4294967270 = 29, 4294967270»

So, the problem is in the low byte, mylo. But how can it equal
4294967270 when it is taken from an 8-byte char that I have AND’d with
0xFF. It should have a maximum value of 255.

I’m confused. Can anyone tell me what I’m doing wrong?

Thanks in advance,
T

calculator

Home / Data Storage Conversion / Convert Byte to Word

Please provide values below to convert byte [B] to word, or vice versa.

From: byte switch
To: word
   

 

Byte to Word Conversion Table

Byte [B] Word
0.01 B 0.005 word
0.1 B 0.05 word
1 B 0.5 word
2 B 1 word
3 B 1.5 word
5 B 2.5 word
10 B 5 word
20 B 10 word
50 B 25 word
100 B 50 word
1000 B 500 word

How to Convert Byte to Word

1 B = 0.5 word
1 word = 2 B

Example: convert 15 B to word:
15 B = 15 × 0.5 word = 7.5 word

Popular Data Storage Unit Conversions

MB to GB

GB to MB

KB to MB

MB to KB

KB to GB

GB to KB

Convert Byte to Other Data Storage Units

Byte to Bit

Byte to Nibble

Byte to Character

Byte to MAPM-word

Byte to Quadruple-word

Byte to Block

Byte to Kilobit

Byte to Kilobyte

Byte to Kilobyte (10^3 Bytes)

Byte to Megabit

Byte to Megabyte

Byte to Megabyte (10^6 Bytes)

Byte to Gigabit

Byte to Gigabyte

Byte to Gigabyte (10^9 Bytes)

Byte to Terabit

Byte to Terabyte

Byte to Terabyte (10^12 Bytes)

Byte to Petabit

Byte to Petabyte

Byte to Petabyte (10^15 Bytes)

Byte to Exabit

Byte to Exabyte

Byte to Exabyte (10^18 Bytes)

Byte to Floppy Disk (3.5″, DD)

Byte to Floppy Disk (3.5″, HD)

Byte to Floppy Disk (3.5″, ED)

Byte to Floppy Disk (5.25″, DD)

Byte to Floppy Disk (5.25″, HD)

Byte to Zip 100

Byte to Zip 250

Byte to Jaz 1GB

Byte to Jaz 2GB

Byte to CD (74 Minute)

Byte to CD (80 Minute)

Byte to DVD (1 Layer, 1 Side)

Byte to DVD (2 Layer, 1 Side)

Byte to DVD (1 Layer, 2 Side)

Byte to DVD (2 Layer, 2 Side)

Arduino Forum

Loading

Понравилась статья? Поделить с друзьями:
  • Word friend in many languages
  • Word friend had an end
  • Word fresh in latin
  • Word frequency list for english
  • Word frequency in books