how to show the number instead of character

Started by
5 comments, last by Genjix 18 years, 10 months ago

#include <stdlib.h>
#include <fstream>
#include <vector>
#include <iostream> 
#include <windows.h>
 
main()
{	
	std::vector<BYTE> in(4096);
	std::ifstream infile("coast.raw", std::ios_base::binary);
	infile.read((char*)&in[0], in.size());
	for (int i=0;i<in.size();i++)
	std::cout<<in<<" ";

 
}

how to printf the number .could be hex.
Advertisement
cast to int before outputting:
std::cout << static_cast<int>(in) << " ";
This will tell cout to print all numeric values as hex. You have to cast the BYTE type to a numeric type for this to work fx. an integer.

std::cout instead of . (That will also put it into the std namespace). Most of the old C libraries have C++ equivalents using the same nameconvention as the example with .
//try
std::cout << std::hex << std::showbase << (int)in << ' ';
//given for example 'a' (decimal 65) you will have this ouput: 0x41
std::hex is the manipulator for showing numbers in esadecimal
std::showbase is the manipulator for showing base (0x)
if you convert through (int) the compiler will be forced to parse the char as an integer
[ILTUOMONDOFUTURO]
It messed up my post !

if you want to print in hex use std::cout in <
Quote:Original post by bjogio
if you convert through (int) the compiler will be forced to parse the char as an integer


When using C++ try to use the C++ casts instead, in this case: static_cast
There are several reasons for it:
It clearly shows what kind of cast you're doing.
It gives the compiler a chance to warn you if somethings weird, or even dissalowed.
Casting is ugly, so becomes the written code (yes (int)bla is cleaner than static_cast<int>(bla)).
You can search your code in hope to remove/replace casts.

Worth while reading: What good is static_cast?

Just being a code-nazi :(
[ ThumbView: Adds thumbnail support for DDS, PCX, TGA and 16 other imagetypes for Windows XP Explorer. ] [ Chocolate peanuts: Brazilian recipe for home made chocolate covered peanuts. Pure coding pleasure. ]
nice link

This topic is closed to new replies.

Advertisement