Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

Convert int to Char c++

Original Message
Name: Skips
Date: April 7, 2005 at 14:50:33 Pacific
Subject: Convert int to Char c++
OS: XP SP1a
CPU/Ram: AMD 1.8Ghz
Comment:
I have a program which im writing that will count up ip addresses starting from 1.0.0.1. Each octet of the ip address is assigned as an int.

ie int oct1, int oct2

I then want to assign these 4 values to make one address which is assigned as a char.

My program keeps telling my cannot convert int to char.

Any ideas?
Cheers



Report Offensive Message For Removal


Response Number 1
Name: Skips
Date: April 7, 2005 at 14:52:11 Pacific
Subject: Convert int to Char c++
Reply: (edit)
To add:

I know you can use sprintf to print it, but im not actually wanting to print it.


Report Offensive Follow Up For Removal

Response Number 2
Name: StuartS
Date: April 7, 2005 at 15:25:04 Pacific
Subject: Convert int to Char c++
Reply: (edit)
Char is 8 bits. Int is 16 bits. Therefore you cannot get 16 bits into 8 bits without data overflow.

So why not use Char to begin with. A char can have maximum value of 255 which is all you need for an IP octet.

The full IPA address is 32 bits which needs a Long Int. You can put 4 char data types into a Long Int.

Alternatively just use a Long Int and use bit masking to isolate the four octets.

Stuart


Report Offensive Follow Up For Removal

Response Number 3
Name: SteveCary55
Date: May 4, 2005 at 22:56:18 Pacific
Subject: Convert int to Char c++
Reply: (edit)
try this out...sprintf doesn't print to an output device, it alters a variable. This made my laptop screen get all frizzy when it was running...it's not my fault if you blow something up.
I made cout print to the screen so you could see what was going in...replace that with what you want to do with the ipAddress variable.


#include <iostream>
using namespace std;

int main()
{
char ipAddress[16] = " ";
int oct1 = 1;
int oct2 = 0;
int oct3 = 0;
int oct4 = 1;

while (oct1 <= 255)
{
while (oct2 <= 255)
{
while (oct3 <= 255)
{
while (oct4 <= 255)
{
sprintf(ipAddress, "%d.%d.%d.%d", oct1, oct2, oct3, oct4);
cout << endl << ipAddress;
oct4++;
}
oct3++;
oct4 = 0;
}
oct2++;
oct3 = 0;
}
oct1++;
oct2 = 0;
}

cin.get();
cin.get();
return 0;
}


Report Offensive Follow Up For Removal




Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Convert int to Char c++

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




CPU and Graphics Upgrade Questions

VIRUS ALERT in Taskbar, HELP!

DSHUB24 Connection Problems

need help with dsl and dial up

novel 3.12


The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC