在C ++中使用位运算符来改变4字符为int [英] Using bitwise operators in C++ to change 4 chars to int

查看:163
本文介绍了在C ++中使用位运算符来改变4字符为int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须做的是开放的,它包含了旨在成为PTED为整数间$ P $的数据存储二进制模式的文件。我见过的其他例子,如<一个href=\"http://stackoverflow.com/questions/544928/reading-integer-size-bytes-from-a-char-array\">Stackoverflow-Reading从一个char整数大小的字节数组* 但我想尝试采取不同的方法(我可能只是固执,还是愚蠢:/)。我首先创建一个十六进制编辑器是如下简单的二进制文件。

What I must do is open a file in binary mode that contains stored data that is intended to be interpreted as integers. I have seen other examples such as Stackoverflow-Reading "integer" size bytes from a char* array. but I want to try taking a different approach (I may just be stubborn, or stupid :/). I first created a simple binary file in a hex editor that reads as follows.

00 00 00 47 00 00 00 17 00 00 00 41 结果
这(应该)等于71,23,65,如果12个字节被分为3个整数。

00 00 00 47 00 00 00 17 00 00 00 41
This (should) equal 71, 23, and 65 if the 12 bytes were divided into 3 integers.

在二进制模式下打开此文件,阅读4个字节为字符数组,我如何使用位运算,使字符后[0]位是int型的前8位,依此类推,直至每个字符的位是整型的一部分​​。

After opening this file in binary mode and reading 4 bytes into an array of chars, how can I use bitwise operations to make char[0] bits be the first 8 bits of an int and so on until the bits of each char are part of the int.

 
My integer = 00        00        00        00  
 +           ^         ^         ^         ^
Chars      Char[0]  Char[1]   Char[2]   Char[3]
             00        00        00        47


So my integer(hex) = 00 00 00 47 = numerical value of 71

另外,我不知道我的系统的字节序如何进场这里,所以有什么,我需要注意哪些事项?

Also, I don't know how the endianness of my system comes into play here, so is there anything that I need to keep in mind?

下面是我到目前为止,我只是不知道接下来要采取的步骤一个code片段。

Here is a code snippet of what I have so far, I just don't know the next steps to take.


std::fstream myfile;
    myfile.open("C:\\Users\\Jacob\\Desktop\\hextest.txt", std::ios::in | std::ios::out | std::ios::binary);
    if(myfile.is_open() == false)
    {
        std::cout << "Error" << std::endl;
    }
    char* mychar;
    std::cout << myfile.is_open() << std::endl;
    mychar = new char[4];
    myfile.read(mychar, 4);

我最终的处理阅读计划从一个文件中,也许一个自定义数据类型的最终浮动,但首先我只需要获得更多的熟悉使用位操作。
谢谢你。

I eventually plan on dealing with reading floats from a file and maybe a custom data type eventually, but first I just need to get more familiar with using bitwise operations. Thanks.

推荐答案

您想按位向左移位运算符:

You want the bitwise left shift operator:

int num = (chars[0] << 24) + (chars[1] << 16) + (chars[2] << 8) + chars[3];

它的作用是位指定数量的移位左参数向左,从右边为馅加零。例如, 2';&LT; 1 4,因为2 二进制10 和不断变化的一个左侧提供了 100 ,它是4

What it does is shift the left argument a specified number of bits to the left, adding zeros from the right as stuffing. For example, 2 << 1 is 4, since 2 is 10 in binary and shifting one to the left gives 100, which is 4.

此能够更写在一个更一般的环形式:

This can be more written in a more general loop form:

int num = 0;
for (int i = 0; i != 4; ++i) {
    num += chars[i] << (24 - i * 8);    // |= could have also been used
}

系统的字节序此处无关紧要;你知道文件在重新presentation,这是不变的(因此便携式)的存储方式,所以当您在读取的字节,你知道该怎么跟他们做。 CPU中的整数的内部重组presentation /内存可能是从该文件的不同,但在code它的逻辑位操作独立于系统的字节序的;所述至少显著位总是在正确的,并在左侧的最(在code)所示。这就是为什么移动是跨平台 - 它在操作的逻辑的位水平: - )

这篇关于在C ++中使用位运算符来改变4字符为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆