473,786 Members | 2,765 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1642
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.co m> schrieb im Newsbeitrag
news:8f******** *************** ***@posting.goo gle.com...
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.co m> wrote in message
news:8f******** *************** ***@posting.goo gle.com...
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(littl eEnd) | int.Parse(bigEn d) << 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.co m> wrote in message
news:8f******** *************** ***@posting.goo gle.com...
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******* *******@TK2MSFT NGP10.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.co m> wrote in message
news:8f******** *************** ***@posting.goo gle.com...
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.co m> wrote in message
news:8f******** *************** ***@posting.goo gle.com...
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******* *******@TK2MSFT NGP10.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.co m> wrote in message
news:8f******** *************** ***@posting.goo gle.com...
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.co m> wrote in message
news:8f******** *************** ***@posting.goo gle.com...
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******* *******@TK2MSFT NGP10.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.co m> wrote in message
news:8f******** *************** ***@posting.goo gle.com...
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
1787
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 would be identical. I was 100% incorrect. The code below produces the results: CType Took: 0.2187528 seconds. Convert Took: 12.187656 seconds.
4
3636
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 of the type the object is instantiated with. In my test program I have Option<std::string> and Option<long>. Here's the code for OptionBase and Option along with a small helper function. In the code are comments describing my problem, look closely...
3
10294
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 and there you have it. So I enquired and found the Convert class with it's promising ToInt32 method, great... but it doesn't work. The thing keeps throwing Format Exceptions all over the place. What is the "C#" way to do this??? code int wmin,...
2
3816
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
29245
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
2986
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 (DateTime) works sometimes not, on the other hand
17
71457
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
5232
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 problem is when I want to filter records for a given day instead of a date range. I use the CONVERT function to return just the date part of the field (101 as a style parameter) and compare that to a start and stop date (both being the same) and...
1
3602
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 some files and work on it. Let say, I want add new page to web project named websiteOrder.sln, i will open websiteOrder.sln in my local computer, connected to websiteOrder.sln located in Visual Source Safe 6.0(source safe located in another...
4
5078
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
9647
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10363
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9961
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8989
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3669
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.