[SOLVED]Wanting To Display More Info On Ascii

Alright so I'm using this to display the ascii Characters
1
2
3
4
5
6
7
void ASCIIChartTwoOne()
{
	for(int i = 0; i < 256; i++)
	{
		std::cout << i << " = " << (char)i << std::endl;
	}
}

Now the output comes out fine but I'm just wondering if there's a way to call each ones Hex, Oct and Binary values as well.

Basically so the output would be like
Binary = 010 0011, Oct = 043, Dex = 35, Hex = 23, Glyph = #


With an Input of etc:
std::cout << binaryValue << octValue << dexValue << hexValue << (char)glyphValue << std::endl;
Last edited on
Thanks a heap for that man - So far I got this working:
1
2
3
4
for(int i = 0; i < 256; i++)
	{
		std::cout << " Hex = " << std::hex << i << " \tOct = " << std::oct << i << " \tDex = " << std::dec << i << " \tGlyph = " << (char)i << std::endl;
	}

But I'm not sure how to display the binary value?
closed account (z05DSL3A)
The following may be of interest to you: (but may be way more than you want to deal with)
A bin Manipulator For IOStreams
http://accu.org/index.php/journals/350

NB: I've only given it a quick scan
Last edited on
Alright, I read over the Creating "The bin Manipulator" section. I understand it except for the part at the end that states
Well, except that there is no easy way to turn binary formatting off again! Since we have replaced the formatting routine we can use the std::hex manipulator as often as we want: there will be no change at all.
I don't understand the consequences that might happen if its not turned off?

If this is the only way to do it I'm fine with it - I just don't understand the above quote properly.

Thanks so much for the help so far guestgulkan and Grey Wolf
Ok so lets say I did this - is this possible?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
///////////////////////////////////////////////////////////////////////
//Template for using Binary calls - "std::cout << bin << 10 << "\n";"//
///////////////////////////////////////////////////////////////////////
template <typename cT, typename traits>
std::basic_ios<cT, traits>&bin(std::basic_ios<cT, traits>& ios)
{
	typedef std::ostreambuf_iterator<cT>
                         iterator;
	std::locale loc =
         std::locale(ios.getloc(),
         new bin_num_put<cT, iterator>());
	ios.imbue(loc);
	return ios;
}

void BinaryValue(int value)
{
	std::cout << "Bin = " << bin << value;
}


If i comment out the BinaryValue function - it compiles but if I don't comment it out it will give me this error..

1>.\HelperFunctions.cpp(114) : error C2061: syntax error : identifier 'bin_num_put'
1> .\HelperFunctions.cpp(121) : see reference to function template instantiation 'std::basic_ios<_Elem,_Traits> &bin<char,std::char_traits<char>>(std::basic_ios<_Elem,_Traits> &)' being compiled
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1>.\HelperFunctions.cpp(114) : error C2059: syntax error : ')'


So in my mind this makes sence what I'm doing but I'm still unsure.
closed account (z05DSL3A)
If you want a simple 'hack' you could try somthing like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include "stdafx.h"
#include <iostream>
#include <sstream>

std::string intToBinaryString(int in)
{
    std::stringstream ss (std::stringstream::in | std::stringstream::out);
    int mask = 0x8000;

    for(int i = 0;i < 16; ++i)
    {
        if (in & mask)
            ss << 1;
        else
            ss << 0;
        mask = mask >> 1;
    }
    std::string str;
    ss >> str;
    return str;
}

int _tmain(int argc, _TCHAR* argv[])
{
    std::cout <<  intToBinaryString(4) <<std::endl;
    return 0;
}


Sorry I don't have time to look at the Template stuff at the moment.
Thanks mate, i got it working :)
Just encase anyone wants this - i changed the string method a bit and made it so it would displays the binaries correctly.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
///////////////////////////////////
//Template for using Binary calls//
///////////////////////////////////
std::string intToBinaryString(int in)
{
    std::stringstream ss (std::stringstream::in | std::stringstream::out);
    int mask = 0x8000;

    for(int i = 0;i < 16; ++i)
    {
        if (in & mask)
            ss << 1;
        else
            ss << 0;
        mask = mask >> 1;
    }
    std::string str;
    ss >> str;

	std::string space = " ";

	if(in < 128)
	{
		for(int i = 0; i < 9; i++)
		{
			str.erase((str.begin()+1) - 1);
		}
		str.insert(3, space);// Insert a space 3 characters into the string to 
							 // break apart the binary digits if decimal < 128
	}

	if(in > 127) // && in < 256)
	{
		for(int i = 0; i < 8; i++)
		{
			str.erase((str.begin()+1) - 1);
		}
		str.insert(4, space);// Insert a space 4 characters into the string to 
							 // break apart the binary digits if decimal > 127
	}

    return str;
}
Topic archived. No new replies allowed.