[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 245
  • Last Modified:

output 607 Static Hexadecimal bytes.

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
rkcth
Asked:
rkcth
  • 10
  • 5
  • 3
1 Solution
 
nietodCommented:
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
 
nietodCommented:
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
 
rkcthAuthor Commented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
nietodCommented:
>>  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
 
rkcthAuthor Commented:
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
 
nietodCommented:
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
 
rkcthAuthor Commented:
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
 
nietodCommented:
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
 
rkcthAuthor Commented:
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
 
ercisCommented:
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
 
nietodCommented:
>> 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
 
nietodCommented:
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
 
ercisCommented:
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
 
nietodCommented:
I'm not familar with CGI, does it make standard output hardware/platform independant?  
0
 
ercisCommented:
in short: yes
0
 
nietodCommented:
Do you have the answer you need?
0
 
rkcthAuthor Commented:
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
 
nietodCommented:
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

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 10
  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now