Jump to content
  • Advertisement
Sign in to follow this  
derek7

how to show the number instead of character

This topic is 4892 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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<<" ";

 
}

how to printf the number .could be hex.

Share this post


Link to post
Share on other sites
Advertisement
cast to int before outputting:

std::cout << static_cast<int>(in) << " ";

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 << ' ';
//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
Guest Anonymous Poster
It messed up my post !

if you want to print in hex use std::cout in <

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!