BinToHex conversion

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tawanda diza
    New Member
    • Sep 2011
    • 29

    BinToHex conversion

    Im a reading a bitmap from a file using
    fp = fopen("\\custph oto.bmp","rb");


    when i trace the data being sent it wil be like:

    "ÿØÿà JFIFÿÛ C 
    (1#% (:3=<9387@H\N@ DWE78PmQW_bghg> Mqypdx\egcÿÛ C//cB8Bccccccccccc ccccccccccccccc ccccccc"


    i want to send this picture to a server which only accepts hex values,
    my question is How do i convert those funny characters to hex equivalance?

    thanks in advance.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    writing text data to a file is easy

    Code:
    char buffer[10];
    int ix;
    
    fp = fopen("\\custphoto.bmp","rb");
    fread(buffer, 1, sizeof buffer, fp);
    fout = fopen("\\custphoto.txt","w");
    
    for(ix=0; ix < sizeof buffer)
    {
      fprintf(fout, "%02x", buffer[ix]);
    }
    
    fclose(fp);
    fclose(fout);
    The question is what do you expect the receiving server to do with the data when it receives it?

    Do you expect it to display it? That would only work if someone had coded the server to look for jpg files coded as ASCII hex values.

    Do you expect someone else to be able to download and use your jpg? They will need a way to convert the ASCII hex string you have provided back into the binary file.

    So the real question is what are you trying to achieve and will doing this really help?

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Your intentions are not clear.
      Suppose the next byte you read from the input file has binary value 0x18. What do you want to write to the output file?
      • One byte with the value 0x18. (output is a binary file)
      • Two bytes with the value '1' and '8'. (output is a text file containing "hex-ASCII" characters)

      Comment

      • tawanda diza
        New Member
        • Sep 2011
        • 29

        #4
        I cant believe this, this is great, Banfa thank you.
        i will integrate that into my app to send the data to the server.

        Comment

        • tawanda diza
          New Member
          • Sep 2011
          • 29

          #5
          what i wanted was to send the .bmp file to a server in hexformat, the server then stores it and will send me back when ever i request the data, the server will return it to me in hex format,then i have to convert it back to the original picture and display it on my screen.

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            Yes, but what do you mean by "hexformat" ? This is a Cheshire Cat term -- it means whatever you want it to mean.

            The original bmp file was a binary file (that's why your open argument was "rb"). Do you want the file written to the server to be identical to the original file or do you want to change it? Converting the binary file to a "hex-ASCII" text file changes the file.

            Consider what happens you if repeat this process (assuming ASCII encoding):
            1. Read binary 0x18 from the original file, and write out '1' and '8'.
            2. Read '1' (0x31) and '8' (0x38) from the previous output file and write out '3', '1', '3', and '8'.
            3. Repeat and you write out '3', '3', '3', '1', '3', '3', '3', and '8'.
            4. Repeat ...


            Are you sure you don't want your output to be a binary file?

            Comment

            • tawanda diza
              New Member
              • Sep 2011
              • 29

              #7
              hie donbock

              yes i want the file written on the server to be identical to the original file.

              may be you can help me, the server is not accepting my binary data i.e "ÿØÿà JFIFÿÛ C 
              (1#% (:3=<9387@H\N@ DWE78PmQW_bghg> Mqypd x\egcÿÛ C//cB8Bccccccccccc ccccccccccccccc ccccccc"

              it understands data like "FFD8FFE000104A 464946000102010 04800480000FFE1 0C9445786966000 049492A00080000 000A000F0102000 300000048544300 100102000B00000 086000000120103 000100000001000 0001A0105000100 0000910000001B0 105000100000099 00000028010"

              Comment

              • donbock
                Recognized Expert Top Contributor
                • Mar 2008
                • 2427

                #8
                The funny text you report coming from your binary file indicates that you tried to print that file. You can't print a binary file -- it does not contain printable text. Likewise, you don't want to open a binary file with a text editor.

                If you want your output file to match the input file, then you want both to be binary files (or both to be text files). What do you mean "server ... only accepts hex values" and "the server is not accepting my binary data"? What errors did you get?

                The following code snippet copies a binary file one byte at a time.
                Code:
                int c;
                FILE *fin, *fout;
                fin = fopen(infilename, "rb");
                fout = fopen(outfilename, 'wb");
                if ((fin == NULL) || (fout == NULL)) do something;
                while ((c = fgetc(fin)) != EOF) {
                   if (fputc(c, fout) == EOF) do something;
                   }
                fclose(fout);
                fclose(fin);

                Comment

                Working...