473,320 Members | 1,977 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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é

Jul 25 '07 #1
4 2886
On Jul 25, 9:43 am, ohmmega <sho...@gmx.atwrote:
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

Jul 25 '07 #2
On 25 Jul., 10:45, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
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 :)

Jul 25 '07 #3
On Jul 25, 10:55 am, ohmmega <sho...@gmx.atwrote:

<snip>
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

Jul 25 '07 #4
On 25 Jul., 11:59, "Jon Skeet [C# MVP]" <sk...@pobox.comwrote:
On Jul 25, 10:55 am, ohmmega <sho...@gmx.atwrote:

<snip>
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
oops :)

that's really fine
thx

Jul 25 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Arvid Andersson | last post by:
Hello, I need to convert a string to a number, but the string can contain +,-,* and / as well as parenthesis. For example, if I have the string "30/(6+9)" I would like a function that returned the...
1
by: LRW | last post by:
Below is some of the code I'm using. I have a PHP page generating a list of items. I've made it so that each one has a radiobutton with a unique value. You can click on the radiobutton and it will...
5
by: KB | last post by:
Hi, This may be a rudimentary question: How to convert a string like '777' to an octal integer like 0777, so that it can be used in os.chmod('myfile',0777)? I know the leading zero is...
19
by: linzhenhua1205 | last post by:
I want to parse a string like C program parse the command line into argc & argv. I hope don't use the array the allocate a fix memory first, and don't use the memory allocate function like malloc....
1
by: Bell, Kevin | last post by:
I'm building a dictionary from values a database and upon print the dictionary I see key value pairs like this: u'Briarcliff' : , u'Shumway' : do I need to slice off the "u", or anything? I...
4
by: Akihiro KAYAMA | last post by:
Hi all. I would like to ask how I can implement string-like class using tuple or list. Does anyone know about some example codes of pure python implementation of string-like class? Because I...
10
by: Zontar | last post by:
I'm trying to improve performance on a query, and I was wondering if this is possible in Access. Let's say I have a table with one text column and one row. In that column, I have a field name...
7
by: mark4asp | last post by:
How can I use name:value pairs like an array? 1. I want to load data on to the page which will be used to populate a list box (see example below): <html> <head> <script...
2
by: chuckzter | last post by:
I'm in a bit of a pickle here. I need to pass the variable @uname which gets a value from a text box to a statement on one of my stored procedures in SQL server and return an output on my form. ...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.