> 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
ohmmega - 25 Jul 2007 10:55 GMT
> You should be *very* clear about the differences between characters
> and bytes. In .NET, each char is represented as *two* bytes (a UTF-16
[quoted text clipped - 6 lines]
>
> 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 :)
Jon Skeet [C# MVP] - 25 Jul 2007 10:59 GMT
<snip>
> so: yes, i meant ASCII text and i've made a small method for my prob:
> <code>
[quoted text clipped - 8 lines]
>
> 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
ohmmega - 25 Jul 2007 14:07 GMT
> <snip>
>
[quoted text clipped - 17 lines]
>
> Jon
oops :)
that's really fine
thx