在INT替换字节 [英] Replace byte in a int

查看:146
本文介绍了在INT替换字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个INT由4个字节。我怎么能取代那些4个字节一个新的字节之一。换句话说,我要寻找一个方法:

  INT ReplaceByte(INT指数,int值,字节replaceByte)
{
     //执行
}

例如,如果我有值 FFFFFFFF (-1),我会想更换个字节 0 0A (10),那么我会调用该方法为:

ReplaceByte(0,-1,10)

和我会喜欢这个方法返回我 FFFFFF0A

我必须转换INT为一个字节数组,然后替换字节我想然后再转换回一个int?我要寻找这样的一个有效途径。我们正在建立类似的程序调试器连接到目标(板),我们很频繁地更新这些值。

编辑(结果)

感谢您​​的回答我比较方法:

下面是结果:

注意我的执行是最慢的!

下面是code:

 静态无效的主要(字串[] args)
    {
        字节[] = randomBytes新的字节[1024 * 1024 * 512];        随机R =新的随机();
        r.NextBytes(randomBytes);        Int64的总和;
        VAR现在= DateTime.Now;        Console.WriteLine(测试1);
        总和= 0;
        现在= DateTime.Now;
        的foreach(在randomBytes VAR BT)
        {
            总和+ = ReplaceByte1(1,-1,BT);
        }        Console.WriteLine((DateTime.Now测试1 {0}秒\\ t哈希= {1} \\ n完成 - 现在).TotalSeconds,总和);        Console.WriteLine(试验2);
        总和= 0;
        现在= DateTime.Now;
        的foreach(在randomBytes VAR BT)
        {
            总和+ = ReplaceByte2(1,-1,BT);
        }        Console.WriteLine((DateTime.Now测试2 {0}秒\\ t哈希= {1} \\ n完成 - 现在).TotalSeconds,总和);
        Console.WriteLine(测试3);
        总和= 0;
        现在= DateTime.Now;
        的foreach(在randomBytes VAR BT)
        {
            总和+ = ReplaceByte3(1,-1,BT);
        }        Console.WriteLine((DateTime.Now测试3 {0}秒\\ t哈希= {1} \\ n完成 - 现在).TotalSeconds,总和);        Console.Read();
    }    //测试1
    静态INT ReplaceByte1(INT指数,int值,字节replaceByte)
    {
        收益率(值和放大器;〜(0xFF的<<(指数* 8)))| (replaceByte&所述;≤(指数* 8));
    }    //测试2
    静态INT ReplaceByte2(INT指数,int值,字节replaceByte)
    {
        //你应该有多少位转移replaceByte把它到位
        变种shiftBits = 8 *指数;        //位与此同值来清除应该成为replaceByte位
        VAR面膜=〜(0xFF的<< shiftBits);        //清除这些位,然后将它们设置为任何replaceByte是
        返回值及放大器;面膜| (replaceByte<< shiftBits);
    }    //测试3
    静态INT ReplaceByte3(INT指数,int值,字节replaceByte)
    {
        VAR字节= BitConverter.GetBytes(值);
        字节[指数] = replaceByte;        返回BitConverter.ToInt32(字节,0);
    }


解决方案

没有,没有字节数组。这其实很简单。

没有测试:

  INT ReplaceByte(INT指数,int值,字节replaceByte)
{
    收益率(值和放大器;〜(0xFF的<<(指数* 8)))| (replaceByte&所述;≤(指数* 8));
}

首先,它清除指定索引处的空间在哪里,然后它把新的价值空间。

A int is composed of 4 bytes. How could I replace one of those 4 bytes with a new byte. In other words I am looking for a method:

int ReplaceByte(int index, int value, byte replaceByte)
{
     // implementation
}

for example if I have the value FFFFFFFF (-1) and I will like to replace the byte 0 with 0A (10) then I will call the method as:

ReplaceByte(0,-1,10)

and I will like that method to return me FFFFFF0A

Do I have to convert the int to a byte array then replace the byte I want then convert back to an int? I am looking for an efficient way of doing this. We are creating a debugger like program that connects to a target (board) and we update these values very frequently.

Edit (results)

Thanks to your answers I compared the methods:

here are the results:

Note my implementation was the slowest!

Here is the code:

    static void Main ( string[ ] args )
    {
        byte[ ] randomBytes = new byte[ 1024 * 1024 * 512 ]; 

        Random r = new Random( );
        r.NextBytes( randomBytes );

        Int64 sum;
        var now = DateTime.Now;

        Console.WriteLine( "Test 1" );
        sum = 0;
        now = DateTime.Now;
        foreach ( var bt in randomBytes )
        {
            sum += ReplaceByte1( 1 , -1 , bt );
        }

        Console.WriteLine( "Test 1 finished in {0} seconds \t hash = {1} \n" , ( DateTime.Now - now ).TotalSeconds, sum );

        Console.WriteLine( "Test 2" );
        sum = 0;
        now = DateTime.Now;
        foreach ( var bt in randomBytes )
        {
            sum += ReplaceByte2( 1 , -1 , bt );
        }

        Console.WriteLine( "Test 2 finished in {0} seconds \t hash = {1} \n" , ( DateTime.Now - now ).TotalSeconds,  sum );


        Console.WriteLine( "Test 3" );
        sum = 0;
        now = DateTime.Now;
        foreach ( var bt in randomBytes )
        {
            sum += ReplaceByte3( 1 , -1 , bt );
        }

        Console.WriteLine( "Test 3 finished in {0} seconds \t hash = {1} \n" , ( DateTime.Now - now ).TotalSeconds , sum );

        Console.Read( );            
    }

    // test 1
    static int ReplaceByte1 ( int index , int value , byte replaceByte )
    {
        return ( value & ~( 0xFF << ( index * 8 ) ) ) | ( replaceByte << ( index * 8 ) );
    }

    // test 2
    static int ReplaceByte2 ( int index , int value , byte replaceByte )
    {
        // how many bits you should shift replaceByte to bring it "in position"
        var shiftBits = 8 * index;

        // bitwise AND this with value to clear the bits that should become replaceByte
        var mask = ~( 0xff << shiftBits );

        // clear those bits and then set them to whatever replaceByte is
        return value & mask | ( replaceByte << shiftBits );
    }

    // test 3
    static int ReplaceByte3 ( int index , int value , byte replaceByte )
    {
        var bytes = BitConverter.GetBytes( value );
        bytes[ index ] = replaceByte;

        return BitConverter.ToInt32( bytes , 0 );
    }

解决方案

No, no bytes arrays. This actually very simple.

Not tested:

int ReplaceByte(int index, int value, byte replaceByte)
{
    return (value & ~(0xFF << (index * 8))) | (replaceByte << (index * 8));
}

First it clears the space where at the specified index, and then it puts the new value in that space.

这篇关于在INT替换字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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