Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: how to read c++ unsigned long in java

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default how to read c++ unsigned long in java

    C++ datatypes using this below structure ,i have to read the file.But the problem im not getting the proper values unsigned long
    short
    unsigned long
    char[8]
    char[8]
    char[16]
    short
    short
    long
    long
    This is the logic i am using to read unsigned long in java
    private static final int bytemask = 0x0000ff;
    private static final long intmask = 0x00000000ffffffffL;

    long readUnsignedLong(DataInputStream in) throws IOException {
    int signed = readLong(in);
    long unsigned = signed & intmask;
    return unsigned;
    }
    public int readLong(DataInputStream in) throws IOException {
    int bytemask = 0x000000ff;
    byte b1 = in.readByte();
    byte b2 = in.readByte();
    byte b3 = in.readByte();
    byte b4 = in.readByte();
    return makeCppSignedLong(b1, b2, b3, b4);
    }
    int makeCppSignedLong(byte b1, byte b2, byte b3, byte b4) {
    int a1 = bytemask & b1;
    int a2 = bytemask & b2;
    int a3 = bytemask & b3;
    int a4 = bytemask & b4;
    return ((a4) << 24) + (a3 << 16) + (a2 << 8) + a1;
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: how to read c++ unsigned long in java

    im not getting the proper values unsigned long
    Please post the input value and its results.

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to read c++ unsigned long in java

    Thanks for quick reply

    DataInputStream input = new DataInputStream(new FileInputStream("-----\\sample.mdx"));

    First we are reading the header later readUnsignedLong(DataInputStream in),i mentioned in the above post
    ------------------------------------------------
    Long ObjectId = readUnsignedLong(input);
    -------------------------------------------------
    Output--UnsignedLong----------------3114199

    expected output value ---3435927511
    Last edited by satyendra.kumar; July 10th, 2014 at 08:05 AM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: how to read c++ unsigned long in java

    Sorry, I don't understand how you get 3435927511 from 40? Please explain.
    Also what are the ---- shown in the post for?

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    May 2014
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: how to read c++ unsigned long in java

    Thanks for update,

    how you get 3435927511 from 40?

    40 is the first reading short value from the DataInputStream.

    3435927511 is the second reading unsignedlong from the DataInputStream.

    I have input mdx file which i am reading through this below ,what functionality is trying convert c++ unsignedlong to signedlong in java

    DataInputStream input = new DataInputStream(new FileInputStream("sample.mdx"));

    Short st=input.readShort();
    ------------------------------------------------
    long ObjectId = readUnsignedLong(input);
    -------------------------------------------------

    This is the method i am using to read unsignedLong
    =================================
    long readUnsignedLong(DataInputStream in) throws IOException {
    int bytemask = 0x000000ff;
    byte b1 = in.readByte();
    byte b2 = in.readByte();
    byte b3 = in.readByte();
    byte b4 = in.readByte();
    int a1 = bytemask & b1;
    int a2 = bytemask & b2;
    int a3 = bytemask & b3;
    int a4 = bytemask & b4;
    return ((a4) << 24) + (a3 << 16) + (a2 << 8) + a1;

    }

    ==========================================
    while running
    Output:
    40--these reading from short of DataInputStream
    3114199---------method readunsignedlong(DataInputStream in) ,so i am getting Output for UnsignedLong value for DataInputStream.

    But exact output value i have to get this 3435927511 value, while i crosschecked Database for above Sample.mdx
    ------------------
    Last edited by satyendra.kumar; July 10th, 2014 at 11:31 PM.

  6. #6
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    196
    My Mood
    Sleepy
    Thanks
    17
    Thanked 32 Times in 28 Posts

    Default Re: how to read c++ unsigned long in java

    Is this an assignment for college? It is quite a strange program to design.

    All data types are signed by default - meaning they can be negative. C++ used
    the unsigned keyword to ensure a value could never be negative.

    byte b1 = in.readByte();
    byte b2 = in.readByte();
    byte b3 = in.readByte();
    byte b4 = in.readByte();
    int a1 = bytemask & b1;
    int a2 = bytemask & b2;
    int a3 = bytemask & b3;
    int a4 = bytemask & b4;
    return ((a4) << 24) + (a3 << 16) + (a2 << 8) + a1;
    If I am understanding that correctly each of the integer variables labelled 'a'
    are assigned the reference of byte through the bytemask not sure if this is
    a type of template system or not.

    In C++, '<<' was the insertion operator that would read data along the output stream.
    I am not very versed in byte code - but from what I can gather is the reason that your
    output is not correct is perhaps the references that are assigned to each 'a' variable are
    containing incorrect values? Just an insight and I could be totally wrong - but I guess it
    might be worth a check. Java does use a byte-code verifier and a class loader - again,
    the bytecode statement looks like a mask for something.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: how to read c++ unsigned long in java

    convert c++ unsignedlong to signedlong in java
    Can you show what the data input looks like in hex bytes?
    For example if the value is 40
    is it this: 0x2800000000000000
    or this: 0x0000000000000028
    or something else
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 47
    Last Post: December 4th, 2013, 11:46 AM
  2. Converting a piece of C# code to Java (unsigned longs, etc)
    By nahkiss in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 10th, 2013, 12:44 PM
  3. Replies: 6
    Last Post: January 21st, 2013, 08:13 PM
  4. Signed & unsigned
    By Senovit in forum Java Theory & Questions
    Replies: 9
    Last Post: September 11th, 2012, 10:59 PM
  5. Socket connections and Unsigned values
    By helloworld922 in forum Java Networking
    Replies: 5
    Last Post: June 15th, 2010, 08:48 PM

Tags for this Thread