Unlucky windows (bin2hex function)

Source: Internet
Author: User

A standard C ++ program.

First, let's take a look at the functions that convert bin and hexview.

Hexviewtobin:
The following program is used to convert the hexadecimal number represented by a text to the binary format:

/*
* Function: convert a string with the length of Len to the binary format containing one byte in every step of S.
* Fan chenpeng
* @ Param sdesbuffer: buffer for storing results
* @ Param ssourcbuffer source string
* @ Param Len the length of the source string
* @ Param step: the number of bytes occupied by each hexadecimal symbol
* Note: This function does not consider lowercase letters in hexadecimal notation.
*/
Void hextobin (char * sdesbuffer, const char * ssourcbuffer, unsigned int Len, unsigned step)
{

Const char DIC [] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 0, 10, 11, 12, 13, 14, 15, 16 };
Char * psourcbuffer = (char *) ssourcbuffer;
Char * pdesbuffer = sdesbuffer;
For (INT I = 0; I <Len; I + = step)
{
* Pdesbuffer ++ = char (DIC [* psourcbuffer-0x30] * 16 + DIC [* (psourcbuffer + 1)-0x30]);
Psourcbuffer + = step;
}
Return;

}

 

Binto hexview

/*************************************** *********************************
* The memory data pointed to by the bubfin is sent to the Strout in a hexadecimal view.
* By fancp 2009-09-08
*
**************************************** **********************************/
Char * gethexview (char * Strout, size_t strlen, const char * bufin, size_t buflen)
{
Int offset = 0;
Strout [0] = '\ 0 ';
If (3 * buflen> strlen)
{
Return Strout;
}
For (offset = 0; offset <buflen; offset ++)
{
Sprintf (Strout + 3 * offset, "% 02x", (unsigned char) bufin [offset]);
}
Return Strout;
}

 

 

The following main function reads the strings in the file and saves them in hexadecimal format.

 

# Include <iostream>
Using namespace STD;

Void main ()
{
Char slineread [len_line];
Char sbin [200];
Char plineread;
Ifstream fin ("test.txt ");
Ofstream fout ("test. bin ");

While (! Fin. EOF ())
{
Fin. Getline (slineread, len_line );
Hextobin (sbin, slineread, Fin. gcount (), 3 );
Fout. Write (sbin, 20 );
}
Fin. Close ();
Fout. Close ();

}

It is written in one byte more.
As follows:
The text file test.txt has only one line of content: A5 0a 02 00 00 3C 00 00 01 9e

There should be 10 bytes written as the binary file test. Bin. 11 bytes are not needed in VC ++. The hexadecimal view is as follows: A5 0d 0a 02 00 00 3C 00 01 9e

Compiling in Linux does not solve this problem. I have been thinking for a few hours. Where did this "0d" come from?
When someone else reminds me that OD is a line break. Combined with 0a is the carriage return line break in windows.
In Windows, if you write files in text mode, Windows writes the read "0d" or "0a" to two characters "0d 0a ". But not in binary mode.
Once you know the crux of the problem, you can take the right medicine. The solution is to open test. bin in "iOS: Binary" mode.
Ofstream fout ("test. bin", IOS: Binary );

Ah! Bad windows. It took me several hours to get one byte ~

Function for converting uppercase letters: Inline char upper (char m)
{
Return (M <= 0x66 & M> = 0x61 )? M-0x20: m;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.