473,387 Members | 1,485 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,387 software developers and data experts.

How to convert this

I have a 16-bit port no. This address information is broken into 8-bit
fields and the value of each field is transmitted as a decimal number
(in character string representation , eg: "177,147")

How do I get the port no from this..??

thanks
Sunit
Nov 16 '05 #1
6 1621
int portnumber = (177<<8) | 147;

or the other way around depending on the endianess.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
"Sunit Joshi" <sj****@ingr.com> schrieb im Newsbeitrag
news:8f**************************@posting.google.c om...
I have a 16-bit port no. This address information is broken into 8-bit
fields and the value of each field is transmitted as a decimal number
(in character string representation , eg: "177,147")

How do I get the port no from this..??

thanks
Sunit

Nov 16 '05 #2

"Sunit Joshi" <sj****@ingr.com> wrote in message
news:8f**************************@posting.google.c om...
I have a 16-bit port no. This address information is broken into 8-bit
fields and the value of each field is transmitted as a decimal number
(in character string representation , eg: "177,147")

How do I get the port no from this..??


Assuming 177 is the most significant byte,

string littleEnd = "147";
string bigEnd = "177";

int port = int.Parse(littleEnd) | int.Parse(bigEnd) << 8;

David
Nov 16 '05 #3
Hi Sunit,

First use String.Split method to get each parts of the string.

Then using int.Parse create the numerical representation of these parts.
and then depending on the order of which they come calculate the value

string a = "171,205";
string []parts = a.Split(',');
int part1 = int.Parse(parts[0]);
int part2 = int.Parse(parts[1]);

int res = part1*256 + part2;
//if the most segnificant bits are in part2 then part2*256 + part1;
--

HTH
Stoitcho Goutsev (100) [C# MVP]
"Sunit Joshi" <sj****@ingr.com> wrote in message
news:8f**************************@posting.google.c om...
I have a 16-bit port no. This address information is broken into 8-bit
fields and the value of each field is transmitted as a decimal number
(in character string representation , eg: "177,147")

How do I get the port no from this..??

thanks
Sunit

Nov 16 '05 #4
Thanks for all the replies...could someone please explain to me how
they arrived at this..??

thanks
Sunit

"Stoitcho Goutsev \(100\) [C# MVP]" <10*@100.com> wrote in message news:<uD**************@TK2MSFTNGP10.phx.gbl>...
Hi Sunit,

First use String.Split method to get each parts of the string.

Then using int.Parse create the numerical representation of these parts.
and then depending on the order of which they come calculate the value

string a = "171,205";
string []parts = a.Split(',');
int part1 = int.Parse(parts[0]);
int part2 = int.Parse(parts[1]);

int res = part1*256 + part2;
//if the most segnificant bits are in part2 then part2*256 + part1;
--

HTH
Stoitcho Goutsev (100) [C# MVP]
"Sunit Joshi" <sj****@ingr.com> wrote in message
news:8f**************************@posting.google.c om...
I have a 16-bit port no. This address information is broken into 8-bit
fields and the value of each field is transmitted as a decimal number
(in character string representation , eg: "177,147")

How do I get the port no from this..??

thanks
Sunit

Nov 16 '05 #5
From your question, of course.

You said that you were being given a 16 bit number in two 8-bit groups. So,
we recreated a 16 bit number. You take the big end and either left-shift it
8 bits or multiply by 256 (which does the same thing)... then either add the
little end or OR the little end in (since a bit shift leaves the low order
bits as zero, OR produces the same effect as addition).

This is elementary binary math.

--- Nick

"Sunit Joshi" <sj****@ingr.com> wrote in message
news:8f**************************@posting.google.c om...
Thanks for all the replies...could someone please explain to me how
they arrived at this..??

thanks
Sunit

"Stoitcho Goutsev \(100\) [C# MVP]" <10*@100.com> wrote in message

news:<uD**************@TK2MSFTNGP10.phx.gbl>...
Hi Sunit,

First use String.Split method to get each parts of the string.

Then using int.Parse create the numerical representation of these parts.
and then depending on the order of which they come calculate the value

string a = "171,205";
string []parts = a.Split(',');
int part1 = int.Parse(parts[0]);
int part2 = int.Parse(parts[1]);

int res = part1*256 + part2;
//if the most segnificant bits are in part2 then part2*256 + part1;
--

HTH
Stoitcho Goutsev (100) [C# MVP]
"Sunit Joshi" <sj****@ingr.com> wrote in message
news:8f**************************@posting.google.c om...
I have a 16-bit port no. This address information is broken into 8-bit
fields and the value of each field is transmitted as a decimal number
(in character string representation , eg: "177,147")

How do I get the port no from this..??

thanks
Sunit

Nov 16 '05 #6
I posted an answer to this question, but I cannot see the answer in my
newsreader... so I'll post another answer. If you get both: sorry!

Binary math: each binary digit is worth two times more than the position
below it (just like decimal math). So, to more one bit, you multiply by 2.
To move 8 bits, you multiply by 256 (or simply shift the bits over 8
positions).

So, if you take the byte that represents the 'big end' of the number, put it
into a container that is 16 bits wide, it will be placed in the lower 8
bits. That's wrong... it needs to be in the upper 8 bits... so shift it out
of the way. The lower 8 bits will be left containing zeros (trust me). You
can either add the lower 8 bits in, or use the OR operator. Either way, you
get a single, 16 bit number that contains all the bits in all the right
places.

Does that explain it?

--- Nick

"Sunit Joshi" <sj****@ingr.com> wrote in message
news:8f**************************@posting.google.c om...
Thanks for all the replies...could someone please explain to me how
they arrived at this..??

thanks
Sunit

"Stoitcho Goutsev \(100\) [C# MVP]" <10*@100.com> wrote in message

news:<uD**************@TK2MSFTNGP10.phx.gbl>...
Hi Sunit,

First use String.Split method to get each parts of the string.

Then using int.Parse create the numerical representation of these parts.
and then depending on the order of which they come calculate the value

string a = "171,205";
string []parts = a.Split(',');
int part1 = int.Parse(parts[0]);
int part2 = int.Parse(parts[1]);

int res = part1*256 + part2;
//if the most segnificant bits are in part2 then part2*256 + part1;
--

HTH
Stoitcho Goutsev (100) [C# MVP]
"Sunit Joshi" <sj****@ingr.com> wrote in message
news:8f**************************@posting.google.c om...
I have a 16-bit port no. This address information is broken into 8-bit
fields and the value of each field is transmitted as a decimal number
(in character string representation , eg: "177,147")

How do I get the port no from this..??

thanks
Sunit

Nov 16 '05 #7

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

Similar topics

1
by: Logan X via .NET 247 | last post by:
It's official....Convert blows. I ran a number of tests converting a double to an integer usingboth Convert & CType. I *ASSUMED* that CType would piggy-back ontop of Convert, and that performance...
4
by: Eric Lilja | last post by:
Hello, I've made a templated class Option (a child of the abstract base class OptionBase) that stores an option name (in the form someoption=) and the value belonging to that option. The value is...
3
by: Convert TextBox.Text to Int32 Problem | last post by:
Need a little help here. I saw some related posts, so here goes... I have some textboxes which are designed for the user to enter a integer value. In "old school C" we just used the atoi function...
2
by: fabrice | last post by:
Hello I'm getting an error during a .vb file compilation. My command is : vbc /t:library /r:system.web.dll /r:system.dll /r:mscorlib.dll myFile.vb The error is :
7
by: patang | last post by:
I want to convert amount to words. Is there any funciton available? Example: $230.30 Two Hundred Thirty Dollars and 30/100
2
by: SimonZ | last post by:
Hi, can someone explain me, when to use: (DateTime)DataBinder.Eval(Container.DataItem, "dateField") OR Convert.ToDateTime(DataBinder.Eval(Container.DataItem, "dateField")) Sometimes...
17
by: Terry Jolly | last post by:
New to C# ---- How do I convert a Date to int? In VB6: Dim lDate as long lDate = CLng(Date) In C#
1
by: Wes Peters | last post by:
I have a situation where I want to filter records for a given day. The field that stores the date/time uses the date() function as a default value, kind of a date/time stamp for the record. The...
1
by: johnlim20088 | last post by:
Hi, Currently I have 6 web projects located in Visual Source Safe 6.0, as usual, everytime I will open solution file located in my local computer, connected to source safe, then check out/check in...
4
by: tshad | last post by:
I am trying to convert a string character to an int where the string is all numbers. I tried: int test; string stemp = "5"; test = Convert.ToInt32(stemp); But test is equal to 53.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.