开发者

Need to initialize a byte[] array with ascii blank (i.e. 0x20 OR " ")

Hello all Ol' Guy Newbie here ... I've defined a string=> final static String Blank = " " ; and a byte array static byte[] LU62_Partner = new byte[8] ;

Further down in my logic I want to initialize the byte[] array with blanks

     // Prep LU6.2 Session  
 for ( ndx=0 ; ndx < 8 ; ++ndx)
   { 
     LU62_Partner[ndx] = Blank.getBytes() ;    // initialize the the LU6.2 partner name byte array w/blanks  
   }
 LU62_Partner = APPC_Partner.getBytes() ;      // convert string array to byte array 
                                               // if the appc-partner name < 8 bytes, rightmost bytes
                                               // will be padded with blanks 

However upon compilation I get the following error src\LU62XnsCvr.java:199: incompatible types found : byte[] required: byte LU62_Partner[ndx] = Blank.getBytes() ;

Again I'm confused... I was under the impression that the method ge开发者_高级运维tBytes() would convert a string into a byte. Thanks very much again

Guy


getBytes() returns an array, so you are attempting to jam an array of bytes into a byte

use

Arrays.fill(LU62_Partner, (byte)' ');


I think you want that line to be

LU62_Partner[ndx] = Blank.getBytes()[0];

The variable on the left-hand-side of the assignment is a byte, and so the value on the right-hand-side should also be a byte, not an array of bytes.

In any case, since you're already implicitly assuming that space is a single byte, why not just say

LU62_Partner[ndx] = (byte) ' ';

or

LU62_Partner[ndx] = 0x20; 

(because hex 20 is space)?

EDIT: and as @MeBigFatGuy points out, Arrays.fill() would let you eliminate your explicit loop altogether.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜