簡體   English   中英

使用C ++中的十六進制值初始化unsigned char數組

[英]Initializing an unsigned char array with hex values in C++

我想用16個十六進制值初始化一個unsigned char數組。 但是,我似乎不知道如何正確初始化/訪問這些值。 當我試圖直觀地訪問它們時,我根本沒有任何價值。

這是我的輸出

The program was run with the following command: 4
Please be a value! -----> p
Here's some plaintext 

使用以下代碼運行時 -

int main(int argc, char** argv)
{
  int n;
  if (argc > 1) {
    n = std::stof(argv[1]);
  } else {
    std::cerr << "Not enough arguments\n";
    return 1;
  }

  char buff[100];
  sprintf(buff,"The program was run with the following command: %d",n);
  std::cout << buff << std::endl;

unsigned char plaintext[16] = 
   {0x0f, 0xb0, 0xc0, 0x0f,
    0xa0, 0xa0, 0xa0, 0xa0,
    0x00, 0x00, 0xa0, 0xa0,
    0x00, 0x00, 0x00, 0x00};

unsigned char test = plaintext[1]^plaintext[2];

std::cout << "Please be a value! -----> " << test << std::endl;

std::cout << "Here's some plaintext " << plaintext[3] << std::endl;

  return 0;
}

從背景來看,這是學校集體項目的一部分。 我們最終試圖實現Serpent密碼,但繼續被無符號的char數組絆倒。 我們的項目規范說我們必須有兩個函數來接受Java中的Byte數組。 我假設C ++中最接近的親戚是unsigned char []。 否則我會使用矢量。 在代碼的其他地方,我實現了一個setKey函數,該函數接受一個unsigned char數組,將其值打包成4個long long int(密鑰需要為256位),並對這些int執行各種位移和xor運算以生成密碼算法所必需的密鑰。 希望有足夠的背景來做我想做的事情。 我猜我在這里只是忽略了一些基本的C ++功能。 感謝您的幫助!

char是一個8位值,能夠存儲-128 <= n <= +127,經常用於存儲不同編碼的字符表示,通常用於西方,羅馬字母表安裝 - char用於表示ASCII或utf編碼值。 “編碼”表示字符集中的符號/字母已分配數值。 將周期表視為元素的編碼,使'H'(氫)編碼為1,鍺編碼為32.在ASCII(和UTF-8)表中,位置32表示我們稱之為“空格”的字符。

在char值上使用operator <<時,默認行為是假設您正在傳遞字符編碼,例如ASCII字符代碼。 如果你這樣做

char c = 'z';
char d = 122;
char e = 0x7A;
char f = '\x7a';
std::cout << c << d << e << f << "\n";

所有四項任務都是等同的。 'z'是char(122)的快捷/語法糖, 0x7A是122的十六進制,'\\ x7a'是一個轉義,形成一個值為0x7a或122的ascii字符 - iez

許多新程序員出錯的地方是他們這樣做:

char n = 8;
std::cout << n << endl;

這不會打印“8”,它會在ASCII表的第8位打印ASCII字符。

想一想:

char n = 8;  // stores the value 8
char n = a;  // what does this store?
char n = '8'; // why is this different than the first line?

讓我們回顧一下:當你在一個變量中存儲120時,它可以表示ASCII字符'x' ,但最終存儲的只是數值120 ,簡單明了。

具體來說:當您將122傳遞給最終使用它從使用Latin1,ISO-8859-1,UTF-8或類似編碼的字符集中查找字體條目的函數時, 120表示'z'

在一天結束時, char只是標准整數值類型之一,它可以存儲值-128 <= n <= +127 ,它可以簡單地提升為shortintlonglong long等,等等

雖然它通常用於表示字符,但它也經常被用作說“我只存儲非常小的值”(例如整數百分比)的方式。

int incoming = 5000;
int outgoing = 4000;
char percent = char(outgoing * 100 / incoming);

如果要打印數值,只需將其提升為不同的值類型:

std::cout << (unsigned int)test << "\n";
std::cout << unsigned int(test) << "\n";

或者首選的C ++方式

std::cout << static_cast<unsigned int>(test) << "\n";

我認為(你所要求的並不完全清楚)答案就像這樣簡單

std::cout << "Please be a value! -----> " << static_cast<unsigned>(test) << std::endl;

如果要輸出char或unsigned char的數值,則必須先將其強制轉換為int或unsigned。

毫不奇怪,默認情況下,字符輸出為字符而不是整數。

BTW這個時髦的代碼

char buff[100];
sprintf(buff,"The program was run with the following command: %d",n);
std::cout << buff << std::endl;

更簡單地寫成

std::cout << "The program was run with the following command: " << n << std::endl;

std::coutstd::cin始終將char變量視為char
如果要輸入或輸出為int,則必須手動執行以下操作。

std::cin >> int_var; c = int_var;
std::cout << (int)c;

如果使用scanf或printf,則沒有這樣的問題,因為格式參數(“%d”,“%c”,“%s”)告訴如何隱藏輸入緩沖區(整數,字符串,字符串)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM