| |

VerySource

 Forgot password?
 Register
Search
Author: rogue110

rms storage how to store int array data

[Copy link]

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 Invalid IP Address

Post time: 2020-7-28 10:15:01
| Show all posts
ByteArrayOutputStream baops = new ByteArrayOutputStream();
DataOutputStream outputStream = new DataOutputStream(baops);
outputStream.writeInt();
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-7 21:00:01
| Show all posts
public static byte[] int2Byte(int n) {
return new byte[] {(byte) ((n >> 24)&0xff),
(byte) ((n >> 16)&0xff), (byte) ((n >> 8)&0xff),
(byte) (n&0xff) };

}
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-8-8 09:45:01
| Show all posts
There are 2 ways.
1. Encode int first
  ByteArrrayOutputStream baos = new ByteArrayOutputStream();
  DataOutputStream dos = new DataOutputStream();
  dos.writeInt(i);//i is your integer data

  byte[] result = null;
  result = baos.toByteArray();

  Decode when used:
  ByteArrayInputStream bais = new ByteArrayInputStream();
  DataInputStream dis = new DataInputStream();
  
  int i = dis.readInt();

2.
  byte[] b = new byte[4]; (Int is 32 bits)
  b[0] = (byte)(0xff&(i >> 24));
  b[1] = (byte)(0xff&(i >> 16));
  b[2] = (byte)(0xff&(i >> 8));
  b[3] = (byte)(0xff&(i >> 0));
Reply

Use magic Report

0

Threads

23

Posts

17.00

Credits

Newbie

Rank: 1

Credits
17.00

 China

Post time: 2020-8-8 12:15:01
| Show all posts
Seeing so many of you, String is the easiest!
public void save(int n)
{
String s=new String(""+n); //Integer becomes a string.
rs.write(s.getBytes(),0,s.length); //Assuming rs is your Record object
}
public int get(int position)
{
byte[] temp=rs.getRecord(position);
return Integer.parseInt(new String(temp));
}
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 Invalid IP Address

Post time: 2020-8-8 14:00:01
| Show all posts
It is best not to use string. If you save both int and short, string and byte[], wouldn't it be troublesome...
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-12 01:45:01
| Show all posts
byte[] result = null;
  result = baos.toByteArray();
what's the point?
Reply

Use magic Report

0

Threads

2

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-8-12 02:00:01
| Show all posts
int i = dis.readInt();
dos.writeInt(i);
Are these two I the same?
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-8-12 12:30:01
| Show all posts
int i = dis.readInt();
dos.writeInt(i);
Are these two I the same?

===================================
===================================

the same
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list