Reputation: 11131
I have an application that is storing the IP Addresses of requests in the database as a varbinary(16) in a manner described here: Byte Array Size for a IPv6 IP Address.
I need to pass the IP Address from one server to another. For that reason, I cannot just rely on the Request object. My question is, if I have the IP Address as a byte[], how do I encode it as a string and then decode it as a byte[] again? I always get confused with the ASCII, UTF8, Unicode, etc. encodings.
Thank you so much!
Upvotes: 1
Views: 2348
Reputation: 149020
For a slightly more user-friendly string representation, you can use Base64
str = System.Convert.ToBase64String(bytes);
bytes = System.Convert.FromBase64String(str);
Upvotes: 1
Reputation: 120450
var ipString = (new IPAddress(myBytes)).ToString()
then at the other end
var addressBytes = IPAddress.Parse(ipString).GetAddressBytes();
Upvotes: 4