Solved

output 607 Static Hexadecimal bytes.

Posted on 2000-02-22
18
233 Views
Last Modified: 2010-04-02
I need to output, 607 static Hexadecimal bytes, I know how to do this, with characters, I do not know how to with Hex. This is for a cgi which will output a single pixel. Therefore, it will be console I/O.
0
Comment
Question by:rkcth
  • 10
  • 5
  • 3
18 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
Comment Utility
You can use the "hex" ios manipulate to make the stream ouput numeric values in their ASCII reprentation of their hex values instead of of the ASCII reprensetation of their decimal values.  For example.

cout << hex << 20;

will ouput "16".  the hex value of 20.

continues.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
If you have these values in an array you might do somethign like

const int ArraySize = 607;
char Array[ArraySize] = {??????};

cout << hex;
for (int i = 0; i < ArraySize; ++i)
{
   cout << Array[i] << ' ';
}
cout << dec;

Let me know if you have any questions.
0
 

Author Comment

by:rkcth
Comment Utility
I need to do this as efficiently as possible, is the array method the most efficient way? The 607 bytes will ALWAYS be the same. But this must be done in a fraction of a second.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
>>  is the array method the most efficient way?
Its not a method.  I was just showign you how to convert the data if it were stored in an array.   I don't know how your data is stored.

>> The 607 bytes will ALWAYS be the same.
Then don't bother doing the conversion at run-time.  Dot he conversion one time, manually or through a utility program and use this output to create a string that expresses the values in hex, then output this string instead.

>> this must be done in a fraction of a second.
The code above should be able to do te conversion in less than a second on any modern machine, probably much less than a second.
0
 

Author Comment

by:rkcth
Comment Utility
So if I convert the Hex to ascii, I could output it and it would look like binary to the client, if I use the proper mime?
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
What do you menan by "look like binary"?  

it will look like an an ASCII string that represents a Hex value.  Like

"0x12 0x34 0x56..."

with or without the 0x's depending on what you want.
0
 

Author Comment

by:rkcth
Comment Utility
I do not want it to output in ascii, I want it to output the Hexadecimal, on the screen, it will look like garbage.
In other words, if I output FA, I don't want FA, I want the ascii equivalent to be output. This is neccessary so that it will work for the cgi. Sorry for not being clearer earlier.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
What does it mean to be hexadecimal?  

How can you display a number if hexadecimal?  in decimal?  You can't.  Byt you can display an ASCII representation of a number in hexadecima or on decimal, or octal etc.  i.e. you can display ASCII characters that _represent_ the number in decimal.  Is that what you want?  if so, just convert the data to hex a string that represents its hex value and display that hex value.  like  if the data to be converted is

32,12,10

you would create a string like

const char *datastring = "20 0C 0A";

(I'm not sure what format you want to see it in.)
0
 

Author Comment

by:rkcth
Comment Utility
Ok, I'll ask in a more professional and perhaps understandable way. How can I output a stream of binary data to the screen? Thanks nietod for all of your help. I hope this question is clearer.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 1

Expert Comment

by:ercis
Comment Utility
try this prog, it outputs a red dot (gif image, size 1x1 pix)

#include <stdio.h>

const unsigned char IMAGE_GIF[] = {
0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x01, 0x00,
0x01, 0x00, 0x80, 0xFF, 0x00, 0xFF, 0xFF, 0xFF,
0xFF, 0x00, 0x00, 0x2C, 0x00, 0x00, 0x00, 0x00,
0x01, 0x00, 0x01, 0x00, 0x00, 0x02, 0x02, 0x4C,
0x01, 0x00, 0x3B };

int main(void)
{
  char *p = (char *)IMAGE_GIF;
  char *e = p + sizeof(IMAGE_GIF);

  printf("Content-type: image/gif\r\n\r\n");

  do
  {
    putchar(*p);   // print char
  }
  while (++p < e);

  exit(0);
}
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
>> How can I output a stream of
>> binary data to the screen?
My understanding is thaat you want and ASCII representation of the binary data to go to the screen.  right?  Or do you want the actual data to go to the screen?

>> try this prog, it outputs a red dot
Now this program is sending binary data, not its representation, to standard output.  The effect of that is unpredictable.  It depends on the OS, the device that is currenctly acting as stndard output, and the current state of that device.  i.e. This will work on your computer with its current configuration, but won't work on other types of computers and even computers of the same tupe with different configurations.  In other words, this is hardware-dependant.  

Now that doesn't necessarily mean it is bad, but you should understand the limitations.  It means that the code can't be ported to other types of computers and other OSs, and you may have to place restrictons on how it is used on your type of computer and OS.

Now is there any reason you can't do this wit the bigger chunk of ddata you have?  bigger image?
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
Wait a second, ignore the end of that comment, I didn't realsize that the code was posted by someone else.

So I still don't know what it is you want.

Do you want to see a hexadecimal representation of the data?  Or do you want the data itself?
0
 
LVL 1

Expert Comment

by:ercis
Comment Utility
nietod: this code is most portable and very usable for CGI programs under any platform, that has C/C++ compilator...
it works with WinNT/IIS or Linux/Apache etc.
of course, if usinig it as simple console program, it can make strange results, like any operation, that prints binary data to stdout...
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
I'm not familar with CGI, does it make standard output hardware/platform independant?  
0
 
LVL 1

Expert Comment

by:ercis
Comment Utility
in short: yes
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
Do you have the answer you need?
0
 

Author Comment

by:rkcth
Comment Utility
Yes ercis gave it to me. But you helped so I am going to give you both points. I would have made it worth more had I known it was so tough. I looked in all my references and was unable to find anything. I guess it is not a common procedure:)
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
Not its very common--its just not what you asked.  All you want to do is send bytes or characters (they are the same thing) to the console.  You can do that in many different ways like

cout << 'a';
cout <<"ABC"
cout.write(somearray,arraylength);

and probably 2 dozen other ways.  In your case the bytes you want to write aren't necessarily ASCII printable characters (like 'a').  But that doesn't matter.  This doesn't really have anything to do with hex, you aren't sending them to the display in hex, you are just sending the bytes (characters).

What may have confused you is that you are specifying the bytes/characters in the source code using hex numbers.  But that  just matters at compile time.  Once the code is compiled the result is a byte or character and it doesn't matter how you told the compiler which byte/character you wanted.

Also for the sake of speed, you probably don't want to use repeated calls that write one character (like putchar).  A good choice for this would be to use the basic_ostream::write() function.  That allows you to write all 607 butes at a single time.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now