Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 49c6405209baa6b52af4a7c09a7aa1a5a824b67e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*******************************************************************************
 * Copyright (c) 2007 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     David Pickens - [Memory View] Endian in hex view and ASCII view doesn't work
 *******************************************************************************/
package org.eclipse.debug.internal.ui.views.memory.renderings;

import java.math.BigInteger;

import org.eclipse.debug.core.model.MemoryByte;
import org.eclipse.debug.internal.ui.DebugUIPlugin;
import org.eclipse.debug.ui.IDebugUIConstants;

/**
 * @since 3.3
 */
public class HexIntegerRendering extends AbstractIntegerRendering {

	public HexIntegerRendering(String renderingId)
	{
		super(renderingId);
	}

	@Override
	public String getString(String dataType, BigInteger address,
			MemoryByte[] data) {
		StringBuffer strBuffer = new StringBuffer();
		int endianess = getEndianness(data);

		String paddedStr = DebugUIPlugin.getDefault().getPreferenceStore().getString(IDebugUIConstants.PREF_PADDED_STR);

        if (endianess == RenderingsUtil.LITTLE_ENDIAN) {
            MemoryByte[] swapped = new MemoryByte[data.length];
            for (int i = 0; i < data.length; i++){
                swapped[data.length-i-1] = data[i];
            }
            data = swapped;
        }

		for (int i=0; i<data.length; i++)
		{
			if (data[i].isReadable())
			{
				strBuffer.append(new String(RenderingsUtil.convertByteToCharArray(data[i].getValue())));
			}
			else
			{
				// pad with padded string
				strBuffer.append(paddedStr);
			}
		}

		return strBuffer.toString().toUpperCase();
	}

    /**
     * @todo davidp needs to add a method comment.
     * @param data
     * @return
     */
    private int getEndianness (MemoryByte[] data) {
        // if the user has not set an endianess to the rendering
        // take default
        int endianess = getDisplayEndianess();
        if (endianess == RenderingsUtil.ENDIANESS_UNKNOWN) {
			endianess = getBytesEndianess(data);
		}
        return endianess;
    }

	@Override
	public byte[] getBytes(String dataType, BigInteger address,
			MemoryByte[] currentValues, String data) {

		int endianess = getEndianness(currentValues);
		byte[] bytes = RenderingsUtil.convertHexStringToByteArray(data, currentValues.length, getNumCharsPerByte());


        if (endianess == RenderingsUtil.LITTLE_ENDIAN) {
            byte[] swapped = new byte[bytes.length];
            for (int i = 0; i < bytes.length; i++){
                swapped[bytes.length-i-1] = bytes[i];
            }
           bytes = swapped;
        }

		return bytes;
	}

	@Override
	public int getNumCharsPerByte()
	{
		return 2;
	}


}

Back to the top