473,708 Members | 2,438 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Little Endian to Big Endian

hi I wanted to convert Little Endian to big endian for 2 byte value

for example

if hex value of aValue = 0102
required valu e = 0201

This can be done

bValue = ( (aValue << 8) | (aValue << 8))

But how about if value is bValue = F26B
I am getting FFF2 instead of 6BF2

Thanks for help

Jul 23 '05 #1
10 7112
"invincible " wrote
hi I wanted to convert Little Endian to big endian for 2 byte value

for example

if hex value of aValue = 0102
required valu e = 0201

This can be done

bValue = ( (aValue << 8) | (aValue << 8))

But how about if value is bValue = F26B
I am getting FFF2 instead of 6BF2


Try this:
bValue = (aValue << 8) | (aValue >> 8);
Jul 23 '05 #2
On Tue, 14 Jun 2005 09:46:35 +0530, "invincible "
<mo**********@i n.bosch.com> wrote in comp.lang.c++:

First, if you are going to post the same question in multiple
newsgroups, you should cross-post it, not post it separately to each
group.
hi I wanted to convert Little Endian to big endian for 2 byte value
Two bytes equals 32 bits on one platform I work on, and 64 bits on
another that I haven't used for a few years. Do you mean a 16-bit
value?
for example

if hex value of aValue = 0102
required valu e = 0201

This can be done
Yes, it can.
bValue = ( (aValue << 8) | (aValue << 8))
But not like that!
But how about if value is bValue = F26B
I am getting FFF2 instead of 6BF2

Thanks for help


Bit-shift and logical operators should not be used on signed integer
types unless you know exactly what you are doing, and even then only
rarely.

Used unsigned types.

#include <stdio.h>

unsigned int swap_octets(uns igned int aval)
{
return ((aval << 8) | (aval >> 8)) & 0xffff;
}

int main()
{
unsigned short aval;
unsigned short bval;

aval = 0x0201;
bval = (aval << 8) | (aval << 8);

printf("Your method, %04X produces %04X\n", aval, bval);

printf("%04X reverses to %04X\n", aval, swap_octets(ava l));

aval = 0xF26B;
printf("%04X reverses to %04X\n", aval, swap_octets(ava l));

return 0;
}

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 23 '05 #3
invincible wrote:
hi I wanted to convert Little Endian to big endian for 2 byte value

for example

if hex value of aValue = 0102
required valu e = 0201

This can be done

bValue = ( (aValue << 8) | (aValue << 8))
If aValue is signed, you need to remove the sign extension.

bValue = ( (aValue << 8) | ( 0xff & (aValue >> 8) ))

But how about if value is bValue = F26B
I am getting FFF2 instead of 6BF2

Thanks for help

Jul 23 '05 #4
"tuvok" <52************ ***@t-online.de> wrote in message
news:d8******** *****@news.t-online.com...
"invincible " wrote
hi I wanted to convert Little Endian to big endian for 2 byte value

for example

if hex value of aValue = 0102
required valu e = 0201

This can be done

bValue = ( (aValue << 8) | (aValue << 8))

But how about if value is bValue = F26B
I am getting FFF2 instead of 6BF2


Try this:
bValue = (aValue << 8) | (aValue >> 8);


Hmmm... I think that would work if you make aValue and
bValue unsigned. But if they're signed, that right shift
is going to pour-in a bunch of 1s if the msb of aValue
is a 1.

example:

#include <iostream>
int main(void)
{
register signed int Blat = 0x80000000;
Blat = Blat >> 21;
std::cout << std::hex << Blat << std::endl;
return 0;
}

Do you expect that to print 400?
No, it prints fffffc00.

BUT, if you make it unsigned:

#include <iostream>
int main(void)
{
register unsigned int Blat = 0x80000000;
Blat = Blat >> 21;
std::cout << std::hex << Blat << std::endl;
return 0;
}

then it prints 400 as expected.
--
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jul 23 '05 #5
"invincible " <mo**********@i n.bosch.com> wrote in message
news:d8******** **@ns2.fe.inter net.bosch.com.. .
hi I wanted to convert Little Endian to big endian for 2 byte value

for example

if hex value of aValue = 0102
required valu e = 0201

This can be done

bValue = ( (aValue << 8) | (aValue << 8))

But how about if value is bValue = F26B
I am getting FFF2 instead of 6BF2

Thanks for help


You're sign-extending. You must be using a C++ implimentation
which uses 16-bit int. (Or you're using short int.)

Use unsigned ints to avoid that problem:

#include <iostream>
int main(void)
{
unsigned int aValue, bValue;
aValue = 0xA857;
bValue = ((aValue&0x00FF )<<8) | ((aValue&0xFF00 )>>8);
std::cout << std::hex << bValue << std::endl;
return 0;
}

Prints "57A8".

--
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
web: home dot pacbell dot net slant earnur slant

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jul 23 '05 #6
"Gianni Mariani" <gi*******@mari ani.ws> wrote in message
news:Cc******** ************@sp eakeasy.net...
invincible wrote:
hi I wanted to convert Little Endian to big endian for 2 byte value

for example

if hex value of aValue = 0102
required valu e = 0201

This can be done

bValue = ( (aValue << 8) | (aValue << 8))


If aValue is signed, you need to remove the sign extension.

bValue = ( (aValue << 8) | ( 0xff & (aValue >> 8) ))


Be aware that the constant 0xff can have its sign extended when it's
promoted to 16 bits. So

0xff & (aValue >> 8)

can become:

0xffff & (aValue >> 8)

which isn't what you intended and doesn't kill the sign extension.

I always write:

0x0ff & (aValue >> 8)

to avoid this problem.

-Dana

Jul 23 '05 #7
invincible wrote:
hi I wanted to convert Little Endian to big endian for 2 byte value


Only use the predefined functions htonl, htons, ..., and not your own
hacks.

Jul 23 '05 #8
Panjandrum wrote:
invincible wrote:
hi I wanted to convert Little Endian to big endian for 2 byte value

Only use the predefined functions htonl, htons, ..., and not your own
hacks.


Not part of the ISO standard. I think they're POSIX, but they aren't
guaranteed on all systems.
Jul 23 '05 #9
Dana Cartwright wrote:
"Gianni Mariani" <gi*******@mari ani.ws> wrote:

If aValue is signed, you need to remove the sign extension.

bValue = ( (aValue << 8) | ( 0xff & (aValue >> 8) ))

Be aware that the constant 0xff can have its sign extended when
it's promoted to 16 bits.


Actually it can't. 0xff is (int) 255, and this must remain
as 255 when it is promoted to anything. In fact it
does not get promoted at all in this example.
I always write:

0x0ff & (aValue >> 8)

to avoid this problem.


0x0ff is identical to 0xff .

Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
29010
by: hicham | last post by:
Hi, I am looking for help, i would like to know how can i use the endian.h and config.h to convert compiled files under solaris from BIG-ENDIAN to compiled files LITTLE-ENDIAN. I am working under linux debian 3.0 woody Thank you for your help.
3
5189
by: gary | last post by:
Hi, 1. About all C/C++ compilers, Does stack increase from high address to low address and heap grow increase from low to high? What on earth decides their increase direction, CPU architecture, OS or compiler? 2. In GNU gcc, { int a = {0, 1, 2, 3, 4}; bool b; float c; for (int i = 0; i < 5; i++) {
8
27461
by: Perception | last post by:
Hello all, If I have a C-like data structure such that struct Data { int a; //16-bit value char; //3 ASCII characters int b; //32-bit value int c; //24-bit value }
2
6546
by: bhatia | last post by:
Hello all, If I have a C-like data structure such that struct Data { int a; //16-bit value char; //3 ASCII characters int b; //32-bit value int c; //24-bit value }
5
3143
by: manishster | last post by:
I keep getting the following in my output file , regardless of whether I convert endian-ness or not . How do i get "01 02 03 04" .... Mahamannu output : 04 03 02 01 b0 00 00
3
2626
RRick
by: RRick | last post by:
This was a question that showed up in a job interview once. (And to answer your next question: No, I didn't :)) Write a subroutine that returns a bool on whether a system supports big endian numbers (true) or little endian (false). I tried something like the following, but never did find out if it was correct. There are two assumptions here: First, big endian direction from small to large is ordered like a string. Second, endian is...
6
3176
by: Javier | last post by:
Hello people, I'm recoding a library that made a few months ago, and now that I'm reading what I wrote I have some questions. My program reads black and white images from a bitmap (BMP 24bpp without compression). It has it's words and dwords stored in little- endian, so I do a conversion to big-endian when reading full words or dwords. I have done this because my system is big-endian. But now... what if one compiles the library in a...
23
14173
by: guthena | last post by:
Write a small C program to determine whether a machine's type is little-endian or big-endian.
2
4053
by: Ramesh | last post by:
Hi I have a structure as below on big endian based system typedef struct { unsigned long LedA:5; unsigned long LedB:4; unsigned long LedC:8;
23
7039
by: Niranjan | last post by:
I have this program : void main() { int i=1; if((*(char*)&i)==1) printf("The machine is little endian."); else printf("The machine is big endian."); }
0
8788
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8697
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9290
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9001
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7925
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5939
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4454
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2508
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2097
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.