• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EST 10:03
CET 16:03
KST 00:03
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
TL.net Map Contest #21: Winners11Intel X Team Liquid Seoul event: Showmatches and Meet the Pros10[ASL20] Finals Preview: Arrival13TL.net Map Contest #21: Voting12[ASL20] Ro4 Preview: Descent11
Community News
[TLMC] Fall/Winter 2025 Ladder Map Rotation6Weekly Cups (Nov 3-9): Clem Conquers in Canada4SC: Evo Complete - Ranked Ladder OPEN ALPHA8StarCraft, SC2, HotS, WC3, Returning to Blizzcon!45$5,000+ WardiTV 2025 Championship7
StarCraft 2
General
[TLMC] Fall/Winter 2025 Ladder Map Rotation Mech is the composition that needs teleportation t Weekly Cups (Nov 3-9): Clem Conquers in Canada Craziest Micro Moments Of All Time? SC: Evo Complete - Ranked Ladder OPEN ALPHA
Tourneys
Constellation Cup - Main Event - Stellar Fest Tenacious Turtle Tussle Sparkling Tuna Cup - Weekly Open Tournament $5,000+ WardiTV 2025 Championship Merivale 8 Open - LAN - Stellar Fest
Strategy
Custom Maps
Map Editor closed ?
External Content
Mutation # 499 Chilling Adaptation Mutation # 498 Wheel of Misfortune|Cradle of Death Mutation # 497 Battle Haredened Mutation # 496 Endless Infection
Brood War
General
BW General Discussion FlaSh on: Biggest Problem With SnOw's Playstyle Terran 1:35 12 Gas Optimization BGH Auto Balance -> http://bghmmr.eu/ [ASL20] Ask the mapmakers — Drop your questions
Tourneys
[BSL21] RO32 Group D - Sunday 21:00 CET [BSL21] RO32 Group C - Saturday 21:00 CET [ASL20] Grand Finals [Megathread] Daily Proleagues
Strategy
Current Meta PvZ map balance How to stay on top of macro? Soma's 9 hatch build from ASL Game 2
Other Games
General Games
EVE Corporation Nintendo Switch Thread Stormgate/Frost Giant Megathread Should offensive tower rushing be viable in RTS games? Path of Exile
Dota 2
Official 'what is Dota anymore' discussion
League of Legends
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Deck construction bug Heroes of StarCraft mini-set
TL Mafia
TL Mafia Community Thread SPIRED by.ASL Mafia {211640}
Community
General
US Politics Mega-thread Russo-Ukrainian War Thread Things Aren’t Peaceful in Palestine Canadian Politics Mega-thread The Games Industry And ATVI
Fan Clubs
White-Ra Fan Club The herO Fan Club!
Media & Entertainment
[Manga] One Piece Anime Discussion Thread Movie Discussion! Korean Music Discussion Series you have seen recently...
Sports
2024 - 2026 Football Thread Formula 1 Discussion NBA General Discussion MLB/Baseball 2023 TeamLiquid Health and Fitness Initiative For 2023
World Cup 2022
Tech Support
SC2 Client Relocalization [Change SC2 Language] Linksys AE2500 USB WIFI keeps disconnecting Computer Build, Upgrade & Buying Resource Thread
TL Community
The Automated Ban List
Blogs
Dyadica Gospel – a Pulp No…
Hildegard
Coffee x Performance in Espo…
TrAiDoS
Saturation point
Uldridge
DnB/metal remix FFO Mick Go…
ImbaTosS
Reality "theory" prov…
perfectspheres
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1179 users

Java help

Blogs > shmay
Post a Reply
shmay
Profile Blog Joined July 2004
United States1091 Posts
Last Edited: 2008-03-05 05:06:32
March 05 2008 05:05 GMT
#1
Copied from my Hw:

Write a method with the following signature:

public static String oneByte (int value, int byteNum)

This method take in an integer and return, as a String, the specified byte (0-3, numbered right to left) of value as a hexadecimal number.



My question: what the hell is my teacher asking? What does the 0-3 part mean?

Help! Thank you!

***
pheer
Profile Blog Joined July 2004
5391 Posts
March 05 2008 05:14 GMT
#2
I have no idea what the 0-3 part means, but you can just convert INT -> HEX using the built in functions, then convert that to a string. Maybe the 0-3 part is what makes it more than 2 lines of work?
Moderator
Raithed
Profile Blog Joined May 2007
China7078 Posts
March 05 2008 05:18 GMT
#3
0123 ?
AcrossFiveJulys
Profile Blog Joined September 2005
United States3612 Posts
March 05 2008 05:19 GMT
#4
One byte = 1 character
strings are basically a bunch of characters...

if you have something like this

word

'd' is byte 0, 'r' is byte 1, 'o' is byte 2, and 'w' is byte 3 by your teacher's convention.

so the first part is just figuring out how to locate the part of the string that the byte# corresponds to. youll have to do something like

public static String oneByte (int value, int byteNum) {
// convert integer into string
String s = ...

// get byte index
int byteIndex = (s.length() - byteNum) - 1;

// extract the character
char c = s.substring(byteIndex);

// convert into hex
int h = ...

return h;

}

i dont remember java syntax too well, been a couple years. hope this helps.
shmay
Profile Blog Joined July 2004
United States1091 Posts
Last Edited: 2008-03-05 05:27:51
March 05 2008 05:21 GMT
#5
On March 05 2008 14:14 pheer wrote:
I have no idea what the 0-3 part means, but you can just convert INT -> HEX using the built in functions, then convert that to a string. Maybe the 0-3 part is what makes it more than 2 lines of work?


We can't use stuff from the API for the hex conversion
SonuvBob
Profile Blog Joined October 2006
Aiur21550 Posts
March 05 2008 05:23 GMT
#6
On March 05 2008 14:14 pheer wrote:
I have no idea what the 0-3 part means, but you can just convert INT -> HEX using the built in functions, then convert that to a string. Maybe the 0-3 part is what makes it more than 2 lines of work?

32 bit int = 4 bytes. :p
Administrator
shmay
Profile Blog Joined July 2004
United States1091 Posts
March 05 2008 05:29 GMT
#7
oh I see. cool, thank you guys, especially across
AcrossFiveJulys
Profile Blog Joined September 2005
United States3612 Posts
March 05 2008 05:33 GMT
#8
in my post i forgot to mention that you need to turn the hex value you obtain into a string before you return it.
Macavenger
Profile Blog Joined January 2008
United States1132 Posts
Last Edited: 2008-03-05 05:57:56
March 05 2008 05:55 GMT
#9
Across is returning an int when the method signature asks for a String, though. I assume the return should be a string of either 2 characters (giving the integer value of the byte in hex) or 8 characters (giving the byte in binary). Doing it in binary is more work, although much cleaner in converting from int to String, at least for the first method that jumps out at me. I'll try to write out how I would do this for binary, since it's easier to write out:

public static String oneByte (int value, int byteNum) {

// initialize a string to store the return value in
String result = "";

// This division on value will eliminate all bytes lower than byteNum
// Equivalent to dividing a decimal number by 10^3 to get rid of the last 3 digits, for example
value = value / (256 ^ byteNum);

for ( int i = 1; i > 8; i++) {

// Taking the mod 2 of value gives us the value of the last digit
// Since mod 2 returns either a 1 or a 0, we don't need anything else in the if statement,
// but doing value % 2 == 1 might be a bit more readable
if value % 2 {

// We're finding bits from right to left, so when we get a new one,
// it needs to be put at the front of the string
result = "1" + result;
}

else {

result = "0" + result;

}

// Now that we've determined the rightmost remaining bit, we need to get rid of it to find the rest
value = value / 2;

}

// Now we've found all 8 bits and stored them in proper order, so return
return result;

}

Hopefully that syntax isn't too far off, I haven't coded in java in a while. Also hope you can read that, WTB [code] tags on TL so I can format things.

Doing it Hex would give a cleaner result, just 2 characters 0-f. The method is basically the same, except you take the modulo 16 and divide by 16 twice, and have to deal with sorting 15 = f, 10 = a etc. into the string. A big ugly case statement is the easiest way I can think of to do that off the top of my head, and I didn't want to write then, hence why I did it in binary.

Edit: looking at what you said about the API limitation again, what across did is probably fine once you convert it to a string. I read it originally as saying you couldn't convert between types for some reason, which I don't think is right now. Thus doing it via string and not my crazy math way is probably fine, but at least I had fun working this out.
AcrossFiveJulys
Profile Blog Joined September 2005
United States3612 Posts
March 05 2008 06:49 GMT
#10
lol MacAvenger overrrrrkillllll

I suppose that's a faster method than mine though since you are doing it in binary and not making API calls to convert back and forth between types.
azndsh
Profile Blog Joined August 2006
United States4447 Posts
Last Edited: 2008-03-05 07:11:50
March 05 2008 07:10 GMT
#11
I don't think anyone has posted the correct solution/interpretation.

An int is 32 bits, or 4 bytes. This can be expressed as 32 digits in binary or 8 digits in hexadecimal. For example

int a = 13371337 would be

00000000 11001100 00000111 11001001
in binary as split into 4 bytes.

or

00 CC 07 C9
in hexadecimal as split into 4 bytes

oneByte (a, 0) would return "C9"
oneByte (a, 1) would return "07"
etc.

That's what the problem is asking. It's pretty much your job to figure out how to actually calculate those return values.
shmay
Profile Blog Joined July 2004
United States1091 Posts
Last Edited: 2008-03-05 07:29:57
March 05 2008 07:21 GMT
#12
yeah, you're correct azndsh. i learned that soon after i tried implementing it. that's what i needed. ty
Macavenger
Profile Blog Joined January 2008
United States1132 Posts
March 05 2008 07:55 GMT
#13
azndsh, that's the interpretation I was working off when I designed mine. I'm pretty sure that's what both Across and I are doing, unless you see something in our code we don't Well, my code is returning the binary value "11001001" instead of "C9", but unless it specifies in the problem statement whether to return in binary or hex either should be valid.
azndsh
Profile Blog Joined August 2006
United States4447 Posts
March 05 2008 09:16 GMT
#14
the problem specifies the hex value, and also, it's probably best not to post the actual solution, but to let the person figure out the actual programming part
shmay
Profile Blog Joined July 2004
United States1091 Posts
March 05 2008 09:48 GMT
#15
Any tips on how to go about this? I converted the int to a 4 byte array, but I have no idea how to manually convert one of those bytes to hex.
shmay
Profile Blog Joined July 2004
United States1091 Posts
Last Edited: 2008-03-05 11:06:12
March 05 2008 10:00 GMT
#16
cancel the byte array, now have this:

value = ((value << (8*byteNum)) >>> 24) & 0x000000ff;

still open for hints on the hex conversion

edit: nm, I think i have it. don't worry.

got it
Cambium
Profile Blog Joined June 2004
United States16368 Posts
March 05 2008 19:29 GMT
#17
This makes perfect sense...

Since an int is 32 bits, it has 4 bytes (hence 0 - 3).

Numbered right to left means.... 3-2-1-0.

It basically asks you to take a chunk of the integer and output it in hex.
When you want something, all the universe conspires in helping you to achieve it.
Cambium
Profile Blog Joined June 2004
United States16368 Posts
Last Edited: 2008-03-05 19:46:42
March 05 2008 19:43 GMT
#18
I got held up answering some email, here's the solution... I think (I'm not gonna run it).

Pretty easy...

public static String oneByte (int value, int byteNum)
{
String binaryCode = Integer.toBinaryString(value);
int returnValue = Integer.parseInt( binaryCode.substring( (3 - byteNum) * 8, (3 - byteNum) * 8 + 7 ), 2 );
return Integer.toHexString( returnValue );
}
When you want something, all the universe conspires in helping you to achieve it.
FreeZEternal
Profile Joined January 2003
Korea (South)3396 Posts
March 05 2008 21:08 GMT
#19
public static String oneByte(int value, int byteNum){
byte[] byteHolder = new byte[4];
//Little-indian.
byteHolder[3] = (byte)((value >> 24) & 0xff);
byteHolder[2] = (byte)((value >> 16) & 0xff);
byteHolder[1] = (byte)((value >> 8) & 0xff);
byteHolder[0] = (byte)(value & 0xff);
return "0x" + Integer.toHexString(byteHolder[byteNum] & 0xff);

}
Please log in or register to reply.
Live Events Refresh
Kung Fu Cup
12:00
2025 Monthly #3: Day 1
ByuN vs ShoWTimELIVE!
RotterdaM787
TKL 217
SteadfastSC147
Rex133
IntoTheiNu 83
Liquipedia
OSC
11:30
Mid Season Playoffs
Cure vs SpiritLIVE!
Krystianer vs Percival
WardiTV574
Liquipedia
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
RotterdaM 787
TKL 217
Rex 133
SteadfastSC 130
StarCraft: Brood War
Calm 3557
Bisu 2912
Rain 2601
Hyuk 1494
Horang2 1048
Flash 554
Soma 510
Stork 377
Rush 278
Backho 145
[ Show more ]
Soulkey 123
Barracks 61
hero 42
sas.Sziky 30
sSak 23
Aegong 22
Killer 22
zelot 21
Rock 20
Terrorterran 11
Noble 6
Dota 2
Gorgc3032
qojqva1803
Dendi1041
BananaSlamJamma125
XcaliburYe114
Counter-Strike
olofmeister1193
Other Games
B2W.Neo987
hiko570
DeMusliM348
Hui .278
Fuzer 177
Sick170
Mew2King117
QueenE52
Organizations
StarCraft: Brood War
Kim Chul Min (afreeca) 10
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 17 non-featured ]
StarCraft 2
• poizon28 14
• Adnapsc2 7
• LaughNgamezSOOP
• AfreecaTV YouTube
• sooper7s
• intothetv
• Migwel
• Kozan
• IndyKCrew
StarCraft: Brood War
• HerbMon 1
• STPLYoutube
• ZZZeroYoutube
• BSLYoutube
Dota 2
• C_a_k_e 2430
• WagamamaTV423
League of Legends
• Nemesis3849
• TFBlade859
Upcoming Events
Tenacious Turtle Tussle
7h 57m
The PondCast
18h 57m
RSL Revival
18h 57m
Solar vs Zoun
MaxPax vs Bunny
Kung Fu Cup
20h 57m
WardiTV Korean Royale
20h 57m
PiGosaur Monday
1d 9h
RSL Revival
1d 18h
Classic vs Creator
Cure vs TriGGeR
Kung Fu Cup
1d 20h
CranKy Ducklings
2 days
RSL Revival
2 days
herO vs Gerald
ByuN vs SHIN
[ Show More ]
Kung Fu Cup
2 days
IPSL
3 days
ZZZero vs rasowy
Napoleon vs KameZerg
BSL 21
3 days
Tarson vs Julia
Doodle vs OldBoy
eOnzErG vs WolFix
StRyKeR vs Aeternum
Sparkling Tuna Cup
3 days
RSL Revival
3 days
Reynor vs sOs
Maru vs Ryung
Kung Fu Cup
3 days
WardiTV Korean Royale
3 days
BSL 21
4 days
JDConan vs Semih
Dragon vs Dienmax
Tech vs NewOcean
TerrOr vs Artosis
IPSL
4 days
Dewalt vs WolFix
eOnzErG vs Bonyth
Wardi Open
4 days
Monday Night Weeklies
5 days
WardiTV Korean Royale
5 days
The PondCast
6 days
Liquipedia Results

Completed

Proleague 2025-11-07
Stellar Fest: Constellation Cup
Eternal Conflict S1

Ongoing

C-Race Season 1
IPSL Winter 2025-26
KCM Race Survival 2025 Season 4
SOOP Univ League 2025
YSL S2
BSL Season 21
BLAST Rivals Fall 2025
IEM Chengdu 2025
PGL Masters Bucharest 2025
Thunderpick World Champ.
CS Asia Championships 2025
ESL Pro League S22
StarSeries Fall 2025
FISSURE Playground #2
BLAST Open Fall 2025
BLAST Open Fall Qual

Upcoming

SLON Tour Season 2
BSL 21 Non-Korean Championship
Acropolis #4
IPSL Spring 2026
HSC XXVIII
RSL Offline Finals
WardiTV 2025
RSL Revival: Season 3
META Madness #9
BLAST Bounty Winter 2026
BLAST Bounty Winter 2026: Closed Qualifier
eXTREMESLAND 2025
ESL Impact League Season 8
SL Budapest Major 2025
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2025 TLnet. All Rights Reserved.