Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to swap Four bytes ?

Posted on 2014-03-18
13
Medium Priority
?
293 Views
Last Modified: 2014-03-19
virtual ParameterResult setValueRaw    (const char * val, unsigned short size, bool force = false) 
    {
        if (size == LONG_SIZE)
        {
           char *tv = new char[size];
      
           tv[0] = val[2];
           tv[1] = val[3];
           tv[2] = val[0];
           tv[3] = val[1];
      
           ParameterLong::setValue(tv, size, force);
			
           delete[] tv;
			}
          return getResultId();                                                      
    }

Open in new window


Above is how I did it.  Please share a better solution ?

We use Keil tools.  We write code for NXP LPC2468 ARM 7 Microcontroller.

Most Significant word is      00 01
Leastg Significant Word is   00 05

Example, val will be pointing to four bytes as follows:
01 00 05 00

After running the function:
05 00 01 00
0
Comment
Question by:naseeam
  • 6
  • 4
  • 3
13 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 39938035
This is pretty much the same that 'htonl()'/'ntohl()' perform. You can implement that using a macro, i.e.

#define SWAP_BYTES(n) (((((unsigned short)(n) & 0xFF)) << 8) | (((unsigned short)(n) & 0xFF00) >> 8))

Open in new window

0
 
LVL 86

Expert Comment

by:jkr
ID: 39938052
Ooops, forgot to add an example:

#define SWAP_BYTES(n) (((((unsigned short)(n) & 0xFF)) << 8) | (((unsigned short)(n) & 0xFF00) >> 8))

virtual ParameterResult setValueRaw    (const char * val, unsigned short size, bool force = false) 
    {
        if (size == LONG_SIZE)
        {
           long l = SWAP_BYTES((long) val);
      
           ParameterLong::setValue((char*) l, size, force);
			
           
	}

          return getResultId();                                                      
    }
                          

Open in new window

0
 
LVL 37

Expert Comment

by:bbao
ID: 39938062
just right shift the four-byte LONG WORD variable for 16 bits.

see below for the details.

FYI - Left Shift and Right Shift Operators (>> and <<)
http://msdn.microsoft.com/en-us/library/336xbhcz.aspx
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 86

Expert Comment

by:jkr
ID: 39938074
>>just right shift the four-byte LONG WORD variable for 16 bits.

Yes, that's exactly what my example does ;o)
0
 
LVL 37

Expert Comment

by:bbao
ID: 39938128
sorry, i posted from mobile and didn't see your previous comment. you must be posting it when i was typing slowly on the phone. :-)
0
 
LVL 1

Author Comment

by:naseeam
ID: 39938403
Macro you provided looks correct to me.  I must be doing something wrong.  Please help debug.

#define SWAP_WORDS(n) (((((long)(n) & 0xFFFF)) << 16) | (((long)(n) & 0xFFFF0000) >> 16))

Open in new window



 virtual ParameterResult setValueRaw            (const char * val, unsigned short size, bool force = false) 
    {
			if (size == LONG_SIZE)
			{
        long l = SWAP_WORDS( (long)(*((long *)val)) );
            
        ParameterLong::setValue((char *)l, size, force);
			}
      return getResultId();                                                      
    }

Open in new window


I pass 0x00000300 to this function.  value of l is  0x00000003.  That's incorrect!
0
 
LVL 1

Author Comment

by:naseeam
ID: 39938417
After swapping, it should be 0x03000000
0
 
LVL 86

Expert Comment

by:jkr
ID: 39938419
Too much casting IMO - just try

           long l = SWAP_BYTES((long) val);
      
           ParameterLong::setValue((char*) l, size, force);

Open in new window

0
 
LVL 1

Author Comment

by:naseeam
ID: 39938447
>> long l = SWAP_BYTES((long) val);

but val is pointer.  If you pass val, then address will be swapped.  We need to swap what val points to.
0
 
LVL 86

Expert Comment

by:jkr
ID: 39938458
I took that into account, try it ;o)

It is a pointer, but will be treated as an integer for swapping. Then later, it is cast back to a pointer.
0
 
LVL 86

Accepted Solution

by:
jkr earned 1400 total points
ID: 39938467
Ooops, sorry, I see what you mean - in that case, try

           long l = SWAP_BYTES(*((long*) val));
      
           ParameterLong::setValue((char*) &l, size, force);

Open in new window

0
 
LVL 37

Assisted Solution

by:bbao
bbao earned 600 total points
ID: 39938572
IMHO, from a perspective of professional programming, it is NOT a good practice to use a LONG WORD variable for calculating a pointer, as the code will be highly processor, platform and compiler dependent. maintainIng this kind of code could be very difficult, as compiling the same code with different compiler or with different processor may cause different result, hence calling the pointer or using the pointed data may cause unexpected software behaviour.

commonly, as a best practice, pointers should be always managed by pointer variables, not converted from numbers even something like LONG WORD. a pointer's value should not be calculated that way except the compuation is under control such as that for string operations.

however, if for some reasons a processor specific pointer calculation is really necessary, e.g. for a kernel routine of memory management, preprocessor directives and conditional compilation directives are required at the beginning of source code to control the compiling and building process in order to make the result consistent and of course correct.
0
 
LVL 1

Author Closing Comment

by:naseeam
ID: 39939588
Great support.  Provided solution and debugged the problem.

Excellent portability tip.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question