0

I have to update the date and time of a time clock in a dynamic way. I am not able to convert a string to a format accepted by the variable char (not char array).

Is there another way?

Thus that's there works! But not so dynamic ...

char year = 0x0E;
char month = 0x0A;
char day = 0x0A;
char hour = 0x0A;
char min = 0x0A;
char sec = 0x0A;

byte[] date_hour = new byte[]{(byte) 0xFE, (byte) 0x82, (byte) 0x71, (byte) 0x00, (byte) 0x0B, 
                   (byte) 0x01, (byte) 0x00, (byte) 0x07, 
                   (byte) year, (byte) month, (byte) day, (byte) hour, (byte) min, (byte) sec, 
                   (byte) 0x0A, (byte) 0x17, (byte) 0x0E, (byte) 0x0A, (byte) 0x17};

out.write(date_hour);

I wanted something like:

public String getYear() {
    return year;
}

//gets...

char year = getYear();
char month = getMont();
char day = getDay();
char hour = getHour();
char min = getMin();
char sec = getSec();

byte[] date_hour = new byte[]{(byte) 0xFE, (byte) 0x82, (byte) 0x71, (byte) 0x00, (byte) 0x0B, 
                   (byte) 0x01, (byte) 0x00, (byte) 0x07, 
                   (byte) year, (byte) month, (byte) day, (byte) hour, (byte) min, (byte) sec, 
                   (byte) 0x0A, (byte) 0x17, (byte) 0x0E, (byte) 0x0A, (byte) 0x17};

out.write(date_hour);

I tried: Hex-encoded String to Byte Array How to convert/parse from String to char in java?

Sorry for my bad english ... Thanks!

Community
  • 1
  • 1
Eduardo
  • 97
  • 1
  • 9
  • The char values you need have no relationship to the characters in a string. Rather, they are numeric values stored in a `char` (which is simply a small integer). (In fact, you should probably not use `char` at all, but should declare your values as `byte`.) – Hot Licks Oct 29 '14 at 12:10
  • Yes, an hour value is not a char - start by using appropriate data types. Joda time has some good classes and Java8 has a decent date/time package. – BarrySW19 Oct 29 '14 at 12:16
  • I don't get the problem... What are you really trying to do? What is `out.write`? Why do you have a byte array? – bobbel Oct 29 '14 at 12:30
  • I did making it! Thank you `int year = Integer.parseInt("0E",16);` `byte[] date_hour = new byte[]{(byte)((year)&0xff)}; ` – Eduardo Oct 29 '14 at 13:46

1 Answers1

0

If you want to convert String to char, you can do this:

Suppose str is the variable name of string.

char ch = str.toCharArray()[0];

or, You can use:

char ch = str.charAt(0);

Both of these will store the first character (which will be the only character in case the String is truly a character) of the String str int the Character ch.

dryairship
  • 6,022
  • 4
  • 28
  • 54