Conversion between integer variables (INT) and byte arrays (byte [])

Source: Internet
Author: User

// Int2byte. cpp: defines the entry point of the console application. // # Include "stdafx. H "# include <windows. h>/* # define makeword (A, B) (Word) (byte) (dword_ptr) (a) & 0xff) | (word) (byte) (dword_ptr) (B) & 0xff) <8) # define makelong (A, B) (long) (Word) (dword_ptr) (a) & 0 xFFFF) | (DWORD) (Word) (dword_ptr) (B) & 0 xFFFF ))) <16) # define loword (L) (Word) (dword_ptr) (L) & 0 xFFFF) # define hiword (L) (word) (dword_ptr) (L)> 16) & 0 xFFFF) # define lobyte (w) (byte) (dword_ptr) (w )) & 0xff) # define hibyte (w) (byte) (dword_ptr) (w)> 8) & 0xff )) * // ============================================== ========================================/// big endian/small endian utility functions // ======== ========================================================== ============= bool issmallendian () {dword wd = 0x22; If (* (byte *) & WD) = 0x22) // small endianreturn true; elsereturn false ;} void swapshort (word * SP) {byte * CP = (byte *) SP, t = CP [0]; CP [0] = CP [1]; CP [1] = T;} void swaplong (DWORD * LP) {byte * CP = (byte *) LP, t = CP [0]; CP [0] = CP [3]; CP [3] = T; t = CP [1]; CP [1] = CP [2]; CP [2] = T;} // int 2 bytebyte * int2byte (INT nval) {byte * pbyte = new byte [4]; for (INT I = 0; I <4; I ++) {pbyte [I] = (byte) (nval> 8 * (3-I) & 0xff);} return pbyte ;} // byte 2 intint byte2int (byte * pb) {// assume the length of Pb is 4int nvalue = 0; For (INT I = 0; I <4; I ++) {nvalue + = (Pb [I] & 0xff) <(8 * (3-I);} return nvalue;} int _ tmain (INT argc, _ tchar * argv []) {// PC, small-end bytes ////////////////////////////////////// /// // byte * byte = int2byte (0x12345678 ); printf ("byte [0] = 0x % XH, byte [1] = 0x % XH, byte [2] = 0x % XH, byte [3] = 0x % XH \ n ", byte [0], byte [1], byte [2], byte [3]); int nval = byte2int (byte); printf ("nval = 0x % XH \ n", nval ); //////////////////////////////////////// /// // small-end bytes should be 0xefcdab89, 0x89abcdefword wlow, whigh; DWORD dwdata; byte B [4] = {0x89, 0xab, 0xcd, 0xef}; DWORD dwval = 0xefcdab89; // DWORD into byte array word Lo = loword (dwval), HI = hiword (dwval); printf ("Lo = 0x % XH, HI = 0x % XH \ n ", lo, hi); // combines byte arrays into dwordwlow = makeword (B [0], B [1]); whigh = makeword (B [2], B [3]); dwdata = makelong (wlow, whigh); printf ("wlow = 0x % XH, whigh = 0x % XH, dwdata = 0x % XH \ n ", wlow, whigh, dwdata); getchar (); Return 0 ;}

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.