繁体   English   中英

数字写入字节/ Java

[英]numeric to writeBytes / Java

我需要从数据库中的表中获取一个数字,然后将其作为ChannelBuffer放入宽度为4个字节的字段中。

该数字可以是1至9999,例如。 237,在ChannelBuffer中,我需要填充4个字节。

ChannelBuffer bufcmd = ChannelBuffers.directBuffer(4);
bufcmd.writeByte(0x00); // 1º digit of 4 bytes
bufcmd.writeByte(0x00); // 2º digit of 4 bytes
bufcmd.writeByte(0x00); // 3º digit of 4 bytes
bufcmd.writeByte(0x01); // 4º digit of 4 bytes

如何直接用237值填充?

整个代码是:

String strcmd = "dyd#"; //from database

ChannelBuffer bufcmd = ChannelBuffers.directBuffer(100);

bufcmd.writeByte(0x78);
bufcmd.writeByte(0x78);
bufcmd.writeByte((5 + strcmd.length()));
bufcmd.writeByte(0x80);
bufcmd.writeByte((4 + strcmd.length()));



// TODO: convert 1 to 9999 to HEX 
// (id of command from database eg: 3)
// but filling 4 bytes
bufcmd.writeByte(0x00);
bufcmd.writeByte(0x00);
bufcmd.writeByte(0x00);
bufcmd.writeByte(0x03);


bufcmd.writeBytes(ChannelBuffers.copiedBuffer(strcmd, CharsetUtil.US_ASCII));
bufcmd.writeShort(buf.readUnsignedShort());
bufcmd.writeShort(Crc.crc16Ccitt(bufcmd.toByteBuffer(2, 4)));
bufcmd.writeByte(0x0D);
bufcmd.writeByte(0x0A);
channel.write(bufcmd);

这将转换为大端字节(二进制):

   int theNumber = 237;
   byte[] buf = new byte[4];
   for( int i = 0; i < 4; i++ ){
   buf[3 - i] = (byte)(theNumber & 0xFF);
       theNumber = theNumber >> 8;
   }

请注意,java.io.DataOutputStream和类似的类具有方法writeInt(int v)和其他类型的类似方法,从而简化了您似乎考虑过的任务。

下面的代码将转换为十进制ASCII,最高有效数字在前,以零开头:

   int theNumber = 237;
   byte[] buf = new byte[4];
   for( int i = 0; i < 4; i++ ){
   buf[3 - i] = (byte)(theNumber % 10 + 0x30);
       theNumber = theNumber / 10;
   }

不知道您想要在bufcmd中使用哪种编码等,我将大胆猜测“无论使用哪种String都可以”

int theNumber = 237; // The value
String theString = Integer.toString(theNumber); //The string representation of the value
for(int i=0; i<4;i++)
{
      bufcmd.writeByte( theString.charAt(i) );
}

补充:一个完整​​的实现,该实现考虑了字符串长度并添加了零填充,还将字符转换为整数,如果不需要,请删除包裹在字符周围的Character.getNumericValue()调用。

int theNumber = 237; // The value
String theString = Integer.toString(theNumber); //The string representation of the value
int len = theString.length(); //Bytes in the string
int requiredLen = 4; // 3 = 0003, 42 = 0042
int padding = requiredLen-len; //How many 0 we need to send first

while( padding-- > 0 )
{
    bufcmd.writeByte( 0 );
}

for(int i=0; i<len;i++)
{
    bufcmd.writeByte( Character.getNumericValue(theString.charAt(i)) );
}

您也可以计算位置值,而不是使用Math.pow将其转换为字符串。

这是一个测试证明该方法有效的测试。 http://www.compileonline.com/compile_java_online.php

public class HelloWorld{

     public static void main(String []args){
        System.out.println("Hello World");
        int theNumber = 237; // The value
        String theString = Integer.toString(theNumber); //The string representation of the value
        int len = theString.length(); //Bytes in the string
        int requiredLen = 4; // 3 = 0003, 42 = 0042
        int padding = requiredLen-len; //How many 0 we need to send first

        while( padding-- > 0 )
        {
            System.out.println( 0 );
        }

        for(int i=0; i<len;i++)
        {
            System.out.println( Character.getNumericValue(theString.charAt(i)) );
        }        

     }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM