LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 04-10-2013, 07:17 PM   #1
ThePedge
LQ Newbie
 
Registered: Apr 2013
Posts: 7

Rep: Reputation: Disabled
Can anyone help convert this c# code into SSH?


Basically I know nothing about SSH but need this for a uni assessment, I'm not asking for straight-up code unless you feel like it but I need to convert a netmask into binary. To try help myself I whipped up a quick c# version, code is messy I appreciate but if anyone can give me some tips I'll be very grateful.

Here is the kind of thing I need to do:
EDIT: All of my indentation has been lost in the quote making it hard to read, sorry.
Quote:
string netmask = "255.255.255.0";
string[] numStrings;
string binaryTemp = "";
string binary = "";

private void Loop()
{
numStrings = netmask.Split('.');
for (int i = 0; i < 4; i++)
{
int number = int.Parse(numStrings[i]);

if (number == 0) binaryTemp += "0";

while (number != 0)
{
int tempNum = number % 2;
string temp = binaryTemp;
if (tempNum == 1)
{
binaryTemp = "1" + temp;
}
else
{
binaryTemp = "0" + temp;
}
number = number / 2;
}
binary += binaryTemp + ".";
binaryTemp = "";
}
Console.WriteLine(binary);
}

static void Main(string[] args)
{
Program prog = new Program();
prog.Loop();
}
I have the netmask in my SSH program in array form so I plan on using a for loop 0 through 3 then with a while loop like I have done so here. Just any tips on syntax or even help improve the method would be so helpful.

Thank you for your time.

Last edited by ThePedge; 04-10-2013 at 07:19 PM.
 
Old 04-11-2013, 02:43 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975
what is any of this for??? You've not explained what you want to achieve... Converting C# into SSH? That makes as much as sense (to me at least) as converting it into a sandwich...
 
Old 04-11-2013, 09:02 AM   #3
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 19,176

Rep: Reputation: 4401Reputation: 4401Reputation: 4401Reputation: 4401Reputation: 4401Reputation: 4401Reputation: 4401Reputation: 4401Reputation: 4401Reputation: 4401Reputation: 4401
Quote:
Originally Posted by ThePedge View Post
Basically I know nothing about SSH but need this for a uni assessment, I'm not asking for straight-up code unless you feel like it but I need to convert a netmask into binary. To try help myself I whipped up a quick c# version, code is messy I appreciate but if anyone can give me some tips I'll be very grateful.

Here is the kind of thing I need to do: I have the netmask in my SSH program in array form so I plan on using a for loop 0 through 3 then with a while loop like I have done so here. Just any tips on syntax or even help improve the method would be so helpful.
If this is for a university assignment, did you try to look up how to do this?? Putting "convert decimal to binary in Linux" into Google yields LOTS of results. For a bash script, just:
Code:
echo "obase=2; 255" | bc
Done. 255 decimal is equal to 11111111 in binary. But I agree with acid_kewpie...your question makes no sense at all. SSH is a protocol, not a programming language. It provides terminal and file transfer capabilities via an encrypted tunnel, it's not a scripting or programming language.
 
Old 04-11-2013, 11:05 AM   #4
ThePedge
LQ Newbie
 
Registered: Apr 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by TB0ne View Post
If this is for a university assignment, did you try to look up how to do this?? Putting "convert decimal to binary in Linux" into Google yields LOTS of results. For a bash script, just:
Code:
echo "obase=2; 255" | bc
Done. 255 decimal is equal to 11111111 in binary. But I agree with acid_kewpie...your question makes no sense at all. SSH is a protocol, not a programming language. It provides terminal and file transfer capabilities via an encrypted tunnel, it's not a scripting or programming language.
Not going to lie we weren't told anything basically, lecturers were off ill and we only got 4 lectures for this little networking assignment in the first place. I'm sorry if my question made no sense, at least you've managed to help!
The was I made a C# version of the code was the way were suggested to do so. I have next to no knowledge of Linux, bash script & it's syntax and didn't know about that simple way, I should probably research more beforehand next time.
Thank you anyway, I have progressed a lot now and you helped.
 
Old 04-11-2013, 11:27 AM   #5
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 19,176

Rep: Reputation: 4401Reputation: 4401Reputation: 4401Reputation: 4401Reputation: 4401Reputation: 4401Reputation: 4401Reputation: 4401Reputation: 4401Reputation: 4401Reputation: 4401
Quote:
Originally Posted by ThePedge View Post
Not going to lie we weren't told anything basically, lecturers were off ill and we only got 4 lectures for this little networking assignment in the first place. I'm sorry if my question made no sense, at least you've managed to help!
The was I made a C# version of the code was the way were suggested to do so. I have next to no knowledge of Linux, bash script & it's syntax and didn't know about that simple way, I should probably research more beforehand next time.
Thank you anyway, I have progressed a lot now and you helped.
I'm glad it helped, and your lecturer really should be chastised for not providing better lessons/information. Really, that's a huge failure on their part, and good for you for taking initiative on getting it solved.
 
Old 04-11-2013, 08:49 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,406

Rep: Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396Reputation: 2396
Given you are new to Linux, these may be useful
http://rute.2038bug.com/index.html.gz
http://linux.die.net/man/
www.linuxtopia.org

Welcome to LQ
 
Old 04-12-2013, 09:27 AM   #7
ThePedge
LQ Newbie
 
Registered: Apr 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by TB0ne View Post
Code:
echo "obase=2; 255" | bc
Is there a way of storing the binary value as an integer? I need then to count the number of 1's and number of 0's inthe binary outcome.
for example I have a netmask of 255.255.255.0 each quarter is stored in an array of netmask[0] to [3].
if theyre a way I can store
Code:
"obase=2; ${netmask[0]}"
as a variable?
 
Old 04-12-2013, 05:17 PM   #8
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,707

Rep: Reputation: 1270Reputation: 1270Reputation: 1270Reputation: 1270Reputation: 1270Reputation: 1270Reputation: 1270Reputation: 1270Reputation: 1270
bc is short for "basic calculator", but bc is a lot more - it has within it a programming language than can be used for anything you want (and nearly any size you want - try sqrt(2^400)

And there are a number of algorithms on the net for computing the number of ones in a binary number.
 
Old 04-13-2013, 08:22 AM   #9
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,147

Rep: Reputation: 887Reputation: 887Reputation: 887Reputation: 887Reputation: 887Reputation: 887Reputation: 887
Quote:
Originally Posted by ThePedge View Post
Is there a way of storing the binary value as an integer? I need then to count the number of 1's and number of 0's inthe binary outcome.
for example I have a netmask of 255.255.255.0 each quarter is stored in an array of netmask[0] to [3].
if theyre a way I can store
Code:
"obase=2; ${netmask[0]}"
as a variable?
something like:
Code:
var="obase=2; ${netmask[0]}"
 
Old 04-13-2013, 09:33 AM   #10
ThePedge
LQ Newbie
 
Registered: Apr 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by schneidz View Post
something like:
Code:
var="obase=2; ${netmask[0]}"
The output for that just comes out as
Code:
obase=2; 255
so I think it's saving the code as a string and not executing it first.
 
Old 04-13-2013, 08:13 PM   #11
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,147

Rep: Reputation: 887Reputation: 887Reputation: 887Reputation: 887Reputation: 887Reputation: 887Reputation: 887
isn't that what you asked for ?
what command are you trying to save the output for (this is the output on my fedora machine) :
Code:
[schneidz@hyper stuff]$ obase
bash: obase: command not found...
edit: i tried to read thru the last few posts; is this what you are after:
Code:
[schneidz@hyper stuff]$ echo "obase=2; 255" | bc
11111111
[schneidz@hyper stuff]$ var=`echo "obase=2; 255" | bc`
[schneidz@hyper stuff]$ echo var = $var
var = 11111111
editorial: this whole thread is confusing; you need to ask questions so that people understand them else you'll get the wrong answer (actually you are getting the right answer but the questions were wrong).

Last edited by schneidz; 04-13-2013 at 08:25 PM.
 
Old 04-14-2013, 02:04 PM   #12
ThePedge
LQ Newbie
 
Registered: Apr 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by schneidz View Post
]
Code:
[schneidz@hyper stuff]$ echo "obase=2; 255" | bc
11111111
[schneidz@hyper stuff]$ var=`echo "obase=2; 255" | bc`
[schneidz@hyper stuff]$ echo var = $var
var = 11111111
I've tried this but I dont know if it's the fact I'm not directly telling it the number but this;
Code:
var = 'echo "obase=2; $netmask[0]" | bc'
echo $var
$netmask[0] is 255 and it gives me the output
Code:
obase=2; 255
The reason I need it as a variable is because I need to count the number of 1's and then the number of 0's in the binary version of the entire netmask. Without saving it as variables it displays but I can't use that to count can I?
 
Old 04-14-2013, 04:13 PM   #13
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,147

Rep: Reputation: 887Reputation: 887Reputation: 887Reputation: 887Reputation: 887Reputation: 887Reputation: 887
^ copy and paste fail.

those are `backticks` not 'single quotes'.

also, no space between the variable name and the assignment.
 
Old 04-15-2013, 02:30 AM   #14
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975Reputation: 1975
Quote:
Originally Posted by schneidz View Post
^ copy and paste fail.

those are `backticks` not 'single quotes'.

also, no space between the variable name and the assignment.
another good reason to use $(...) instead of `...`
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Small matlab code to convert Morse code to plaintext using regular expressions gregorian Programming 3 03-31-2010 12:34 PM
pls convert this windows code into linux code nagendrar Programming 6 06-03-2008 07:00 AM
How to convert Assembly code to "C" source code ssg14j Programming 2 08-01-2005 12:48 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:24 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration