Skip to Content
-1

how to convert a character string to a hex string

Nov 27, 2016 at 06:37 AM

822

avatar image

Hi all, I am a Java Programmer, and I have two functions:

1. covert a character string to a hex string

2. covert a hex string to a character string

I can implement these functions in Java, anyone can help me implement these functions in ABAP. below is my Java code:

import java.io.ByteArrayOutputStream;


public class Main {
	private static String hexString = "0123456789ABCDEF";
	/**
	 * encoding a character string to hex String
	 * @param str
	 * @return
	 */
	public static String encode(String str) {
		byte[] bytes = str.getBytes();
		StringBuilder sb = new StringBuilder(bytes.length * 2);
		for (int i = 0; i < bytes.length; i++) {
			sb.append(hexString.charAt((bytes[i] & 0xf0) >> 4));
			sb.append(hexString.charAt((bytes[i] & 0x0f) >> 0));
		}
		return sb.toString();
	}
	/**
	 * decoding a hex string to character string
	 * @param bytes
	 * @return
	 */
	public static String decode(String bytes) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream(bytes.length() / 2);
		for (int i = 0; i < bytes.length(); i += 2)
			baos.write((hexString.indexOf(bytes.charAt(i)) << 4 | hexString.indexOf(bytes.charAt(i + 1))));
		return new String(baos.toByteArray());
	}
	public static void main(String[] args) {
		String ch = "寄售差额调整";
		System.out.println("‘寄售差额调整’ in Hex String:"+Main.encode(ch));
		System.out.println("‘BCC4CADBB2EEB6EEB5F7D5FB’ in Chinese charater String::"+Main.decode(Main.encode(ch)));


	}
}

Thanks in advance.

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

4 Answers

Best Answer
Sandra Rossi Nov 27, 2016 at 10:59 AM
1

There's an implicit data type conversion between byte-like strings and character-like strings (cf ABAP documentation: Conversion Rules for Elementary Data Types):

DATA hex TYPE xstring.
DATA chars TYPE string.
chars = 'EFBBBF'. "only uppercase
hex = chars.
chars = hex.
Share
10 |10000 characters needed characters left characters exceeded
Joy K Nov 28, 2016 at 11:58 AM
-1
Show 1 Share
10 |10000 characters needed characters left characters exceeded

This FM systematically dumps in Unicode systems (unicode being mandatory from 7.50)

0
Balaraju Pandiri Nov 28, 2016 at 12:24 PM
-1

Hi,

Please use below code.

* READ TABLE i_data into w_data INDEX 1.
** move w_data-line to v_xml.
CALL FUNCTION 'HR_KR_STRING_TO_XSTRING'
EXPORTING
* CODEPAGE_TO = '8500'
unicode_string = l_xml_table
* OUT_LEN =
IMPORTING
xstring_stream = v_xml.
* EXCEPTIONS
* INVALID_CODEPAGE = 1
* INVALID_STRING = 2
* OTHERS = 3
* .

Thanks,

Bala

Show 1 Share
10 |10000 characters needed characters left characters exceeded

This FM does a code page conversion (to Korean), but the OP doesn't ask for it.

0
Horst Keller
Nov 28, 2016 at 06:30 PM
0

If a codepage conversion is needed, use CL_ABAP_CODEPAGE, Methods CONVERT_TO and CONVERT_FROM.

Otherwise, as Sandra said ...

Share
10 |10000 characters needed characters left characters exceeded