转至:http://blog.csdn.net/centralperk/article/details/7533857
版权声明:本文为博主原创文章,未经博主允许不得转载。
-
<pre
class
=
“java”
name=
“code”
>
public
static
void
main(String[] args)
throws
IOException {
-
// TODO Auto-generated method stub
-
String s =
“AD6DEC74E223517170487C238A7527B0DD0CA1C684FC13F666473E7C08323F9F66473E7C08323F9F66473E7C08323F9F66473E7C08323F9F66473E7 C08323F9F66473E7C08323F9F0000000000000000”
;
-
byte
[] bcd = ASCII_To_BCD(s.getBytes(), s.length());
-
String s1 = bcd2Str(bcd);
-
-
}
-
-
private
static
byte
asc_to_bcd(
byte
asc) {
-
byte
bcd;
-
-
if
((asc >=
‘0’
) && (asc <=
‘9’
))
-
bcd = (
byte
) (asc –
‘0’
);
-
else
if
((asc >=
‘A’
) && (asc <=
‘F’
))
-
bcd = (
byte
) (asc –
‘A’
+
10
);
-
else
if
((asc >=
‘a’
) && (asc <=
‘f’
))
-
bcd = (
byte
) (asc –
‘a’
+
10
);
-
else
-
bcd = (
byte
) (asc –
48
);
-
return
bcd;
-
}
-
-
private
static
byte
[] ASCII_To_BCD(
byte
[] ascii,
int
asc_len) {
-
byte
[] bcd =
new
byte
[asc_len /
2
];
-
int
j =
0
;
-
for
(
int
i =
0
; i < (asc_len +
1
) /
2
; i++) {
-
bcd[i] = asc_to_bcd(ascii[j++]);
-
bcd[i] = (
byte
) (((j >= asc_len) ?
0x00
: asc_to_bcd(ascii[j++])) + (bcd[i] <<
4
));
-
}
-
return
bcd;
-
}
-
-
public
static
String bcd2Str(
byte
[] bytes) {
-
char
temp[] =
new
char
[bytes.length *
2
], val;
-
-
for
(
int
i =
0
; i < bytes.length; i++) {
-
val = (
char
) (((bytes[i] &
0xf0
) >>
4
) &
0x0f
);
-
temp[i *
2
] = (
char
) (val >
9
? val +
‘A’
–
10
: val +
‘0’
);
-
-
val = (
char
) (bytes[i] &
0x0f
);
-
temp[i *
2
+
1
] = (
char
) (val >
9
? val +
‘A’
–
10
: val +
‘0’
);
-
}
-
return
new
String(temp);
-
}</pre><br>
-
<pre></pre>
-
<pre></pre>