Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Passing data from int vector to char array
New Posts  All Forums:Forum Nav:

Passing data from int vector to char array

post #1 of 19
Thread Starter 
I have to write a program for a class that takes a floating point number and outputs a character array representing the binary conversion. For example, given the float 25.8125, my program needs to return
" 0 | 10000011 | 10011101000000000000000 " as a char array. I think my conversion process is correct, but how do I pass integers from an int vector to a char array? Here's my code. Sorry it's so long. The two places I'm trying to pass integers from my vector to my array are in blue.
Code:
#include<iostream>
  2 #include<vector>
  3 #include<cstdio>
  4 using namespace std;
  5
  6 void float2bits(float f){
  7
  8     char fb [39];
  9     int a, i, j, k, exp;
 10     vector <int> decimal(23);
 11     vector <int> dec_bin(16);
 12     vector <int> bin;
 13     vector <int> dec_bin2(16);
 14     vector <int> bin2;
 15
 16     if(f < 0){
 17         fb[0]=1;
 18         f = f*(-1);
 19     }else{
 20         fb[0]=0;
 21     }
 22
 23     fb[1] = ' ';
 24     fb[2] = '|';
 25     fb[3] = ' ';
 26     fb[12] = ' ';
 27     fb[13] = '|';
 28     fb[14] = ' ';
 29
 30     a = (int) f;
 31     f = f-a;
 32
 33     for(i=0; i < 23; i++){
 34         f = f*2;
 35         if(f >= 1){
 36             decimal[i] = 1;
 37         }else{
 38             decimal[i] = 0;
 39         }
 40     }
 41
 42     for(i=0; a!=0; i++){
 43         dec_bin[i] = a%2;
 44         a = a/2;
 45         if(a == 0){
 46             dec_bin.resize(i+1);
 47         }
 48     }
 49
 50     for(i=0, j=(dec_bin.size()-1); j>=0; i++, j--){
 51         bin[i] = dec_bin[j];
 52     }
 53
 54     exp = dec_bin.size()-1;
 55     exp = exp + 127;
 56
 57     for(i=0; exp!=0; i++){
 58         dec_bin[i] = exp%2;
 59         exp = exp/2;
 60         if(exp == 0){
 61             dec_bin2.resize(i+1);
 62         }
 63     }
 64
 65     for(i=0, j=(dec_bin2.size()-1); j>=0; i++, j--){
 66         bin2[i] = dec_bin2[j];
 67     }
 68
 69     for(i=4, j=8; i<12; i++, j--){
 70         if((j-bin2.size())>0){
 71             fb[i]='0';
 72         }else{
 73             k=0;
 74             sprintf(fb[i], "%d", bin2[k]);
 75             //fb[i]= (char) bin2[k];
 76             k++;
 77         }
 78     }
 79
 80     i= bin.size();
 81     bin.resize(i + bin2.size());
 82     for(j=0; j<bin2.size(); j++){
 83         bin[i] = bin2[j];
 84         i++;
 85     }
 86
 87     for(i=15, j=23, k=1; i<38; i++, j--, k++){
 88         if(bin.size()<j){
 89             sprintf(fb[i], "%d", bin[k]);
 90             //fb[i] = (char) bin[k];
 91         }else{
 92             fb[i] = '0';
 93         }
 94     }
 95
 96     for(i=0; i<38; i++){
 97         cout << fb[i];
 98     }
 99     cout << endl;
100 }
101
102 int main(){
103
104     float f;
105
106     cout << "Please enter a floating point decimal you would like converted to binary:  ";
107     cin >> f;
108
109     cout << "Your floating point decimal in binary is:  ";
110     float2bits(f);
111
112     return 0;
113 }
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
post #2 of 19
the itoa function might help here.

check this out:
http://www.cplusplus.com/reference/c.../cstdlib/itoa/
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
post #3 of 19
Thread Starter 
Quote:
Originally Posted by travesty View Post
the itoa function might help here.

check this out:
http://www.cplusplus.com/reference/c.../cstdlib/itoa/
I have a teacher who said not to use atoi... Is there any other way to do it?
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
post #4 of 19
For a general int to char conversion you need a loop to separate the digits of the integer and then add '0' (the ascii code of 0 which is 48) to get the ascii code of the digit. Then you can store the resulting ascii code in the char array. This is pretty much what itoa does. Your teacher probably doesn't want you to use built-in procedures so you can learn how they work.
post #5 of 19
Thread Starter 
Quote:
Originally Posted by TFL Replica View Post
For a general int to char conversion you need a loop to separate the digits of the integer and then add '0' (the ascii code of 0 which is 48) to get the ascii code of the digit. Then you can store the resulting ascii code in the char array.
Ok. Well since I'm converting to binary, each index in my vector is only holding one digit, a 1 or a 0.
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
post #6 of 19
Quote:
Originally Posted by superhead91 View Post
Ok. Well since I'm converting to binary, each index in my vector is only holding one digit, a 1 or a 0.
Even if you hypothetically weren't aware of how to convert you could use a simple if else condition to copy '0' for 0 and '1' for 1.

Having said that writing a subprogram for the conversion would be beneficial for future use (and full range of digits instead of just 0 and 1) too.
post #7 of 19
Thread Starter 
Quote:
Originally Posted by TFL Replica View Post
Even if you hypothetically weren't aware of how to convert you could use a simple if else condition to copy '0' for 0 and '1' for 1.

Having said that writing a subprogram for the conversion would be beneficial for future use (and full range of digits instead of just 0 and 1) too.
The reason I didn't think to do this was bc I looked at an ASCII table and didn't see 0-9 on it..... Maybe I missed them..
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
post #8 of 19
Thread Starter 
Ok, so I did what you recommended, but now when I try and run it, I'm getting a segfault...
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
post #9 of 19
I'm confused, are you taking an int and trying to put it into a char or taking a char and putting it into an int?
Code:
switch(scenario)
{
    case 1: char[x] = int;
    case 2: int = int.parse(char[]);
}
It plays TF2
(18 items)
 
LG Nexus 5
(12 items)
 
Acer 5745PG
(6 items)
 
CPUMotherboardGraphicsRAM
4790k ASRock Z97 Extreme6 Asus GTX980 Strix 4GB Kingston HyperX Fury Black 2x8GB 
Hard DriveHard DriveHard DriveOptical Drive
OCZ Vector 128GB Seagate 2TB  Seagate 500GB 7200.12 Some LG that works properly 
CoolingOSMonitorKeyboard
Corsair H105 Windows 8 Pro 64bit Samsung B2330h MS Digital Media Keyboard 
PowerCaseMouseMouse Pad
CoolerMaster Silent Pro M600 Coolermaster CM690II Advanced Logitech G500s CyberSnipa Something 
AudioAudio
JVC RX700 Creative X-Fi Titanium 
CPUMotherboardGraphicsRAM
Quad-core 2.3 GHz Krait 400 LG Something + Qualcomm MSM8974 Snapdragon 800 Adreno 330 2 GB 
Hard DriveOptical DriveCoolingOS
32 GB internal storage 8MP AutoAwesomeHDRFace + 1MP SelfieMachine Air Android 4.4.2 
MonitorPowerCaseMouse
True HD IPS+ 1080 x 1920 pixels, 4.95 inches Non-removable Li-Po 2300 mAh battery LG Sexybox Capacitive touchscreen 
CPUGraphicsRAMHard Drive
i5 480m @ 2.66GHz GT420M 1GB 4GB DDR3 WD 750GB 
OSMonitor
Windows 7 Professional 64 bit 15" 1366x768 + two point touchscreen 
  hide details  
Reply
It plays TF2
(18 items)
 
LG Nexus 5
(12 items)
 
Acer 5745PG
(6 items)
 
CPUMotherboardGraphicsRAM
4790k ASRock Z97 Extreme6 Asus GTX980 Strix 4GB Kingston HyperX Fury Black 2x8GB 
Hard DriveHard DriveHard DriveOptical Drive
OCZ Vector 128GB Seagate 2TB  Seagate 500GB 7200.12 Some LG that works properly 
CoolingOSMonitorKeyboard
Corsair H105 Windows 8 Pro 64bit Samsung B2330h MS Digital Media Keyboard 
PowerCaseMouseMouse Pad
CoolerMaster Silent Pro M600 Coolermaster CM690II Advanced Logitech G500s CyberSnipa Something 
AudioAudio
JVC RX700 Creative X-Fi Titanium 
CPUMotherboardGraphicsRAM
Quad-core 2.3 GHz Krait 400 LG Something + Qualcomm MSM8974 Snapdragon 800 Adreno 330 2 GB 
Hard DriveOptical DriveCoolingOS
32 GB internal storage 8MP AutoAwesomeHDRFace + 1MP SelfieMachine Air Android 4.4.2 
MonitorPowerCaseMouse
True HD IPS+ 1080 x 1920 pixels, 4.95 inches Non-removable Li-Po 2300 mAh battery LG Sexybox Capacitive touchscreen 
CPUGraphicsRAMHard Drive
i5 480m @ 2.66GHz GT420M 1GB 4GB DDR3 WD 750GB 
OSMonitor
Windows 7 Professional 64 bit 15" 1366x768 + two point touchscreen 
  hide details  
Reply
post #10 of 19
Thread Starter 
Quote:
Originally Posted by biltong View Post
I'm confused, are you taking an int and trying to put it into a char or taking a char and putting it into an int?
Code:
switch(scenario)
{
    case 1: char[x] = int;
    case 2: int = int.parse(char[]);
}
Case 1... I'm passing an integer that's either a 1 or a 0 from a vector to a character array.

As for my segfault, is it bc vector.size() returns and unsigned int and I'm using it to compare things?
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Passing data from int vector to char array