Our Online Store have the new products: RFID antenna board. Currently it can work with JC10M24R and JCOP4 card chips.
Compared with normal cards, the antenna board module has a smaller size and fixed holes, which is easy to integrate in the IOT(Internet Of Things) project.

convert data character string to java array

Algorithm School

Moderator: UNKNwYSHSA

User avatar
NikerCR
Posts: 10
Joined: Fri Jun 19, 2015 6:49 am
Points :15
Contact:

convert data character string to java array

Post by NikerCR » Tue Sep 01, 2015 2:19 am

Every time I process algorithm computation, there is much data to be inputted.
How can I convert data character string to java array quickly ?
Such as "12345678" to 0x12, 0x34, 0x56, 0x78
One way.

User avatar
UNKNwYSHSA
Posts: 630
Joined: Thu May 21, 2015 4:05 am
Points :3053
Contact:

Re: convert data character string to java array

Post by UNKNwYSHSA » Sat Sep 05, 2015 9:59 pm

You can write code for your needs.
Here's mine:

Code: Select all

# -*- coding:utf-8 -*-
import sys

def to_java_byte_array(input_string):
    while input_string.count(' ') != 0:
        input_string = input_string.replace(' ', '')
   
    if ((len(input_string) & 1) != 0) :
        print "The input string length is not multiply of 2, please check."
        return -1;
    java_byte_array = "";
    for index in range(0, len(input_string) >> 1):
        offset = index << 1
        java_byte_array += "0x%c%c, " %(input_string[offset], input_string[offset + 1])
    java_byte_array.strip(', ')
    print java_byte_array

if __name__ == "__main__":
    argv = sys.argv
    argc = len(argv)
    if argc < 2:
        print "Usage: ~ <input_string>"
        exit(-1)
   
    exit(to_java_byte_array(argv[1]))

This code can be modified for other requirements, java byte array, C byte array, etc.
sense and simplicity

Post Reply Previous topicNext topic

Who is online

Users browsing this forum: No registered users and 9 guests

JavaCard OS : Disclaimer