HELP: how to put a long in a byte array
Hi I'm trying to put a long value into a byte array, I have used the following but, it doesn't work and I know it has something to do with signed/unsigned byte. How do I fix it?
staticprivatebyte[] long2byte(long theLong )
{
byte[] byteLong =newbyte[4];
byteLong[0] = (byte)( ( theLong >>> 24 )&0xff );
byteLong[1] = (byte)( ( theLong >>> 16 )&0xff );
byteLong[2] = (byte)( ( theLong >>> 8 )&0xff );
byteLong[3] = (byte)( ( theLong >>> 0 )&0xff );
return byteLong;
}
calling this with 149 will result in:
[0][0][0][-107]
but I want it to be:
[0][0][0][149]
Please help,thanks,
Carlo
[1200 byte] By [
bakara] at [2007-9-26 2:56:17]

First, longs are eight bytes, not four.Second, bytes are signed in Java. -107 is the proper value for byteLong[0] when the method is given the number 149. If you can explain why you would want 149 instead of -107, maybe we could help with how to handle negative bytes.
Ok, you're right about longs being 8 bytes, I should of known this, sorry.
So how can I force this to use unsigned bytes?
I'm writing out a quicktime reference movie. the following it the code:
private String createQuicktime( int userId, String url, String filename ) throws Exception
{
long lenurl = (long)url.length();
FileOutputStream fos = new FileOutputStream( getSavePath( userId ) + "/" + filename + ".mov" );
fos.write( long2byte( lenurl + 45 + 16 ) );
fos.write( string2byte( "moov " ) );
fos.write( long2byte( lenurl + 45 + 8 ) );
fos.write( string2byte( "rmra" ) );
fos.write( long2byte( lenurl + 45 ) );
fos.write( string2byte( "rmda" ) );
fos.write( long2byte( 16 ) );
fos.write( string2byte( "rmdr" ) );
fos.write( long2byte( 0 ) );
fos.write( long2byte( 2147483647 ) ); // 0x7fffffff this is the data rate
fos.write( long2byte( lenurl + 21 ) );
fos.write( string2byte( "rdrf" ) );
fos.write( long2byte( 0 ) );
fos.write( string2byte( "url " ) );
fos.write( long2byte( lenurl + 1 ) );
fos.write( string2byte( url ) );
fos.close();
return filename + ".mov";
}
static private byte[] long2byte( long theLong )
{
byte[] byteLong = new byte[4];
byteLong[0] = (byte)( ( theLong >>> 24 )&0xff );
byteLong[1] = (byte)( ( theLong >>> 16 )&0xff );
byteLong[2] = (byte)( ( theLong >>> 8 )&0xff );
byteLong[3] = (byte)( ( theLong )&0xff );
return byteLong;
}
static private byte[] string2byte( String theString )
{
byte[] byteString = new byte[ theString.length() ];
for ( int i=0; i<theString.length(); i++ )
{
byteString[i] = (byte)theString.charAt( i );
}
return byteString;
}
>
bakara at 2007-6-29 10:47:16 >
![top of Java-index,Archived Forums,Portability & Platform Independence [Archive]...](/library/images/10/top.gif)
I truly don't see any reason to use "unsigned bytes". Why do you think you need to?
In addition to the wrong number of bytes in a long, your program doesn't convert Strings to byte arrays correctly -- it will work only for Strings that contain only ISO-Latin 1 characters. Other characters will be silently "converted" into other characters by your cast from char to byte. It would be easier and more correct to simply use the getBytes() method of String than your string2byte method.
When reading a valid quicktime reference movie by using the following code:
FileInputStream fis2 = new FileInputStream( new File( "d:/javaref.mov" ) );
int data2;
while ( ( data2 = fis2.read() ) != -1 )
{
if ( 31 < data2 && data2 < 127)
{
System.out.print( "[" + (char)data2 + "]");
}
else
{
System.out.print( "[" + data2 + "]" );
}
}
fis2.close();
I get the following:
[0][0][0][149][m][o][o][v][0][0][0][141][r][m][r][a][0][0][0][133][r][m][d][a][0][0][0][16][r][m][d][r][0][0][0][0][127][255][255][255][0][0][0][m][r][d][r][f][0][0][0][0][u][r][l][ ][0][0][0][Y][r][t][s][p][:][/][/][a][2][9][3][.][q][.][k][a][m][a][i][.][n][e][t][/][7][/][2][9][3][/][1][0][6][0][/][3][a][8][7][4][2][4][c][/][s][o][r][e][n][s][o][n][.][d][o][w][n][l][o][a][d][.][a][k][a][m][a][i][.][c][o][m][/][1][0][6][0][/][1][/][6][/][3][9][.][m][o][v][0]
and when I run my program to create the same reference movie and then read it with the same routine posted in this message i get:
[0][0][0][-107][m][o][o][v][ ][0][0][0][-115][r][m][r][a][0][0][0][-123][r][m][d][a][0][0][0][16][r][m][d][r][0][0][0][0][127][-1][-1][-1][0][0][0][m][r][d][r][f][0][0][0][0][u][r][l][ ][0][0][0][Y][r][t][s][p][:][/][/][a][2][9][3][.][q][.][k][a][m][a][i][.][n][e][t][/][7][/][2][9][3][/][1][0][6][0][/][3][a][8][7][4][2][4][c][/][s][o][r][e][n][s][o][n][.][d][o][w][n][l][o][a][d][.][a][k][a][m][a][i][.][c][o][m][/][1][0][6][0][/][1][/][6][/][3][9][.][m][o][v][0]
as you can see the working original .mov file uses unsigned bytes, this is what I would like to do.
Thanks,
Carlo
bakara at 2007-6-29 10:47:16 >
![top of Java-index,Archived Forums,Portability & Platform Independence [Archive]...](/library/images/10/top.gif)
A byte is a byte is a byte. It's 8 bits. Signed -107 is the same as unsigned 149.I don't see how the Java code you show can have negative values, though. The read method is defined to return unsigned bytes.
Allright,
I didn't understand how this all worked, but now I do thanks for pointing me in the right direction. The following is the resulting working code:
private String createQuicktime( int userId, String url, String filename ) throws Exception
{
url += '\000';
int lenurl = url.length();
FileOutputStream fos = new FileOutputStream( getSavePath( userId ) + "/" + filename + ".mov" );
fos.write( int2ByteArray( lenurl + 44 + 16 ) );
fos.write( "moov".getBytes() );
fos.write( int2ByteArray( lenurl + 44 + 8 ) );
fos.write( "rmra".getBytes() );
fos.write( int2ByteArray( lenurl + 44 ) );
fos.write( "rmda".getBytes() );
fos.write( int2ByteArray( 16 ) );
fos.write( "rmdr".getBytes() );
fos.write( int2ByteArray( 0 ) );
fos.write( int2ByteArray( 2147483647 ) );
fos.write( int2ByteArray( lenurl + 20 ) );
fos.write( "rdrf".getBytes() );
fos.write( int2ByteArray( 0 ) );
fos.write( "url ".getBytes() );
fos.write( int2ByteArray( lenurl ) );
fos.write( url.getBytes() );
fos.close();
return filename + ".mov";
}
static private byte[] int2ByteArray( int theInt )
{
byte[] byteLong = new byte[4];
byteLong[0] = (byte)( ( theInt >>> 24 )&0xff );
byteLong[1] = (byte)( ( theInt >>> 16 )&0xff );
byteLong[2] = (byte)( ( theInt >>> 8 )&0xff );
byteLong[3] = (byte)( ( theInt )&0xff );
return byteLong;
}
bakara at 2007-6-29 10:47:16 >
![top of Java-index,Archived Forums,Portability & Platform Independence [Archive]...](/library/images/10/top.gif)
