4byte - value to string (like IPAddress.ToString())

O

ohmmega

hello,

i've a 4 character string stored in a 4byte value (lets say an 32 bit
integer).
the integer holds the value:
char[3]*255^3+char[2]*255^2+char[1]*255^1+char[0]*255^0.

how can i convert this integer back to string?

thx
rené
 
J

Jon Skeet [C# MVP]

i've a 4 character string stored in a 4byte value (lets say an 32 bit
integer).
the integer holds the value:
char[3]*255^3+char[2]*255^2+char[1]*255^1+char[0]*255^0.

how can i convert this integer back to string?

You should be *very* clear about the differences between characters
and bytes. In .NET, each char is represented as *two* bytes (a UTF-16
code point).

Now, if you mean you've got an integer that you want to be converted
into a byte array and then treated as ASCII text, you can use
BitConverter to go from the integer to a byte array, then
Encoding.ASCII to go from the byte array to text.

Jon
 
O

ohmmega

You should be *very* clear about the differences between characters
and bytes. In .NET, each char is represented as *two* bytes (a UTF-16
code point).

Now, if you mean you've got an integer that you want to be converted
into a byte array and then treated as ASCII text, you can use
BitConverter to go from the integer to a byte array, then
Encoding.ASCII to go from the byte array to text.

Jon

usually i'm aware about the difference - anyway thank's for the
casting conclusion.
so: yes, i meant ASCII text and i've made a small method for my prob:
<code>
private string IntToString(int intVal)
{
byte[] bVal = BitConverter.GetBytes(intVal);
System.Text.ASCIIEncoding asciiEncoding = new
System.Text.ASCIIEncoding();
return asciiEncoding.GetString(bVal);
}
</code>

work's great - thank's for the thought-provoking impulse :)
 
J

Jon Skeet [C# MVP]

so: yes, i meant ASCII text and i've made a small method for my prob:
<code>
private string IntToString(int intVal)
{
byte[] bVal = BitConverter.GetBytes(intVal);
System.Text.ASCIIEncoding asciiEncoding = new
System.Text.ASCIIEncoding();
return asciiEncoding.GetString(bVal);}

</code>

work's great - thank's for the thought-provoking impulse :)

One thing you might want to change - there's no need to create a new
ASCIIEncoding each time. Just use:

return Encoding.ASCII.GetString(bVal);

Jon
 
O

ohmmega

so: yes, i meant ASCII text and i've made a small method for my prob:
<code>
private string IntToString(int intVal)
{
byte[] bVal = BitConverter.GetBytes(intVal);
System.Text.ASCIIEncoding asciiEncoding = new
System.Text.ASCIIEncoding();
return asciiEncoding.GetString(bVal);}

work's great - thank's for the thought-provoking impulse :)

One thing you might want to change - there's no need to create a new
ASCIIEncoding each time. Just use:

return Encoding.ASCII.GetString(bVal);

Jon

oops :)

that's really fine
thx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top