Click here to Skip to main content
16,020,628 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello All,

Thanks in advance.

I have one method setpower which is used to set hardware power.
Java
SetHardwarePower(new byte[]{0x0a,0x14,0x14,0x14});

In above method you 0x0a means in 10 , 0x14 means 20.

For API design I wrote one method For e.g. SetRfPower(10,20,20,20)

Java
public boolean SetPower(int Power1,int Power2,int Power3,int Power4)
{
    //here I have to convert int to hex
    String power1inhex = "0x";
    power1inhex += Integer.toHexString(Power1); // power1inhex = 0x0a
    
    // Please help me to write code here. How should I use power1inhex string as byte to pass in method SetHardwarePower.
    
    SetHardwarePower(new byte[]{0x0a,0x14,0x14,0x14});
}

I tried a lot. please let us know any separate approach.
Posted
Updated 5-Nov-15 23:16pm
v2
Comments
[no name] 6-Nov-15 6:03am    
There is nothing to convert: Hex 0x14 is exactly the same value as decimal 20.
[no name] 6-Nov-15 7:41am    
but the value is in string format. Method needs it in byte format. If I write like

SetHardwarePower(new byte[]{power1inhex ,0x14,0x14,0x14});

it doesn't work.
[no name] 6-Nov-15 10:06am    
I suggest you buy a book on basic computer theory. You seem to have a total misunderstanding between the way data is stored, and how it is displayed.

Whether you write
SetHardwarePower(new byte[]{power1inhex ,0x14,0x14,0x14});
or
SetHardwarePower(new byte[]{power1inhex ,20,20,20});

the result is exactly the same.
[no name] 7-Nov-15 0:37am    
Thanks for suggestion.
If it could be that much simple why should I asked here.

I tried that. But it doesn't work dear.
[no name] 7-Nov-15 0:38am    
power1inhex is string it doesnt treat as byte

First of all, if you use int as your argument type you might run into an overflow situation. Better to use byte and do a type cast when you call the method (if necessary)
Java
public boolean SetPower(byte Power1, byte Power2, byte Power3, byte Power4)
{
    //here I have to convert int to hex
    String power1inhex = "0x";
    power1inhex += Integer.toHexString(Power1); // power1inhex = 0x0a

    // Please help me to write code here. How should I use power1inhex string as byte to pass in method SetHardwarePower.

    // No conversion needed. The computer will interpret your the input values as
    // hexadecimal numbers automatically. (Not the best explanation, I know) 
    SetHardwarePower(new byte[]{Power1, Power2, Power3, Power4});
}


If the user is entering decimal values as a string, you only need to convert from string to byte.
Java
string aString = "10";
byte aByte = (byte)Integer.parseInt(aString);
        dec   hex     binary
aByte = 10 or 0x0A or 0000 1010 (it all depends on how you choose to show the value)
 
Share this answer
 
v3
Comments
[no name] 6-Nov-15 7:39am    
Thanks.
I am taking input from user. If user will give value as integer like 10,20,30.. He wont understand byte or hex.

So consider I have value 10,20,20,20...

How should I convert it into 0x0a,0x14,0x14,0x14 as byte not a string ... i.e. what actually I am looking for.
George Jonsson 6-Nov-15 8:51am    
I told you in the solution that you don't need any conversion.
10 as decimal is equal to 0x0A in hexadecimal or '0000 1010' in binary representation.
It is just a matter of how you present it on a printout. Internally the computer uses binary code.
So test the code and use the debugger and you will see that it works.
George Jonsson 6-Nov-15 8:56am    
How do you read the user input? As a string?
[no name] 7-Nov-15 0:41am    
yes
George Jonsson 7-Nov-15 2:26am    
Then see my updated solution.
The solution is to read Java documentation.

In Java documentation, you will learn that there is more than one way to write an integer in source code and the same apply to user integer input.

Any integer can be typed in Hexadecimal, Decimal, Octal and Binary. Your µ-proc only handle Binary, so the compiler and the integer input command silently convert any integer to its Binary representation, no matter what.
if the user input is 100 or 0144 or 0x64 or 0b1100100, you will only ever know the decimal value 100 stop
You don't have to care about theses details.
http://rodrigosasaki.com/2013/06/10/number-literals-in-java/[^]
 
Share this answer
 
Comments
[no name] 7-Nov-15 0:40am    
ok

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900