|
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!
|
5385 Posts
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?
|
|
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.
|
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
|
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
|
oh I see. cool, thank you guys, especially across
|
in my post i forgot to mention that you need to turn the hex value you obtain into a string before you return it.
|
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.
|
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.
|
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.
|
yeah, you're correct azndsh. i learned that soon after i tried implementing it. that's what i needed. ty
|
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.
|
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
|
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.
|
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
|
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.
|
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 ); }
|
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); }
|
|
|
|