Solved

output 607 Static Hexadecimal bytes.

Posted on 2000-02-22
18
240 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 5
  • 3
18 Comments
 
LVL 22

Accepted Solution

by:
nietod earned 50 total points
ID: 2548685
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
ID: 2548691
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
ID: 2552146
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 22

Expert Comment

by:nietod
ID: 2552285
>>  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
ID: 2552344
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
ID: 2552366
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
ID: 2552727
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
ID: 2552830
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
ID: 2552954
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
 
LVL 1

Expert Comment

by:ercis
ID: 2553268
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
ID: 2553990
>> 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
ID: 2554126
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
ID: 2555915
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
ID: 2556009
I'm not familar with CGI, does it make standard output hardware/platform independant?  
0
 
LVL 1

Expert Comment

by:ercis
ID: 2556077
in short: yes
0
 
LVL 22

Expert Comment

by:nietod
ID: 2556600
Do you have the answer you need?
0
 

Author Comment

by:rkcth
ID: 2556628
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
ID: 2557853
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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

628 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