Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  bytes and ints frustration!!  (Read 1732 times)
0 Members and 1 Guest are viewing this topic.
Offline namrog84

JGO Ninja


Medals: 46
Projects: 4


Keep programming!


« Posted 2011-04-06 20:40:05 »

So I am trying to adapt some c++ code into java,  because I wasn't unable to find an example in java for what I wanted.

Basic problem
no unsigned bytes in java
byte = -127 to 127
unsigned byte would be 0 to 255

so I use int instead, which is fine

except, when I try passing it to the GL11.glTexImage2D
which requires a bytebuffer, which I can convert bytes to bytebuffer no problem.

So basically what I am trying to accomplish is converting a int to a byte
all the examples I find are shifting a single byte into a 4 long array of a byte.  for the added potential values
all my values of int are 0 to 255,   so I dont need the added values.

but I need to shift my int down -127 so it falls into proper range of byte, then convert to byte.

Any suggestions?

All online things show int to byte arrays,    though I just need an int to byte

or a conversion process that supports int arrays, an easy way to discard the unnecessary 0s of the byte array[][]


Sorry for the hugely wordy/messy example.


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
int[] pix = new int[250000];

// blah blah random shit

//each value of pix is between 0 and 255


ByteBuffer bb = ByteBuffer.allocate(pix.length);      
bb.put(pix);
GL11.glTexImage2D( GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, 256, 256, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, bb);   //for lwjgl,  


//plan B, rewrite the whole thing to use bytes -127 to 127(from the start),  which requires a painfully manual modification of a huge reference table Sad



///edit
So one possibility?

int[] pix = new int[250000];
byte[] b = new byte[250000];

//large for loop....

b[value]=(byte) (pix[value]-127);





"Experience is what you get when you did not get what you wanted"
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 840
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2011-04-06 20:53:25 »

Casting already does this for you:

int x = 0..255;
byte b = (byte)x; // -128 .. +127, this is not done through subtraction, but a side effect of how the values are represented in bits.
int y = b & 0xFF; // back to 0..255

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Offline DzzD
« Reply #2 - Posted 2011-04-06 20:54:03 »

 Huh

I probably miss the point but... in case of

int i=-100;
byte b=(int)i;
//now b=-100  and is a byte


EDIT :
ha ok !

yes as mentioned by riven, in "byte format" 255 == -1 this is the same representation in bit => 11111111b

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 840
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2011-04-06 20:59:24 »

Just think of it like this: when casting to a byte, we take the least significant 8 bits of the int, and put it in the byte, as is. Due to two-complement interpretation of the byte, you end up with negative numbers if the upper bit (of the Cool was 1.

hence:

(byte)-100 == -100
(byte)+156 == -100

other way around:
+156 & 0xFF = 156
-100 & 0xFF = 156


Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Offline namrog84

JGO Ninja


Medals: 46
Projects: 4


Keep programming!


« Reply #4 - Posted 2011-04-06 21:34:06 »

the problem with the (byte) was

sure
int i = 50;
byte b = (byte) i;

b is 50

but if
int i = 128;
byte b = (byte) i;

b is -127   so that  when i is 254, b is -1  

I also tested i is 256 and b i think turned out to be 1 ?    (I am not at same computer anymore, so from memory may be slightly off)
it wraps it around the wrong way, where I need my int = 0,  to be my byte = -127
But anyways, I went with the (byte) and a subtract 127 method, which led to other problems I need to figure out though.


Thanks all!!

"Experience is what you get when you did not get what you wanted"
Offline DzzD
« Reply #5 - Posted 2011-04-06 21:39:45 »

Quote
but if
int i = 128;
byte b = (byte) i;

b is -127   so that  when i is 254, b is -1 

 Shocked

yes ??

128 should give -128
254 should give -2


Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 840
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #6 - Posted 2011-04-06 21:40:30 »

There are two options:
1. you're doing it wrong, again and again
2. everybody else never noticed this bug you speak of

Pick one.

I explained it already, and if you're still claiming that (byte)256==-1, no matter how vague your memory is, it's starting to get rather silly.

Let me spoon feed you:
System.out.println((byte) 256); => "0"

Why? Just as I said: the 8 least significant bits are taken from the int.
256 == 00000000 00000000 00000001 00000000

You can clearly see the lowest significant bits (righthand side) are zero, no way this yields 1.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Offline namrog84

JGO Ninja


Medals: 46
Projects: 4


Keep programming!


« Reply #7 - Posted 2011-04-06 22:31:22 »

I never meant/said it was a bug, I meant the way it was implemented behaved differently then what I thought my goal was.

(byte)-100 == -100
(byte)+156 == -100 this part just threw me off, for how the 156 and 100


I apologize.  I got it now. Thank you


Also, this thread should probably be moved to "Newbie & Debugging Questions"
My mistake on wrong subforum :/

"Experience is what you get when you did not get what you wanted"
Offline nsigma
« Reply #8 - Posted 2011-04-06 22:32:17 »

wrong way, where I need my int = 0,  to be my byte = -127


You really sure you don't want int=0 to be byte=0?

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Offline DzzD
« Reply #9 - Posted 2011-04-06 22:40:49 »

Undecided become hard...

you got input from 0 to 255 and wanted them to be in range -128 to 127 ?

byte b=(byte)(inputInt-128);

no ??

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 840
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #10 - Posted 2011-04-06 22:42:58 »

Undecided become hard...

you got input from 0 to 255 and wanted them to be in range -128 to 127 ?

byte b=(byte)(inputInt-128);

no ??

He wanted it to be in the range -128..+127 for the wrong reason. Nobody should do that, if the goal is to convert between ints and bytes, simply cast & mask.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Offline namrog84

JGO Ninja


Medals: 46
Projects: 4


Keep programming!


« Reply #11 - Posted 2011-04-06 23:03:03 »

The problem is solved for me, so I am no longer personally needing anything further on this.

Although its no longer an issue, because DzzD asked and because of a little confusion earlier.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
int[] ext_data = {0,50,75,100,125,150,175,200,225,255};
     
      byte[] b = new byte[ext_data.length];
     
     
      for(int i=0;i<ext_data.length;i++){
         b[i]=(byte) ext_data[i];
      }
         
      for(int i=0;i<ext_data.length;i++){
         System.out.print(ext_data[i] + " ");
      }
      System.out.println();
      for(int i=0;i<ext_data.length;i++){
         System.out.print(b[i] + " ");
      }
      for(int i=0;i<ext_data.length;i++){
         b[i]=(byte) (ext_data[i]-128);
      }
      System.out.println();
      for(int i=0;i<ext_data.length;i++){
         System.out.print(b[i] + " ");
      }
     


i[] = 0 50 75 100 125 150 175 200 225 255  // original data example (I don't have control over this)
b[] = 0 50 75 100 125 -106 -81 -56 -31 -1  // a direct (byte) casted
b[] = -128 -77 -52 -27 -2 23 48 73 98 127  // is what I had wanted


The importance wasn't on a direct 10=10,  but the importance of keeping the data in the same relativity of the number before/after it was the same. A direct cast causes a hiccup in the middle that I couldn't have.

"Experience is what you get when you did not get what you wanted"
Offline Riven
« League of Dukes »

« JGO Overlord »


Medals: 840
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #12 - Posted 2011-04-06 23:16:03 »

1  
2  
3  
4  
         for (int i = 0; i < 256; i++)
         {
            System.out.println(i + " => " + ((byte) i) + " => " + (((byte) i) & 0xFF));
         }


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
230  
231  
232  
233  
234  
235  
236  
237  
238  
239  
240  
241  
242  
243  
244  
245  
246  
247  
248  
249  
250  
251  
252  
253  
254  
255  
256  
0 => 0 => 0
1 => 1 => 1
2 => 2 => 2
3 => 3 => 3
4 => 4 => 4
5 => 5 => 5
6 => 6 => 6
7 => 7 => 7
8 => 8 => 8
9 => 9 => 9
10 => 10 => 10
11 => 11 => 11
12 => 12 => 12
13 => 13 => 13
14 => 14 => 14
15 => 15 => 15
16 => 16 => 16
17 => 17 => 17
18 => 18 => 18
19 => 19 => 19
20 => 20 => 20
21 => 21 => 21
22 => 22 => 22
23 => 23 => 23
24 => 24 => 24
25 => 25 => 25
26 => 26 => 26
27 => 27 => 27
28 => 28 => 28
29 => 29 => 29
30 => 30 => 30
31 => 31 => 31
32 => 32 => 32
33 => 33 => 33
34 => 34 => 34
35 => 35 => 35
36 => 36 => 36
37 => 37 => 37
38 => 38 => 38
39 => 39 => 39
40 => 40 => 40
41 => 41 => 41
42 => 42 => 42
43 => 43 => 43
44 => 44 => 44
45 => 45 => 45
46 => 46 => 46
47 => 47 => 47
48 => 48 => 48
49 => 49 => 49
50 => 50 => 50
51 => 51 => 51
52 => 52 => 52
53 => 53 => 53
54 => 54 => 54
55 => 55 => 55
56 => 56 => 56
57 => 57 => 57
58 => 58 => 58
59 => 59 => 59
60 => 60 => 60
61 => 61 => 61
62 => 62 => 62
63 => 63 => 63
64 => 64 => 64
65 => 65 => 65
66 => 66 => 66
67 => 67 => 67
68 => 68 => 68
69 => 69 => 69
70 => 70 => 70
71 => 71 => 71
72 => 72 => 72
73 => 73 => 73
74 => 74 => 74
75 => 75 => 75
76 => 76 => 76
77 => 77 => 77
78 => 78 => 78
79 => 79 => 79
80 => 80 => 80
81 => 81 => 81
82 => 82 => 82
83 => 83 => 83
84 => 84 => 84
85 => 85 => 85
86 => 86 => 86
87 => 87 => 87
88 => 88 => 88
89 => 89 => 89
90 => 90 => 90
91 => 91 => 91
92 => 92 => 92
93 => 93 => 93
94 => 94 => 94
95 => 95 => 95
96 => 96 => 96
97 => 97 => 97
98 => 98 => 98
99 => 99 => 99
100 => 100 => 100
101 => 101 => 101
102 => 102 => 102
103 => 103 => 103
104 => 104 => 104
105 => 105 => 105
106 => 106 => 106
107 => 107 => 107
108 => 108 => 108
109 => 109 => 109
110 => 110 => 110
111 => 111 => 111
112 => 112 => 112
113 => 113 => 113
114 => 114 => 114
115 => 115 => 115
116 => 116 => 116
117 => 117 => 117
118 => 118 => 118
119 => 119 => 119
120 => 120 => 120
121 => 121 => 121
122 => 122 => 122
123 => 123 => 123
124 => 124 => 124
125 => 125 => 125
126 => 126 => 126
127 => 127 => 127
128 => -128 => 128
129 => -127 => 129
130 => -126 => 130
131 => -125 => 131
132 => -124 => 132
133 => -123 => 133
134 => -122 => 134
135 => -121 => 135
136 => -120 => 136
137 => -119 => 137
138 => -118 => 138
139 => -117 => 139
140 => -116 => 140
141 => -115 => 141
142 => -114 => 142
143 => -113 => 143
144 => -112 => 144
145 => -111 => 145
146 => -110 => 146
147 => -109 => 147
148 => -108 => 148
149 => -107 => 149
150 => -106 => 150
151 => -105 => 151
152 => -104 => 152
153 => -103 => 153
154 => -102 => 154
155 => -101 => 155
156 => -100 => 156
157 => -99 => 157
158 => -98 => 158
159 => -97 => 159
160 => -96 => 160
161 => -95 => 161
162 => -94 => 162
163 => -93 => 163
164 => -92 => 164
165 => -91 => 165
166 => -90 => 166
167 => -89 => 167
168 => -88 => 168
169 => -87 => 169
170 => -86 => 170
171 => -85 => 171
172 => -84 => 172
173 => -83 => 173
174 => -82 => 174
175 => -81 => 175
176 => -80 => 176
177 => -79 => 177
178 => -78 => 178
179 => -77 => 179
180 => -76 => 180
181 => -75 => 181
182 => -74 => 182
183 => -73 => 183
184 => -72 => 184
185 => -71 => 185
186 => -70 => 186
187 => -69 => 187
188 => -68 => 188
189 => -67 => 189
190 => -66 => 190
191 => -65 => 191
192 => -64 => 192
193 => -63 => 193
194 => -62 => 194
195 => -61 => 195
196 => -60 => 196
197 => -59 => 197
198 => -58 => 198
199 => -57 => 199
200 => -56 => 200
201 => -55 => 201
202 => -54 => 202
203 => -53 => 203
204 => -52 => 204
205 => -51 => 205
206 => -50 => 206
207 => -49 => 207
208 => -48 => 208
209 => -47 => 209
210 => -46 => 210
211 => -45 => 211
212 => -44 => 212
213 => -43 => 213
214 => -42 => 214
215 => -41 => 215
216 => -40 => 216
217 => -39 => 217
218 => -38 => 218
219 => -37 => 219
220 => -36 => 220
221 => -35 => 221
222 => -34 => 222
223 => -33 => 223
224 => -32 => 224
225 => -31 => 225
226 => -30 => 226
227 => -29 => 227
228 => -28 => 228
229 => -27 => 229
230 => -26 => 230
231 => -25 => 231
232 => -24 => 232
233 => -23 => 233
234 => -22 => 234
235 => -21 => 235
236 => -20 => 236
237 => -19 => 237
238 => -18 => 238
239 => -17 => 239
240 => -16 => 240
241 => -15 => 241
242 => -14 => 242
243 => -13 => 243
244 => -12 => 244
245 => -11 => 245
246 => -10 => 246
247 => -9 => 247
248 => -8 => 248
249 => -7 => 249
250 => -6 => 250
251 => -5 => 251
252 => -4 => 252
253 => -3 => 253
254 => -2 => 254
255 => -1 => 255

No hiccups in the middle...

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social
Offline namrog84

JGO Ninja


Medals: 46
Projects: 4


Keep programming!


« Reply #13 - Posted 2011-04-06 23:22:30 »

^^ Riven is correct. ^^

127 => 127 => 127
128 => -128 => 128

I had previously thought that would cause problems for the glTexImage and not have a smooth transition for a bumpmap/heightmap

"Experience is what you get when you did not get what you wanted"
Offline cylab

JGO Ninja


Medals: 55



« Reply #14 - Posted 2011-04-07 14:49:31 »

Just for completeness, someone might have needed to explain, that signed numbers are encoded in two's complement:http://en.wikipedia.org/wiki/Two%27s_complement

Mathias - I Know What [you] Did Last Summer!
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (31 views)
2014-12-15 09:26:44

Mr.CodeIt (23 views)
2014-12-14 19:50:38

BurntPizza (50 views)
2014-12-09 22:41:13

BurntPizza (84 views)
2014-12-08 04:46:31

JscottyBieshaar (45 views)
2014-12-05 12:39:02

SHC (59 views)
2014-12-03 16:27:13

CopyableCougar4 (57 views)
2014-11-29 21:32:03

toopeicgaming1999 (123 views)
2014-11-26 15:22:04

toopeicgaming1999 (114 views)
2014-11-26 15:20:36

toopeicgaming1999 (32 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!