1

I have an ipv4-address stored in a byte array. At index n and until index n+3 each index takes 4 bytes.

(index)              n       n+1     n+2    n+3                 
[..] [..] [..] [ 1st byte ][ 2nd ] [ 3rd ] [ 4th ] [..] [..]

how to copy this construction to string. I want to receive string like "192.168.0.1", while

byteArray[n] =   192;
byteArray[n+1] = 168;
byteArray[n+2] = 0;
byteArray[n+3] = 1;

Finally, the problem was solved and the solution is:

string str = recCommand.parameters[10] + "." + recCommand.parameters[11] +
 "." +   recCommand.parameters[12] + "." + recCommand.parameters[13];
5
  • so what is stored in tbe Byte[] array before your first octet? If its null, then you can simply look through the array checking if !null and storing the results in a string.... loop byte[] array..check !null ... += string. sorry for the crudeness of the post. Commented Jun 22, 2012 at 15:44
  • there is part of query command received via TCP, and it is not a null Commented Jun 22, 2012 at 15:45
  • 1
    @vard - What have you tried exactly? Just convert the index n through n + 3 to a string and combine them. Until you actually show what you have tried I have to down vote this question. Commented Jun 22, 2012 at 15:51
  • @Ramhound yes, that's I've tried. Here is the piece of real code: string str = recCommand.parameters[10] + "." + recCommand.parameters[11] + "." + recCommand.parameters[12] + "." + recCommand.parameters[13]; Commented Jun 22, 2012 at 15:56
  • @vard - You need to update the question with what you have attempted. Commented Jun 22, 2012 at 19:22

1 Answer 1

3

Something like this

        byte[] some = { 192, 168, 0, 1 };
        String ip = "" + some[0] + "." + some[1] + "." + some[2] + "." + some[3];
        Console.WriteLine("ip=" + ip  );
Sign up to request clarification or add additional context in comments.

2 Comments

Yeah, thanks, there was a comment by Anne with same advice, but unfortunately author deleted it. It works, great, thanks.
could be simplified to string.Format("{0}.{1}.{2}.{3}", some)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.