Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / July 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ohmmega - 25 Jul 2007 09:43 GMT
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é
Jon Skeet [C# MVP] - 25 Jul 2007 09:45 GMT
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.