简体   繁体   English

怎么char []到Hex? C

[英]How to char[] to Hex? C

So I have kind of hit a road block in my project. 所以我在项目中遇到了障碍。 What I am trying to do is figure out a way to take four individual binary characters which are representing flags. 我想要做的是找出一种方法来获取代表标志的四个单独的二进制字符。 Now my last task was to take these four flags and put them in a char[] which wasn't difficult at all. 现在我的最后一项任务是拿下这四个标志并将它们放在一个并不困难的char []中。 But now, I want to take the four and convert it to a hex. 但现在,我想取四个并将其转换为十六进制。

So for example: 例如:

O = 1;
C = 1;
Z = 0;
N = 0;

char flags[5];
flags[0] = O;
flags[1] = C;
flags[2] = Z;
flags[3] = N;

Now I wanted to make a string or something i can convert the above to a Hex. 现在我想创建一个字符串或者我可以将上面的内容转换为Hex。 So for example I want to have 1100 converted to a hex which is 0xC 所以例如我想将1100转换为十六进制,即0xC

Now I have tried to make it in to a string first then parse it but I'm confused and lost now. 现在我先尝试将它变成一个字符串,然后再解析它,但我现在感到困惑和迷失。 I just can't see to get the right output. 我只是看不到正确的输出。

int flags;
flags = (O << 3) | (C << 2) | (Z << 1) | N;
sprintf(buffer, "0x%02X", 0xff & flags);

flags is defined a single int -variable containing all your flags. flags定义了一个包含所有标志的int -variable。

buffer is a char array of sufficient size. buffer是一个足够大小的char数组。

The 0xff & .. is not needed in this case, but might be some day if your flags variable can get negative and you still only want to have a one byte output (2 hex digits). 在这种情况下不需要0xff & ..但是如果你的flags变量可以变为负数并且你仍然只想要一个字节输出(2个十六进制数字),那么可能是某一天。

unsigned char flags = 0; unsigned char flags = 0; flags = ((O << 3) | (C << 2) | (Z << 1) | N); flags =((O << 3)|(C << 2)|(Z << 1)| N);

now flag contains the Hex value you need. now flag包含您需要的Hex值。

put flags[4]="\\0" put flags [4] =“\\ 0”

we can have a new variable-char pointer pointing to this array of characters. 我们可以有一个新的变量char指针指向这个字符数组。

then you can use sprintf(newArray[i], "%x", flags[i]); 然后你可以使用sprintf(newArray[i], "%x", flags[i]);

this will generate each character into HEX in newArray string, we can get the output by printing the string. 这将在newArray字符串中生成每个字符到HEX,我们可以通过打印字符串来获取输出。 printf("%s\\n",newArray);

You can use this code for convert a byte[] to hex string 您可以使用此代码将byte []转换为十六进制字符串

Cast your char[] into byte[] or modify the code as you need :) 将char []转换为byte []或根据需要修改代码:)

    /// <summary>
    /// Converts a byte array to a hex string. For example: {10,11} = "0A 0B"
    /// </summary>
    public static string ByteToHex(byte[] data)
    {
        StringBuilder sb = new StringBuilder();

        foreach (byte b in data)
        {
            string hex = ByteToHex(b);
            sb.Append(hex);
            sb.Append(" ");
        }

        if (sb.Length > 0)
            sb.Remove(sb.Length - 1, 1);

        return sb.ToString();
    }

    /// <summary>
    /// Converts the byte to a hex string. For example: "10" = "0A";
    /// </summary>
    public static string ByteToHex(byte b)
    {
        string sB = b.ToString(ConstantReadOnly.HexStringFormat, CultureInfo.InvariantCulture);

        if (sB.Length == 1)
            sB = "0" + sB;

        return sB;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM