![]() |
| |||||||
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 |
| Registered User Join Date: May 2008
Posts: 22
| I'm new to c++ and to all forms of programming and am trying to get the source code for a program that prints out the hex value of any given byte in a file. I've gone through; this site, google, yahoo, cplusplus.com, C++ for dummies 5th Edition and, unbelievably, more. The answer just seems to elude me. No one says how to do this. I can only get the size of a file or read a txt file, but not the hex, or any other value of any given byte in a file. Thats the problem. Can ANYONE show me the source of a simple program that does this? I'm going to show you the closest I could get to this a majority may lose your lunch when you see it but you'll understand what a state I'm in with which I can only get the character of in a txt file and not the value of a byte of anyfile. I know I'm supposed to make the char unsigned but that stops me from compiling at all and give me errors. [tag] Code: #include <iostream>
#include <fstream>
using namespace std;
int x, y;
int main () {
char * buffer;
Start:
cout << "Enter the number of the byte you are looking for: ";
cin >> x;
if (x<1) {
cout << "We begin counting at 0. Please try again.\n"; goto Start;}
ifstream is;
is.open ("Yo.bin", ios::in|ios::binary);
is.seekg (x, ios::beg);
is.read (buffer,1);
is.close();
cout << "\nThe hex value of byte number"<< x <<" is ";
cout.write (buffer,1);
cout << "\n\nDo you want to return to the start of this program?\n";
cout << "Press 1 for YES or any other key for NO: ";
cin >> y;
if (y == 1) {goto Start;}
if (y == !1) {goto End;}
End:
cout << "Goodbye!\n";
delete[] buffer;
return 0;}
[/tag]
Last edited by koxson; 05-07-2008 at 08:47 AM. |
| koxson is offline | |
| | #2 | |
| The larch Join Date: May 2006
Posts: 3,086
| Whether something is displayed as a hex value or a decimal value or a character is just a matter of output representation. The byte itself is still the same. To change the representation you can use I/O format flags. For example: Code: #include <iostream>
int main()
{
char c = 'A';
std::cout << std::showbase << std::hex << int(c);
}
Code: char buffer[1] = {0};
__________________ I might be wrong. Quote:
| |
| anon is offline | |
| | #3 |
| and the Hat of Ass Join Date: Dec 2007
Posts: 731
| You never allocate memory for buffer (and yet you delete it). Code: char *buffer = new char[2]; Code: char buffer; Gotos? In *my* C++ program? Code: int y = 0;
while (!y)
{
// Do your thing
}
|
| rags_to_riches is offline | |
| | #4 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Is this a "find five problems"? You don't need that much code to make me loose my lunch, surely this can easily be written without goto's: Code: if (x<1) {
cout << "We begin counting at 0. Please try again.\n"; goto Start;}
Also, your error message doesn't seem to match exactly with the if-statement. Second problem: Code: char * buffer; Third problem: Code: cout.write (buffer,1); Code: cout << hex << buffer; Fourth problem: Code: cout << "Press 1 for YES or any other key for NO: "; cin >> y; Problem five: Code: if (y == !1) {goto End;}
End:
Code: if (y == !1) {goto End;}
cout << "Got here" << endl;
End:
Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. |
| matsp is offline | |
| | #5 |
| and the hat of vanishing Join Date: Aug 2001 Location: The edge of the known universe
Posts: 21,214
| > delete[] buffer; You didn't allocate it, so you can't use it and you can't delete it. Also, use loop constructs in place of those goto statements. Also, there is no point in x and y being global. Edit: Meh, beaten *3
__________________ If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut. Up to 8Mb PlusNet broadband from only £5.99 a month! |
| Salem is offline | |
| | #6 | |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Quote:
![]() -- Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. | |
| matsp is offline | |
| | #7 |
| 3735928559 Join Date: Mar 2008
Posts: 662
| you had the right idea, but a few details were off. consider the following: Code: int x;
bool go=true;
while(go)
{
cout << "Enter the number of the byte you are looking for: ";
cin >> x;
if (x<0)
{
cout << "We begin counting at 0. Please try again.\n";
}
else
{
unsigned int y;
char buffer;
ifstream is;
is.open ("Yo.bin", ios::binary);
is.seekg (x, ios::beg);
is.read (&buffer,1);
is.close();
cout << "\nThe hex value of byte number "<< x <<" is " << hex << (int)buffer << endl;
cout << "\n\nDo you want to return to the start of this program?\n";
cout << "Press 0 for NO or any other key for YES: ";
cin >> y;
if(!y)
{
go = false;
}
}
}
cout << "Goodbye!\n";
return 0;
|
| m37h0d is offline | |
| | #8 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Code: cout << "\nThe hex value of byte number "<< x <<" is " << hex << (int)(unsigned char)buffer << endl; -- Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. |
| matsp is offline | |
| | #9 |
| 3735928559 Join Date: Mar 2008
Posts: 662
| could you not just cast it once to unsigned int? |
| m37h0d is offline | |
| | #10 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Perhaps, but I feel that it will still sign-extend to int first, then "unsign" that. -- Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. |
| matsp is offline | |
| | #11 |
| Registered User Join Date: May 2008
Posts: 22
| I wanted to come back to thank everyone on this thread for their help, especially m37h0d for putting it in terms the layman to the world of c++ can understand. Thanks m37h0d! |
| koxson is offline | |
| | #12 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
|
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #13 | |
| 3735928559 Join Date: Mar 2008
Posts: 662
| Quote:
yay me! making the code as clean and simple as possible should always be a goal imo. i didn't write that in any fashion that was supposed to be "for the layman"; i just tried to make it as clear and simple as possible, which as far as i'm concerned is always good programming! | |
| m37h0d is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| A development process | Noir | C Programming | 30 | 10-28-2009 04:24 AM |
| gcc link external library | spank | C Programming | 6 | 08-08-2007 03:44 PM |
| brace-enclosed error | jdc18 | C++ Programming | 53 | 05-03-2007 05:49 PM |
| airport Log program using 3D linked List : problem reading from file | gemini_shooter | C Programming | 3 | 03-04-2005 02:46 PM |