Swapping little endian short to big endian short

I am trying to read a unsigned short value that a C program generates in my Java MIDlet. I am reading the data using a ByteArrayInputStream in conjunction with a DataInputStream. I tried calling readShort but it was returning bogus values. Here's the problem:
The short I am trying to read in is 0xb500 (it was generated on a little endian machine) the value should be 181. I realized my mistake using readShort (it was signing the value) and so I called readUnsignedShort instead. Now the value of the int after the call to readUnsignedShort is 0xb500 (46336). I need to swap this value back to 0x00b5 (181) on the Java side. I have used the following function, but it returns 0xb50000 and i'm not exactly sure why.
The swap function is like so:
public static int swapInt(int i)
     return ((i << 24)
| ((i & 0x0000ff00) << 8)
| ((i & 0x00ff0000) >>> 8)
| (i >>> 24));
I do not have any idea what >>> is in Java. I found this function on google groups. If anyone could help me find a solution to my problem it would be much appreciated.

An int is a signed 4 byte value (as I am sure you know)
You are using an int to represent an unsigned 2 byte value.
Now you want to swap the 2 bytes of your unsigned 2 byte value.
This means that you only want to swap the two least significant bytes of your int.
Oh, by the way, USE CODE TAGS TO FORMAT CODE!!!
[code]
public static int swapUnsignedShort(int i) {
    return ((i & 0x00FF) << 8) | ((i & 0xFF00) >> 8);
}[[i]code]

Similar Messages

  • Big-endian or little-endian

    Hello,
    Is JVM big-endian or little-endian? Or is it platform dependent?
    How does one check this?
    Regards.

    I also saw over the internet that java is big-endian. But when you run this small piece of code,
    class Test {
         public static void main(String[] args) {
              short x = 10;
              byte high = (byte)(x >>> 8);
              byte low = (byte)x;/* cast implies & 0xff */
              System.out.println( "x=" + x + " high=" + high + " low=" + low );
    The output is:
    x=10 high=0 low=10
    This sample code I have taken from "mindprod.com" site only.
    Is storage different from display? Or am I missing something here?
    Regards.

  • How to judge the platform is little or big endian ?

    I have a c code, but anybody has a Java code?
    Thanks.
    #include <stdio.h>
    //little-endian or big-endian
    bool IsLittleEndian()
    int i = 1;
    char* p = (char*)&i;
    return *p;
    int main()
    if(IsLittleEndian())
      printf("LittleEndian\n");
    else
      printf("BigEndian\n");
    return 0;
    }

    http://www.codeproject.com/cpp/endianness.asp
    I got one, but how to translate into Java code.
    Auto detecting the correct Endian format of a data file
    Suppose you are developing a Windows application that imports Nuclear Magnetic Resonance (NMR) spectra. High resolution NMR files are generally recorded in Silicon or Sun Workstations but recently Windows or Linux based spectrometers are emerging as practical substitutes. It turns out that you will need to know in advance the Endian format of the file to parse correctly all the information. Here are some practical guidelines you can follow to decipher the correct Endianness of a data file:
    Typically, the binary file includes a header with the information about the Endian format.
    If the header is not present , you can guess the Endian format if you know the native format of the computer from which the file comes from. For instance, if the file was created in a Sun Workstation, the Endian format will most likely be Big-Endian.
    If none of the above points apply, the Endian format can be determined by trial and error. For example, if after reading the file assuming one format, the spectrum does not make sense, you will know that you have to use the other format.
    If the data points in the file are in floating point format (double), then the _isnan() function can be of some help to determine the Endian format. For example:
    double dValue;
    FILE* fp;
    fread(&nValue, 1, sizeof(double), fp);
    bool bByteSwap = _isnan(dValue) ? true : falseNote that this method does only guarantee that the byte swap operation is required if _isnan() returns a nonzero value (TRUE); if it returns 0 (FALSE), then it is not possible to know the correct Endian format without further information.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • What is Little Endian / Big Endian in Sound settings for Apple Intermediate

    Hi.
    In Final Cut Express, I am trying to splice together multiple video clips, combining footage from:
    1) HDV camcorder imported into iMovie 08 as 960 x 540 (the "lower resolution" option from iMovie '08 import), 16-bit @ 48 KHz (Big Endian), 25 FPS
    2) AVI files (DiVX 512 x 384, MP3 at 44.1 KHz, 23.98 FPS)
    3) Canon Camera "movie" files (Apple OpenDML JPEG 640 x 480, 16-bit Little Endian, Mono @ 44.1 KHz, 30 FPS)
    Questions
    1) What is this little endian / big endian thing?
    2) What is the best codec for me to edit in? My targets are NTSC DVD and also a HD version served via iPod connected to HDTV.
    I am not sure what codec to convert everything to, so that I can edit without having to RENDER every time I do something. I tried to export using QuickTime Pro to convert to Apple Intermediate Codec but am not sure about the option for "Little Endian" (I am using an Intel Mac; I assume I do NOT use little endian? Can someone help clarify?)
    Many thanks!!!

    1. They're compression formats. Different codec use different compression schemes.
    2. You should convert your material to QuickTime using the appropriate DV codec and frame rate.
    None of your material is HD. Some of it is low resolution, lower than even DV. There is no good way to make this material HD.

  • Any need for conversation from big endian and little endian?

    Hi,
    I am planning to migrate an Oracle 9i Database on AIX 5.3 to Oracle 11g R2 Windows 2008, and have planned to use transportable tablespace. But prior to that task is the conversation required from big endian and little endian using RMAN?
    Appreciate any suggestions, comments and hints
    Thanks

    Hi,
    Check V$TRANSPORTABLE_PLATFORM, it shows the ending for each supported platform. Given the results on my 11g, I suspect that you'll have to convert the tablespaces...
    SYSTEM@oracle11 SQL>select *
      2   from V$TRANSPORTABLE_PLATFORM
      3  ;
    PLATFORM_ID PLATFORM_NAME                                                                                         ENDIAN_FORMAT
              7 Microsoft Windows IA (32-bit)                                                                         Little
              6 AIX-Based Systems (64-bit)                                                                            Big
              8 Microsoft Windows IA (64-bit)                                                                         Little
             12 Microsoft Windows x86 64-bit                                                                          LittleHtH
    Johan

  • Little- och big-endian problem?

    Hi!
    I'm writing a program which will run on Windows, Mac OS and Linux. The program will be used for opening binary files created by a program in Windows.
    Because the files are created in Windows i guess they will be created according to the little-endian system. The program that will open them will read them in to the memory as a byte array.
    My question is: will there be a problem to get the right information from these files if I open them on a big-endian system? (There are no magic numbers in the binary file which indicates the used endian system)
    Lina

    I have to admit that I get the meaning of big/little endian confused.
    As JosAH says; Big endian means the big end is first,
    NOT that the big value is at the end.And I have to admit that I find that whole big/little-endian jargon quite confusing
    too, I mean the most significant byte is stored at the lowest address and we
    call it big-endian ... If the number is stored 'backwards' we call it little endian.
    In the old days when computers were made of wood and ran on steam, we
    used to call it 'low-byte-first' and 'high-byte-first'. Sigh ... those were the days ;-)
    kind regards,
    Jos

  • VDI 3.1 Big Endian vs Little Endian: VDI Cluster to Remote mysql server

    Hello,
    We are implementing a Dual Data center VDI environment and with that, will be using VCS to Cluster a remote Mysql instance. My question, if the VDI Cluster is on x86 (Little Endian), can the remote Mysql DB be on Sparc (Big Endian)?
    Thanks,

    The endian matching is only a requirement for the management and data nodes of a MySQL cluster. If you are going to be using a remote MySQL DB, the VDI servers will be connecting to that remote MySQL DB as a client connection, and there is no requirement for clients to match the endian type.
    http://dev.mysql.com/doc/refman/5.1/en/mysql-cluster-limitations-exclusive-to-cluster.html

  • Oracle guids little endian, big endian problem

    hello every one,
    I am having an strange issue. I am developing a .net app against an Oracle DB. Well, when I insert a guid into a raw oracle changes the order of the byte array.
    In VB.net I see (with all the representations of guids, with or without {,- or whatever):
    ac6d5c4f-542e-4fc8-b8b6-f53821811be3
    In Oracle I see (tested with toad and sql tools):
    4F5C6DAC2E54C84FB8B6F53821811BE3
    It goes ok with the rightests numbers but fails the the leftests.
    Any suggestion?

    I have to admit that I get the meaning of big/little endian confused.
    As JosAH says; Big endian means the big end is first,
    NOT that the big value is at the end.And I have to admit that I find that whole big/little-endian jargon quite confusing
    too, I mean the most significant byte is stored at the lowest address and we
    call it big-endian ... If the number is stored 'backwards' we call it little endian.
    In the old days when computers were made of wood and ran on steam, we
    used to call it 'low-byte-first' and 'high-byte-first'. Sigh ... those were the days ;-)
    kind regards,
    Jos

  • From little Endian to big Endian

    Hi guys, Is there a function in LabView 2010 to go from little endian to big endian? I want to change for example 'B0011000' to '001001B0'.

    how about this
    Labview user

  • Little endian & big endian format system

    Hi,
    I have code which works fine for Little endian format system, can somebady please tell how to get data from big endian format system.
    or is sap has setting where i can turn on and than run my code on that system.
    Thanks,
    John.

    hi
    chk this
    OPEN DATASET dset IN LEGACY TEXT MODE [(BIG|LITTLE) ENDIAN] [CODE PAGE cp]
    Effect
    Data is read or written in a form which is compatible to BINARY MODE in Releases <= 4.6. This addition is primarily used to convert a file into the code page format specified already when it is opened. At runtime, the system uses the format of the system code page of the application server. The system saves the file then again in the code page specified. This procedure is important if data is exchanged between systems using different code pages. For more information, see READ DATASET and TRANSFER.
    Notes
    on BIG ENDIAN, LITTLE ENDIAN
    These additions specify the byte sequence in which to store numbers (ABAP types I, F, and INT2) in the file.
    These additions may only be used in combination with the additions IN LEGACY BINARY MODE and IN LEGACY TEXT MODE. If these are not specified, the system assumes that the byte sequence determined by the hardware of the application server is used in the file.
    If the byte sequence specified differs from that determined by the hardware of the application server, READDATASET and TRANSFER make the corresponding conversions.
    These additions replace the language element TRANSLATE ... NUMBER FORMAT ... which must not be used in Unicode programs.
    on CODE PAGE cp
    This addition specifies the code page which is used to represent texts in the file.
    This addition may only be used in combination with the additions IN LEGACY BINARY MODE and IN LEGACY TEXT MODE. If this addition is not specified, the system uses the code page defined by the text environment current at the time a READ or TRANSFER command is executed (see SET LOCALE LANGUAGE).
    This addition replaces the language element TRANSLATE ... CODE PAGE ... which must not be used in Unicode programs.
    open datset ... IN LEGACY BINARY MODE [(BIG|LITTLE) ENDIAN] [CODE PAGE cp]
    Effect
    Data is read or written in a form which is compatible to BINARY MODE in Releases <= 4.6. This addition is primarily used to convert a file into the code page format specified already when it is opened. At runtime, the system uses the format of the system code page of the application server. The system saves the file then again in the code page specified. This procedure is important if data is exchanged between systems using different code pages. For more information, see READ DATASET and TRANSFER.

  • Openfirmware Little + Big Endian

    Hello,
    I was reading a article about OpenFirmware (this one in particlular), and it sayed you can use the command-line tool nvram to look and change the properties of OpenFirmware. When I typed in nvram -p, I got this:
    fcode-debug? false
    skip-netboot? false
    prev-lang:kbd en:0
    load-base 0x800000
    logger-base -1
    default-router-ip
    scroll-lock true
    oem-logo
    boot-screen screen
    default-server-ip
    default-gateway-ip
    security-#badlogins 2
    diag-switch? false
    oem-banner
    default-subnet-mask
    output-device screen
    use-generic? false
    logger-size -1
    diag-file ,diags
    screen-#rows 40
    oem-logo? false
    diag-device enet
    default-client-ip
    default-mac-address? false
    little-endian? false
    aapl,tdm-units
    mouse-device mouse
    input-device keyboard
    virt-base -1
    boot-script
    auto-boot? true
    selftest-#megs 0
    boot-args
    real-base -1
    ram-size 0x20000000
    use-nvramrc? false
    real-size -1
    virt-size -1
    boot-file
    console-screen screen
    boot-volume 3
    input-device-1 /ipc
    real-mode? false
    nvramrc
    gmt-offset 0
    security-mode none
    boot-device pci2/ata-6@D/@0:3,\\:tbxi
    output-device-1 /ipc
    pci-probe-mask -1
    oem-banner? false
    screen-#columns 100
    boot-command mac-boot
    What on earth is little endian doing there? I know that little endian means x86 (Intel, AMD, etc.), and big endian means PowerPC (IBM). What would happen if you turned it to true? This video shows you what little & big endian means. (from Apple) I'm kinda interested in this if x86 was there for years.

    I didn't actually say that there had ever been a little-endian PPC processor, just that Apple have maintained little-endian versions of the OS (and associated parts) and therefore it makes sense that there is some low-level support for controlling the way the system works.
    Just as an aside, the PPC chip has support for both models. According to:
    http://developer.apple.com/documentation/Hardware/DeviceManagers/pcisrvcs/pci_cards_drivers/PCIBOOK.250.html
    The PowerPC microprocessor supports a little-endian processing mode, in which addresses are swizzled when they are used to access memory.
    As that note indicates, it's not a full little-endian support as much as a dynamic flipper that can switch big-endian data into little-endian (and vice versa).

  • Is IntelCore processor big-endian or little-endian?

    Hi,
    I need to know if Mac mini's processor is big-endian or little-endian.
    Thanx.

    Little-endian.
    Snippit from What is endianness?:
    "On the Macintosh platform, PowerPC-based Macintosh computers use big endian addressing, while Intel-based Macs use little-endian addressing."

  • Big endian, little endian, and converting to another datatype.

    Hi all,
    I'm working on a sound visualization program. I'm putting the sound into byte arrays, and I then need to convert those bytes into ints to draw onto the screen. This is easy if the soulds are 8-bit, because you don't have the endian issue. How do I take a byte[] and pull ints from it that represent the waveform that the bytes were pulled from? Is it something like this (where soundBytes is the byte[] pulled from a sound file):
    int sampleFirstPart = (int)soundBytes[0];
    int sampleSecondPart = (int)soundBytes[1];
    int putTogether = sampleFirstPart + sampleSecondPart * 128;
    That's just a wild guess, and I'm not really sure what order these things should go in. How do you do it if it's big-endian? What about if it's little-endian? Is there a preference of one over the other? Does the fact that I'm working with wav files make a difference.
    I'm just full of questions, but any ammount of help you can give is very very much appreciated.
    thanks,
    Matt

    int value = soundBytes[x] + (soundBytes[x+1] << 8); // LE
    or
    int value = (soundBytes[x] << 8) + soundBytes[x+1]; // BE
    I think the LE and BE labels are right, but the calculation part is correct for one or the other.
    However, you need to know the format of the file to know if it's one or the other. You can't really tell from the bytes whether it's one or the other. There should, presumably, be some way to know from the audio format header. Either there'd be a flag to incidate which, or it would be assumed one way or the other because it's of format X. I think that wav files would be LE, but don't quote me on that.

  • Big endian - Little endian conversion

    Hi all, I'm a new user!
    I'm working with As400 and java using jtopen.
    With this code:
    PrintObjectTransformedInputStream is = spoolFile.getTransformedInputStream(printParms);           
    BufferedInputStream bufIn = new BufferedInputStream(is);                       
    BufferedOutputStream bufOut = new BufferedOutputStream(new FileOutputStream("c:/x.tif"));
    int c;
    while ((c = bufIn.read()) != -1) {
         bufOut.write(c);
    bufIn.close();
    bufOut.close();I'm getting a tiff stream and then write it to a file. The tiff stream is in Big endian format, but I need a tiff in Little endian format.
    How can I convert the stream from Big endian to Little endian?
    Thanks

    Tiff is a complex format, containing fields of different lengths, so you aren't likely to succeed just by reversing every pair of bytes in the stream without following the TIF format.
    You could look at the JAI (Java Advanced Imaging), which can read and write TIFF files in a variety of modes.

  • Is Labview handling automatically big endianness?

    I am working with a Labview Application that communicates through a serial line with another computer. I am using VISA subroutines.
    My PC has an Intel processor which is Little Endian. I have read that Labview is Big Endian. My question is: Is Labview handling automatically the endianness of the processor or do I have to handle the conversion to Big endian myself?
    I have also read that network communication is normally Big endian. Another question is: the data I send through the serial line will be Big endian (like Labview) or Little endian (like Intel processor) or it doesn't depend on them and handles itself the endianness of the data?
    For the serial line I am using a RSR-232 bus.
    Thanks very much!

    As you can see from the VISA Write and VISA Read functions they have String as input and output. And these functions are not polymorph so you can use only this type.
    There are only two functions with which you can convert data to String. One is the "Byte Array to String" in the String >> String/Array/Path conversion palette. In this case the bytes must be already in the correct order. The other is the "Flatten to String" in the Advanced >> Data Manipulation palette.
    Using this function you will get flattened data. This data is always in big endian form. This data will be converted correctly into the representation the machine has which runs the "Unflatten from string" function.
    Think of sending a cluster of a I16 followed by a I32. The I16 has the value 0x0102 and the I32 has the value 0x11121314.
    Using "Flatten to string" you get the data string containing 0x01 0x02 0x11 0x12 0x13 0x14. This is the Big endian representation. If you need the Litte endian representation you have to unbundle the cluster, split number and build array.
    The VISA functions do not have any type information so they do not know which bytes/words to swap.
    If you have the bookshelf installed go to Help>>Search the LabVIEW bookshelf >> Application Notes and white papers >> LabVIEW Data Storage to learn about the Flatten and Unflatten functions and how LabVIEW generates the data strings.
    So connecting two LabVIEW applications with serial line and using flatten and unflatten you do not need to worry about endianness because these functions do this for you. If you connect a LabVIEW application together with a non LabVIEW application you have to provide the correct endianness by yourself if you do binary transfers. You can format your data into a string with the string functions and scan the string on the receiving part. This way you will transfer text and you do not have to worry about endianness because the format and scan function will do this for you.
    The Byte order attributes are only valid in the VXI classes and are used for low level memory access of VXI devices. This is described in the NI-VISA programmers reference manual which is installed with your VISA.
    Waldemar
    Waldemar
    Using 7.1.1, 8.5.1, 8.6.1, 2009 on XP and RT
    Don't forget to give Kudos to good answers and/or questions

Maybe you are looking for

  • Database update failed for some organizations when installing Update Rollup 1 for Microsoft Dynamics CRM 2013 Service Pack 1

    Hi,  We get the following error in the logfile when we try to install the latest update for CRM: (KB2953252) Does anyone know how to fix this problem?  09:10:41|   Info| Database update install failed for orgId = 35e7ca08-43fb-4440-ba18-acfc3f42e115.

  • Looking for the new plugin for sony alpha r7ii

    anyone know the download whereabouts for new camera raw plugin?

  • Parallel ledger for tax purposes

    Dear SAPiens, I heard an idea from a client: to use a parallel ledger in newGL for profit tax purposes, i.e. to record taxable revenues and deductible expenses in a ledger dedicated for tax purposes, to make it easy to determine the base of profit ta

  • 1024 x 768 not on Adobe Proto?

    Am I missing something? Adobe creates an app to prototype web sites and I would think for mobile devices as well. I pay $9.99 for this app on my iPad. Yet there's no 1024 x 768 size to wire frame for. Really? I'm sure there's a way to do it, can some

  • Won't even boot from CD?

    Hi, I've got a G4 MMD powermac, and recently is decided to stop booting up. I get a boot beep and it will go to the gray apple screen, but I never get that little grey spinning wheel thing under the Apple. It just sits there forever. After reading so