2

First I receive the data and convert it to string.

data = client.Receive(ref ep);
string received = BitConverter.ToString(data);

The string I get is FF-FF-FF-FF-66-0A. And I try to get INT values from it.

foreach (var item in received)
{
    int rec = Convert.ToInt32(item);
    int rec = Convert.ToInt32(item,16); 

    //IF I try second line I get error
    //cannot convert from 'int' to 'system.iformatprovider' 
}

From using that first line int rec = Convert.ToInt32(item); I get numbers like this 70 70 70 70 45 45 70 70 70 70

As I figured it out I'm converting F > 70 and F > 70, but how to convert FF and make it work by getting FF > 255

6
  • Are you sure you shouldn't be doing BitConverter.ToInt32(data, 0)? Commented May 19, 2016 at 8:04
  • Just tried doing so, my data I get becomes -1. And the data I get before is good Commented May 19, 2016 at 8:07
  • Well an int is 32 bits, but that hex number you have is 48 bits. Do you just want to ignore the extra bits at the end? Commented May 19, 2016 at 8:09
  • 1
    @MatthewWatson I think he only wants the single numbers, not one long number. Commented May 19, 2016 at 8:10
  • @Master117 Ah right - I wonder why he wants bytes stored as ints rather than just bytes... Commented May 19, 2016 at 8:12

4 Answers 4

7

here you go

string input = "FF-FF-FF-FF-66-0A";
int[] result = input.Split('-').Select(x => Convert.ToInt32(x, 16)).ToArray();
Sign up to request clarification or add additional context in comments.

2 Comments

if I could ask one more question. FF-FF-FF-FF - that's is IP address, 66-0A - that is Port. So is it possible to connect 66 and 0A before converting them? Now I have made so after FF it adds .(dot) and increase count by 1 so I would no when it reaches port. But how to combine them.
sure with string ip_port = string.Join("-", ip, port);
1

Use ToInt32(x,16);

string[] hexValuesSplit = received.Split('-');
foreach (String hex in hexValuesSplit)
{
  // Convert the number expressed in base-16 to an integer.
  int value = Convert.ToInt32(hex, 16);
  Console.WriteLine("hexadecimal value = {0}, int value = {1}",
                    hex, value);
}

MSDN Article

Comments

1

Here's an alternative approach you might want to try.

Since you say that you're parsing IP addresses, you could do something like this:

string x = "FF-FF-FF-FF-66-0A";
var ipAddress = IPAddress.Parse(x.Replace("-", ":") + ":0:0");

Note that you are using IPv6 with the last two values 0 (I think), so I had to add ":0:0" to the end of the string before parsing it.

(Note: It's not clear what IP scheme you are actually using, but it seems to me that there should be some way for you to use the IPAddress class to parse them.)

1 Comment

I am getting them from master server, and I get them as hexadecimals
0

Use int intNumber = int.Parse("FF",system.Globalization.NumberStyle.HexNumber);

Comments

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.