C - 用十六进制打印出一个char指针给我带来了奇怪的结果

时间:2013-08-28 20:43:59

标签: c

所以我正在编写一个非常小而简单的程序,它将一个数字作为输入,将其转换为十六进制,并一次打印出两个字符。

对于某些数字,它会在输出前打印出ffffff。

这是我的代码:

    //Convert the input to an unsigned int
unsigned int a = strtoul (argv[1], NULL, 0);
//Convert the unsigned int to a char pointer
char* c = (char*) &a;

//Print out the char two at a time
for(int i = 0; i < 4; i++){
    printf("%02x ", c[i]);
}

大部分输出都很好,看起来像这样:

./hex_int 1

01 00 00 00

但是对于某些数字,输出看起来像这样:

./hex_int 100000

ffffffa0 ffffff86 01 00

如果删除所有f,则转换是正确的,但我无法弄清楚为什么它只在某些输入上执行此操作。

有人有什么想法吗?

4 个答案:

答案 0 :(得分:4)

您的参数和打印格式不匹配。默认参数提升会导致您的char参数(c[i])升级为int,该符号会延伸(显然您的char是签名类型)。然后,您告诉printf使用unsigned int格式将该参数解释为%x。繁荣 - 未定义的行为。

使用:

printf("%02x ", (unsigned int)(unsigned char)c[i]);

相反。

答案 1 :(得分:0)

默认情况下,

char已在您的系统上签名。将int移入其中会使编译器认为您希望将最高位用作需要转换为int的操作的符号位。将其打印为十六进制数是一个。

将char指针c设为unsigned char *,问题就会消失。

如果Carl对他的评论是正确的(我将要检查),请使用此故障安全方法:

printf("%02x ", c[i] & 0xff);

答案 2 :(得分:-1)

我这样做了一段时间并且我意识到,你可以在一个过程中看到exe图像,使用debuger,代码没问题,但我暂时无法将这个机器代码传递给汇编程序,但是很好,指针都是可能的

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_margin="16dp"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:orientation="vertical"
            android:layout_weight="3" >

            <EditText
                android:id="@+id/edittext"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="16dp"
                android:layout_marginTop="30dp"
                android:ems="5"
                android:hint="Name"
                android:inputType="text" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="16dp"
                android:text="Toppings"
                android:textAllCaps="true" />

            <CheckBox
                android:id="@+id/whippedcream_checkbox_id"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="16dp"
                android:paddingLeft="24dp"
                android:text="Whipped Cream"
                android:textSize="16sp" />

            <CheckBox
                android:id="@+id/chocolate_checkbox_id"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="16dp"
                android:paddingLeft="24dp"
                android:text="Chocolate"
                android:textSize="16sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="16dp"
                android:text="Quantity"
                android:textAllCaps="true" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <Button
                    android:layout_width="48dp"
                    android:layout_height="48dp"
                    android:onClick="decrement"
                    android:text="-" />

                <TextView
                    android:id="@+id/quantity_text_view"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="8dp"
                    android:text="2"
                    android:textColor="@android:color/black"
                    android:textSize="16sp" />

                <Button
                    android:layout_width="48dp"
                    android:layout_height="48dp"
                    android:onClick="increment"
                    android:text="+" />

            </LinearLayout>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:text="order summary"
                android:textAllCaps="true" />


            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:onClick="submitOrder"
                android:text="Order" />

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="PRICE:" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Coffee: Rs.5 per cup" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Whipped Cream: Rs.1 per cup additional" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Chocolate: Rs.2 per cup additional" />
        </LinearLayout>
    </LinearLayout>
</ScrollView>

答案 3 :(得分:-2)

嗯,添加了windows.h以使用一些系统调用,我省略了示例中的那些,因为不需要在下面的代码中使用那些,关于unsigned,是的,它可以被省略,因为char总是&gt ; 0,关于地址,如前所述,0400000是exe的标题,在这种情况下是“mz”标题,但是进程的dll代码可能是7c911000,而dll的标题是另一个地址,等等..,这个信息在exe文件中,必须被重新加载以了解加载图像在主存中加载的位置,但是当你只有一个进程时,如果你有更多,你必须使用系统调用要知道第二个进程在哪里,如果一个地址不可读,只需在记事本中读取de exe,该地址只是一个例子

相关问题