Swapping endianness of a long variable

Hi all,

I need a function that can swp the endianness of an unsigned long
variable.

For ex:
unsigned long a = 0x12345678;
some_func(&a); /* or a = some_other_func(a); */
/* Now the value a should be 0x78563412 */

Till now I have written my own function to perform this swap. Does VC/
VC++ provides any such standard function.

Thanks in advance,
MJ

0
2/26/2007 6:58:43 AM
vc.mfc 33608 articles. 0 followers. Follow

4 Replies
1226 Views

Similar Articles

[PageSpeed] 49

On 25 Feb 2007 22:58:43 -0800, "MJ_India" <mail.mohitjain@gmail.com> wrote:

>Hi all,
>
>I need a function that can swp the endianness of an unsigned long
>variable.
>
>For ex:
>unsigned long a = 0x12345678;
>some_func(&a); /* or a = some_other_func(a); */
>/* Now the value a should be 0x78563412 */
>
>Till now I have written my own function to perform this swap. Does VC/
>VC++ provides any such standard function.
>
>Thanks in advance,
>MJ

_byteswap_uint64, _byteswap_ulong, _byteswap_ushort 
http://msdn2.microsoft.com/en-us/library/a3140177(VS.80).aspx

-- 
Doug Harrison
Visual C++ MVP
0
dsh (2498)
2/26/2007 4:54:47 PM
On Feb 26, 9:54 pm, "Doug Harrison [MVP]" <d...@mvps.org> wrote:
> On 25 Feb 2007 22:58:43 -0800, "MJ_India" <mail.mohitj...@gmail.com> wrote:
> >I need a function that can swp the endianness of an unsigned long
> >variable.
> >Does VC/VC++ provides any such standard function.
>
> _byteswap_uint64, _byteswap_ulong, _byteswap_ushorthttp://msdn2.microsoft.com/en-us/library/a3140177(VS.80).aspx
>
> --
> Doug Harrison
> Visual C++ MVP

Thank-you for a very quick reply.
I'm afraid the sample program given on the MSDN page is throws
following error:
My code is: <I have started with a "hello world console application>

// mj.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdlib.h>

int main(int argc, char* argv[])
{
	unsigned long ul_gagan;
	ul_gagan = 0x12345678;
	printf("Hello World!\n");

	printf("byteswap of %Ix = %Ix", ul_gagan, _byteswap_ulong(ul_gagan));

	return 0;
}

Compilation result is:
compiling. . .
mj.cpp
[path]\mj.cpp(13) : error C2065: '_byteswap_ulong' : undeclared
identifier
error executing c1.exe

mj.exe - 1 error (s), 0 warning(s)

I'm using Microsoft VC++ 6.0 Enterprise edition on Windows XP.

Please help....

0
2/27/2007 9:31:47 AM
> Compilation result is:
> compiling. . .
> mj.cpp
> [path]\mj.cpp(13) : error C2065: '_byteswap_ulong' : undeclared
> identifier
> error executing c1.exe
>
> mj.exe - 1 error (s), 0 warning(s)
>
> I'm using Microsoft VC++ 6.0 Enterprise edition on Windows XP.
>
> Please help....

I just created a new project in VC2005 with the following:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char* argv[])
{
	unsigned long ul_gagan;
	ul_gagan =3D 0x12345678;
	printf("Hello World!\n");

	printf("byteswap of %Ix =3D %Ix", ul_gagan, _byteswap_ulong(ul_gagan));=


	return 0;
}

It compiled and ran perfectly fine.

I would guess that the _byteswap_* functions weren't available in VC6.

Eric
0
eric6371 (58)
2/27/2007 3:53:18 PM
They aren't.  But you can write them trivially.

#pragma warning(disable:xxxxx)
__inline UINT __fastcall Swap32(UINT n)
    {
     __asm {
             mov eax, ecx
             bswap eax
       }
    }
#pragma warning(default:xxxxx)

The nice thing in VS6 is that it actually generates the instruction inline, while VS.NET
will not inline the function and generates a call, even in release mode. 

xxxxx is the error code that tells you that you failed to return a value from the
function. 
				joe


On Tue, 27 Feb 2007 09:53:18 -0600, "Eric Hill" <eric@ijack.net> wrote:

>> Compilation result is:
>> compiling. . .
>> mj.cpp
>> [path]\mj.cpp(13) : error C2065: '_byteswap_ulong' : undeclared
>> identifier
>> error executing c1.exe
>>
>> mj.exe - 1 error (s), 0 warning(s)
>>
>> I'm using Microsoft VC++ 6.0 Enterprise edition on Windows XP.
>>
>> Please help....
>
>I just created a new project in VC2005 with the following:
>
>#include <stdio.h>
>#include <stdlib.h>
>
>int main(int argc, char* argv[])
>{
>	unsigned long ul_gagan;
>	ul_gagan = 0x12345678;
>	printf("Hello World!\n");
>
>	printf("byteswap of %Ix = %Ix", ul_gagan, _byteswap_ulong(ul_gagan));
>
>	return 0;
>}
>
>It compiled and ran perfectly fine.
>
>I would guess that the _byteswap_* functions weren't available in VC6.
>
>Eric
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
newcomer (15972)
3/2/2007 6:57:45 AM
Reply:

Similar Artilces:

variable height variable width stacked bar charts
Hi I need to create a chart which would resemble a variable width variable height stacked bar chart with values on both sides of the Y axis (This I achieved by giving values needed on the left a negative sign). I tried to get the above chart using all these basic charts in excel - stacked bar charts, stacked column charts,stacked area charts, stacked scatter plots but could not get the final chart. Though I was able to get a variable height stacked bar chart(with pos n neg values), I am not able to get the final desired chart. Please let me know how I can get the above mentioned chart. Than...

swap axes
In a line chart, Excel seems to predetermine the category axis - how can I swap this so that my Y-value axis appears along the bottom? Hi, Have a look at Jon Peltier's page http://peltiertech.com/Excel/Charts/Y_CategoryAxis.html Cheers Andy Fridgemaster wrote: > In a line chart, Excel seems to predetermine the category axis - how can I > swap this so that my Y-value axis appears along the bottom? -- Andy Pope, Microsoft MVP - Excel http://www.andypope.info Hi, I've got the same problem as Fridgemaster. Andy, good try, but your solution is not what we are asking. Prima...

Pass environmental variables?
Hi there. Is there a way to pass the UNIX equivalent of PWD into a formula automatically? For example: =INDIRECT(PWD&"\Tasks[file0.xls]Tasks!D25") Where PWD = "C:\Documents and Settings\Scott\My Documents\directory1\directory2" I'm currently building [upgrading] a custom project tracking tool for a client. Initially, they only wanted to use it on one computer. All I had to do was hard code the address in that case. They like the tool so much that they want to pass it out to 100 field staff. UGH! Thanks! Scott Ghiz www.SGSystems.com Scott, You can creat...

Swapping cells on linked spreadsheets
I have 2 spreadasheets. The fist one is named "Name and Bed Entry". On this spreadsheet the user literally types in a name as it relates to a bed (or room number) and other info. The name and room number is then linked to another spreadsheet called Master Resident Info. This spreadsheet has the cost of that room. So, in Master Resident Info, the name entry actually looks like ... =IF('[Name & Bed Entry test.xls]Name Entry'!$B$52="","",'[Name & Bed Entry test.xls]Name Entry'!$B$52). The cost of the room in Master Resident...

Secondarey DPM server long-term protection greyed out
Hi ! I have setup a secondary dpm server (2010 RTM) to protect the primary server and the data that the primary backup. When I make the protection group it is no problem backing up c:, system state, local dpm db on the primary, this works fine and i can choose the long-termin protection using tape. But when i want to protect the data the primary server already has protected I can choose long term but the Next button is greyed out and I can't carry on. Why Is this ? and can it be fixed ? Thanx for your help! This issue is already discussed and close on forum at: http://social.technet.mi...

Excel should allow you to swap rows. Also swap columns
This is a general feature request for Excel. It would be great if I could select 2 rows (using ctrl + click). Right-click on one of those rows and have a menu choice called Swap. Example: assume that I have selected row 2 and row 4, Before: 1 2 3 4 After: 1 4 3 2 ...

Swapping Motherboards
I have two older machines that are now exhibiting issues indicating a motherboard failure of some sort. Is a simple motherboard swapout possible, using the same HDD in each machine? I would attempt to obtain a M/B as close as the original as possible, but since they are at least 5 years old, that may not be possible, or desirable. I do not relish the thought of having to reinstall the OS and reinstalling all applications on these two machines. Thanks. -- System Administrator Sprotte + Watson Architecture and Planning Vista, CA You will probably be able to do a Repair Insta...

Codecs swap
Hi, I have a 2 TCHAR arrays has #define MAX_SIZE 20 _TCHAR codec1[MAX_SIZE]; _TCHAR codec2[MAX_SIZE]; I want to swap them , how do i do. is it just , swap(codec1, codec2); or i should use some other API for this to get done. Didn't you ask this question yesterday? There is no api function for that. void Swap(TCHAR *pStr1,int Len1,TCHAR *pStr2,int Len2) { TCHAR *pTemp = new TCHAR[strlen(pStr1)+1]; strcpy(pTemp,pStr1); strncpy(pStr1,pStr2,Len1); strncpy(pStr2,pTemp,Len2); } AliR. "lucky" <Laxmanmaruthy@gmail.com> wrote in message news:11701496...

long formulas
I create really long formulas and when I have the cell with the long formula highlited then the formula is so long that it coveres up the first 2 rows of my spreadsheet. IS there a way to fix this without turning off the forula bar - like truncating the formula bar or something? _________________________ PLAY POKER ONLINE Click <a href="http://www.partypoker.com/index20100.htm?wm=2006013" title="www.partypoker.com">here</a> for a 20% deposit bonus at Party Poker Click <a href="http://www.empirepoker.com/index20100.htm?wm=1702827" title="www.emp...

Syntax for using a variable as part of an object name.
I have 10 cells named UI1 through UI10. I want to put data into the cells through a For loop. What is the proper syntax to use a variable as part of the object name? In the sample below, CELL1 is a variable whose value is the name of the field where I want to enter data. I need to have VB recognize the value of the variable as the field name. Dim Cell1, DATA1 as variant Dim x as integer <DATA1 - calculated > For x = 1 to 10 CELL1 = "UI" & x RS.Edit RS!CELL1 = Data1 -- Error - What is the proper syntax? RS.Update <Change ...

Outbound emails queued for long time
Hi, I have an Exchange 2000 sitting on a pretty juiced up machine. Sending emails withing the company is almost instant. Receiving emails from outside of the company is pretty fast too. But sending emails to outside of the company, takes as much as an hour to leave the queue. Looking in the Queue folder, there are approximately 400 domains, and the Total Message Size is anywhere from 1-90 kb. Which I dont think is a whole lot. Checking the processesor usage, it reads at 1-30%. What would be the cause of the long wait in the queue? Thanks, "Hasan Kachal" <kachal@nospam....

Install FRx on second serve, then swap out
We are running FRx 6.5 for Great Plains 7.0. The seperate server FRx resides on needs to be rebuilt. We want to build up another server with FRx 6.5 on it then swap servers. We want this to be transparent to users as much as possible. Is anyone aware of any documentation other that the regular instalation guide for guidance? Any tips and gotchya's appreciated. Thanks. Kevin ...

Lookup? If Then? Need some advice.. (long)
I hope you veterans out there aren't bothered by my question, but I'm completely stumped. Background: I'm designing an excel workbook that will help me and my friends keep notes and track stuff for a boardgame I've developed. The most important aspect of this workbook is the "battle page" - the display and comparison of two army groups: attacker and the defender. Each player (game can have up to 6 players) has up to 12 army groups at his command. Each army group can hold any number of units of 18 specific types (Parthian cavalry, Balaeric slingers, Roman Legionai...

Formula too long #3
I have an extremely long formula that sums the totals from 13 different worksheets saved in a different folder. When I try to edit the formual I get an error that says formula too long. Is there a way to increase the amount of information within a formula because in the future I am going to need to add more parts to this formula and it is already too long. Thanks for the help. Todd I suggest you write a macro to do this. Then It will be virtually unlimited. >-----Original Message----- >I have an extremely long formula that sums the totals from >13 different worksheets...

Partition Swapping
XPS/Dimension 630i. This came with a 600GB HDD and I added a 1.5TB HDD. The 600 came with Vista and I created a 100GB partition on the 1.5TB for Windows 7. I now want to place 7 on the 600GB and move Vista to the 100GB partition. I've imaged both the 600GB and 100GB separately using Acronis. Can I use the Windows 7 image and restore it to the 600GB HDD and vise versa? -- "Don't pick a fight with an old man. If he is too old to fight, he'll just kill you." The 1.5. tb drive is much faster at read/writes than the 600gb clunker. Keep Win 7 on the...

vb variables
have a series of check boxes on a form such as check box 1 labelled bhn, check box 2 labelled dme, check box 3 labelled amb when any of these boxes are checked, want to store the value in a vb variable. for example, if the first one is checked, the value in the variable would be bhn. then i want to take that variable and use in a query. how can i do this? i was trying to use case in vb code and then when the box is checked, take a numeric value and give the text value in the variable. but then i could not get the query to see the variable. thanks for your help jnewl wrote: >hav...

create report based on variable date/time
I have created a simple dbase based on an export of an excel sheet. The dbase is named "ExportSC" I have created 3 reports which are base on 3 date colums in de dbase. report 1: open -> based on column "open date" and the results in report are a result from a filter "=> "dd-mm-yyyy hh:mm" report 2: close -> based on column "close time" and the results in the report are a result of same filter with diff values. report 3: planned -> based on column "planned start" and the results in the report are a result of a filter "B...

Variable Data_Images_Excel
I am trying to create a database consisting of .tiff or .bmp in Excel. The objective of this database is to use it to push variable images based on using mailmerge in MSWord. The final output will be a newsletter that uses both Contact information and images from the Excel spreadsheet. The question is how do I bring in pictures into Excel, save them and be able to map the images to a word document. I am using Fiery FreeForm to do this. Thanks. Jeff ...

Long, nested functions
Hello All, We inherited a program that exports temperature dependent equations for materials properties in Excel format. It works beautifully, except that the database does not know about the nested function limit. For many of the properties the equation changes 9 or 10 times depending on the temperature range. There are thousands of these equations, and we have no way of knowing which are too nested and which are not until we try them and they do not work. In addition, breaking the nests up over multiple cells or using lookups would be way way too labor intensive. We discovered that the...

Resource with Vendor Swap
Hope someone can help me with this, sorry i am posting it again We are using Project Office 2003 (SP3) and SQL 2005 database, PWA as our Time reporting tool When a new Resource XXXX joins, we create a new id and enter all the information (Email Id, Vendor, Cost ...) using Project office and we link this to PWA. This has been working fine with out any issues. Suppose if the same Resource XXXX after 6 months swaps a vendor, we create a new ID for the existing resources XXXX. We do this for our auditing report to display resource with the right vendor and the right hours worked (If we set up the...

Code for variable sort order
I'm trying to code a program that allows flexible sorting based on certain criteria. I'm having trouble using a variable for sort order. Here is the code. When I hard code sort order (i.e., xlAscending or xlDescending) it works fine. Any suggestions on how to make sort order accept a variable? Selection.Sort Key1:=Range(mSortKey1), Order1:=mSortOrder1, _ Key2:=Range(mSortKey2), Order2:=mSortOrder2, _ Key3:=Range(mSortKey3), Order3:=mSortOrder3, _ Header:=xlGuess, OrderCustom:=1, MatchCase:=False, _ Ori...

Swap File
IIRC, the swap file is fake RAM created on your hard drive, right? If you have 70GB of space on your hard drive, and set Windows to automatically increase your page file, could it go up to 70GB or is there some place in which Windows kicks out? I tried the following code (in MFC, so I didn't have to worry about memory leaks -- it took care of them for me) just to see how far it would go: long double* a; while (true) { a = new long double[100]; }; I got up to 2GB and gave up and killed the program. Windows seemed to keep up with my program. Will it go all the way up to 70GB? Note th...

Regression with Binary Decision Variables
Is there a way to make the Regression function in the data analysis toolpack recognize a binary decision variable? If so how? ...

Spreadsheet Updating Takes Too Long
I have a spreadsheet with between 15,000 to 26,000 rows and between 15 - 30 columns with formulas. When one change is made, it takes "forever" for the update process to complete. Is there anyway to speed up this process? What's causing the slow down? Try with manual calculation. go to Tools > options then click on the calculation tab. select manual press ok now everytime you make a change it wont update so after you make a bunch of changes or when you need press f9 to calculate Regards, Cesar Zapata ' "Ella" <anonymous@discussions.microsoft.com>...

Substitution variables in App.Config?
Is there a way to specify a substitution variable that can be used in several places in the app.config file (like you can do in Ant/NAnt build files)? For example, if there is part of a file path that I specify in several of the appSettings key values, can I somehow specify that path once, and just insert a substitution variable of some sort (e.g., "${path}") and have it get substituted at run-time? I could just separate them into different keys and handle it in code, but this path is being set on a key in a custom configuration section for a third-party component (log4net), so thi...