Sign in to follow this  
derek7

how to show the number instead of character

Recommended Posts

#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[i]<<" ";

 
}

how to printf the number .could be hex.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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 .

Share this post


Link to post
Share on other sites
//try
std::cout << std::hex << std::showbase << (int)in[i] << ' ';
//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

Share this post


Link to post
Share on other sites
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 :(

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this