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 6 of 6

Thread: Converting Hex values to IP and Port Number - IP format

  1. #1
    Junior Member
    Join Date
    Jul 2019
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Converting Hex values to IP and Port Number - IP format

    Could use some help,

    I working on decoding some data from a UDP server. In the response I get a list of IP addresses and Port numbers as a byte array. I then converted them to hex strings.

    I'm confused about the format of some of the IP address once I convert them to decimal. Some of them don't seem to follow the normal IP format of xxx.xx.xxx.xx .

    Here is the code:
    public void addPeer(String addressin, String portin){
            String address = addressin;
            String port = portin;
     
            System.out.println("IP Hex: " + address);
            System.out.println("Port Hex: " + port);
            long A = Long.parseLong(address, 16);
            long B = Long.parseLong(port, 16);
            System.out.println("IP Decimal: " + A);
            System.out.println("Port Decimal: " + B);
     
        }

    ...and the Output:

    IP Hex: 7e7548f3
    Port Hex: 969f
    IP Decimal: 2121615603
    Port Decimal: 38559
     
    IP Hex: 6b4da427
    Port Hex: 2a97
    IP Decimal: 1800250407
    Port Decimal: 10903
     
    IP Hex: d995a68b
    Port Hex: d564
    IP Decimal: 3650463371
    Port Decimal: 54628
     
    IP Hex: 0539728e
    Port Hex: 5b39
    IP Decimal: 87650958
    Port Decimal: 23353

    So the 4th one down as Decimal is "87650958"... I'm confused as to what format this is and IP lookup says its not a valid IP.

    I don't how that would fit in the format xxx.xx.xxx.xx

    Am I converting them wrong or am I just getting junk values from the server?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Converting Hex values to IP and Port Number - IP format

    Where are you getting the values in the Strings: addressin and portin?

    Is there any documentation that describes what data you are receiving?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2019
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Converting Hex values to IP and Port Number - IP format

    Thanks for the reply...

    I have double checked the data from the UDP reply and it seems to be fine.

    Here is the code anyway.

    The reply Byte Array goes through a Display method that converts it into a hex string. Then to the passpeer method that pulls out the IP address and Port number and checks for trailing zeros.

    passPeers(display(bytereceived));
     
    public static String display(byte[] b1) {
            StringBuilder strBuilder = new StringBuilder();
            for(byte val : b1) {
                strBuilder.append(String.format("%02x", val&0xff));
            }
            return strBuilder.toString();
        }
     
        public void passPeers(String received){
            String hold;
            hold = received.substring(40);
            String trim = "";
            int index = 0;
            boolean flip = true;
            String address = "";
            String port = "";
     
     
            if (hold.charAt(index) == '0' && hold.charAt(index + 1) == '0') flip = false;
            while (flip){
                trim = hold.substring(index, index + 12);
                address = trim.substring(0, 8);
                port = trim.substring(8, 12);
                peers.addPeer(addressin, portin);
                index = index + 12;
                if (hold.charAt(index) == '0' && hold.charAt(index + 1) == '0') flip = false;
            }
    }

    Bellow is the raw data in the bytereceived array...
    00000001000000020000068900000000000000195e6eae8beb7f5bb471e42d925bb42fb1b23a54ec236d56c454541f5175e75450f402aa5b512c9c58f38c4f4e905900001fb791956f610539728e5b39027a659f2327c2b11c8b44cabe66468a349db9f204746acdb9955a26c74ba078b9f2e475a0780551000000000000000000000...

    Here is a link to the protocol https://www.libtorrent.org/udp_tracker_protocol.html. The above is an announcing reply packet.

    The peer list of IP and Port number starts at char(40).

    I have this as the output
    IP Hex: 5e6eae8b
    Port Hex: eb7f
    IP Decimal: 1584311947
    Port Decimal: 60287
    IP Hex: 5bb471e4
    Port Hex: 2d92
    IP Decimal: 1538552292
    Port Decimal: 11666
    IP Hex: 5bb42fb1
    Port Hex: b23a
    IP Decimal: 1538535345
    Port Decimal: 45626
    IP Hex: 54ec236d
    Port Hex: 56c4
    IP Decimal: 1424761709
    Port Decimal: 22212
    IP Hex: 54541f51
    Port Hex: 75e7
    IP Decimal: 1414799185
    Port Decimal: 30183
    IP Hex: 5450f402
    Port Hex: aa5b
    IP Decimal: 1414591490
    Port Decimal: 43611
    IP Hex: 512c9c58
    Port Hex: f38c
    IP Decimal: 1361878104
    Port Decimal: 62348
    IP Hex: 4f4e9059
    Port Hex: 0000
    IP Decimal: 1330548825
    Port Decimal: 0
    IP Hex: 1fb79195
    Port Hex: 6f61
    IP Decimal: 532124053
    Port Decimal: 28513
    IP Hex: 0539728e
    Port Hex: 5b39
    IP Decimal: 87650958
    Port Decimal: 23353
    IP Hex: 027a659f
    Port Hex: 2327
    IP Decimal: 41575839
    Port Decimal: 8999
    IP Hex: c2b11c8b
    Port Hex: 44ca
    IP Decimal: 3266387083
    Port Decimal: 17610
    IP Hex: be66468a
    Port Hex: 349d
    IP Decimal: 3194373770
    Port Decimal: 13469
    IP Hex: b9f20474
    Port Hex: 6acd
    IP Decimal: 3119645812
    Port Decimal: 27341
    IP Hex: b9955a26
    Port Hex: c74b
    IP Decimal: 3113572902
    Port Decimal: 51019
    IP Hex: a078b9f2
    Port Hex: e475
    IP Decimal: 2692266482
    Port Decimal: 58485
    IP Hex: a0780551
    Port Hex: 0000
    IP Decimal: 2692220241

    The error is on the 11th one down... "IP Decimal: 87650958"

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Converting Hex values to IP and Port Number - IP format

    The error is on the 11th one down... "IP Decimal: 87650958"
    What is the error?
    If there is an error message can you copy the full text and paste it here?

    Have you looked at how to convert a value to a dotted IP address? There should be 4 bytes of data.
    Take the bytes one by one and convert them to decimal values, insert a . between each two numbers to get the xxx.xxx.xxx.xxx format
    See: https://superuser.com/questions/1104...ddress/1104236

    Should 5e6eae8b convert to 94.110.174.139?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jul 2019
    Posts
    23
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Converting Hex values to IP and Port Number - IP format

    Thanks that was it...

    What about the port number. Its two bytes long. Would I just convert the two bytes together or bit by bit like the IP?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Converting Hex values to IP and Port Number - IP format

    about the port number. Its two bytes long. Would I just convert the two bytes together
    I would guess it should be treated as a number.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Can java assign hex values?
    By Vaderman2787 in forum Java Theory & Questions
    Replies: 1
    Last Post: June 23rd, 2013, 11:40 PM
  2. How to Pass Hex number as Argument to main
    By divix in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: April 2nd, 2012, 06:37 AM
  3. how to send a hex file to a serial port in java
    By Himali in forum Java Networking
    Replies: 10
    Last Post: April 1st, 2012, 03:06 PM
  4. Converting decimal number to m minutes, n.nn seconds format.
    By coreysizemore in forum Java Theory & Questions
    Replies: 1
    Last Post: January 20th, 2012, 11:37 PM
  5. Converting Hex to Decimal
    By r2ro_serolf in forum Java Theory & Questions
    Replies: 10
    Last Post: September 4th, 2011, 04:29 PM