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

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

问题描述

我必须做的是以二进制模式打开一个文件,其中包含旨在被解释为整数的存储数据.我见过其他示例,例如 Stackoverflow-Reading integer" size bytes from一个 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
如果将 12 个字节分成 3 个整数,则这(应该)等于 71、23 和 65.

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 个字节读入一个字符数组后,如何使用按位运算使 char[0] 位成为 int 的前 8 位,依此类推,直到每个 char 的位是 int 的一部分.

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?

这是我目前所拥有的代码片段,我只是不知道接下来要采取的步骤.

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:

typedef unsigned char u8;  // in case char is signed by default on your platform
unsigned num = ((u8)chars[0] << 24) | ((u8)chars[1] << 16) | ((u8)chars[2] << 8) | (u8)chars[3];

它所做的是将左参数向左移动指定数量的位,从右边添加零作为填充.例如,2 <<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:

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

您的系统的字节序在这里无关紧要;您知道文件中表示的字节顺序,它是恒定的(因此是可移植的),因此当您读取字节时,您知道如何处理它们.CPU/内存中整数的内部表示可能与文件的不同,但代码中的逻辑按位操作与系统的字节序无关;最低有效位总是在右边,最高在左边(在代码中).这就是为什么 shift 是跨平台的——它在 logical 位级别上运行 :-)

The endianness of your system doesn't matter here; you know the endianness of the representation in the file, which is constant (and therefore portable), so when you read in the bytes you know what to do with them. The internal representation of the integer in your CPU/memory may be different from that of the file, but the logical bitwise manipulation of it in code is independent of your system's endianness; the least significant bits are always at the right, and the most at the left (in code). That's why shifting is cross-platform -- it operates at the logical bit level :-)

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

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